📄 applet1.java
字号:
package pintugame4;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import javax.swing.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Applet1 extends Applet implements KeyListener, Runnable, MouseListener {
private boolean isStandalone = false;
Image[] smallImage=new Image[9];//定义9个小图像的图像数组
Image bigImage;//定义大图片
int arrange[][]=new int[3][3];//9个小图像的排列位置
int noImage=-1;//设置空的图像位置为0
int width=90;//设置小图像宽度为90
int height=90;//设置小图像的高度为90
int up=1;//上
int down=2;//下
int left=3;//左
int right=4;//右
boolean rePlay=false;//标记是否重新排列
boolean isShowBig=false;//是否显示大图像
int startx=0;
int starty=0;
int steps=0;//计算所用的步数
int playSeconds=0;//花费的时间
Thread timer;
Button button1 = new Button();
Button button2 = new Button();
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
//Construct the applet
public Applet1() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
String name="pintu.jpg";//设置图片名称
bigImage=getImage(getDocumentBase(),"image/"+name);//设置图片目录
MediaTracker mediaTracker=new MediaTracker(this);//定义一个监听器
mediaTracker.addImage(bigImage,1);
try{//查看图像是否装载成功
mediaTracker.waitForID(1);
}catch(Exception e){
System.out.println("无法装载图片");
}
for(int i=0;i<9;i++){
smallImage[i]=createImage(width,height);//创建小图像
Graphics g=smallImage[i].getGraphics();//获得Graphics对象
int j=i%3;
int k=i/3;
//将大图像的某一区域画到小图像上
g.drawImage(bigImage,0,0,width,height,j*width,k*height,(j+1)*width,(k+1)*height,this);
}
arrangeImage();//随机排列小图像
timer=new Thread(this);//定义线程
timer.start();//开始线程
addKeyListener(this);//添加键盘监听器
addMouseListener(this);//添加鼠标监听器
button1.setLabel("打开");
button1.setBounds(new Rectangle(126, 287, 57, 25));
button1.addActionListener(new Applet1_button1_actionAdapter(this));
this.setLayout(null);
button2.setLabel("重玩");
button2.setBounds(new Rectangle(196, 287, 57, 25));
button2.addActionListener(new Applet1_button2_actionAdapter(this));
this.add(button2, null); this.add(button1, null);
}
//Start the applet
public void start() {
}
//Stop the applet
public void stop() {
}
//Destroy the applet
public void destroy() {
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
public void arrangeImage(){
int[] arr=new int[9];
for(int i=0;i<9;i++)
arr[i]=0;
//随机排列各个小图像的位置
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
int k=-1;
do{
k=(int)(9*Math.random());
}while(arr[k]==1);
arrange[i][j]=k;
arr[k]=1;
}
}
//随机设置一个小图像不显示
arrange[(int)(Math.random()*3)][(int)(Math.random()*3)]=noImage;
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
if(isShowBig){//是否显示大图像,如是则显示大图像,返回
g.drawImage(bigImage,0,0,this);
isShowBig=false;
return;
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
int x=i*width;
int y=j*height;
int z=arrange[i][j];
if(z==-1){
g.fill3DRect(x,y,width,height,true);//将小图像设置为黑色
}else{
g.drawImage(smallImage[z],x,y,this);//绘制小图像
g.drawRect(x,y,width,height);
}
}
g.drawImage(bigImage,3*width+5,0,this);
}
g.setColor(Color.WHITE);
g.fillRect(0,3*height,3*width,height*3);
g.setColor(Color.BLACK);
g.setFont(new Font("宋体",Font.PLAIN,16));
g.drawString("你已走了"+steps+"步",0,20+3*height);
if(isFinish()){
g.drawString("你成功了!",0,40+3*width);
}
}
public void moveImage(int dire){
int m=-1;
int n=-1;
boolean isRepaint=false;//查看小图像是否移动
for(int i=0;i<3;i++){//找出空区域的坐标
for(int j=0;j<3;j++){
if(arrange[i][j]==-1){
m=i;
n=j;
break;
}
}
}
switch (dire){//响应键盘事件
case 1:
if(n==2){//如果空区域在最上面,则无法上移,退出
break;
}
arrange[m][n]=arrange[m][n+1];
arrange[m][n+1]=noImage;//空区域和移动的小图像交换
isRepaint=true;//设置变量为真,重画
break;
case 2:
if(n==0){
break;
}
arrange[m][n]=arrange[m][n-1];
arrange[m][n-1]=noImage;
isRepaint=true;
break;
case 3:
if(m==2){
break;
}
arrange[m][n]=arrange[m+1][n];
arrange[m+1][n]=noImage;
isRepaint=true;
break;
case 4:
if(m==0){
break;
}
arrange[m][n]=arrange[m-1][n];
arrange[m-1][n]=noImage;
isRepaint=true;
break;
default:
break;
}
//如果isRepaint为真,表示小图像移动,重新绘制
if(isRepaint){
steps++;
repaint();//重新绘制图像
}
}
//Main method
public static void main(String[] args) {
Applet1 applet = new Applet1();
applet.isStandalone = true;
Frame frame;
frame = new Frame();
frame.setTitle("Applet Frame");
frame.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(270,300);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
public void keyTyped(KeyEvent e) {
/**@todo Implement this java.awt.event.KeyListener method*/
}
public void keyPressed(KeyEvent e) {
/**@todo Implement this java.awt.event.KeyListener method*/
//如果显示大图像,则重新绘制并返回
if(isShowBig){
if(e.getKeyChar()==KeyEvent.VK_S){
isShowBig=false;
steps=0;
playSeconds=0;
repaint();
}
return;
}
//如果重新开始,则调用arrangeImage方法,重新绘制图像并返回
if(rePlay){
arrangeImage();
rePlay=false;
steps=0;
playSeconds=0;
repaint();
return;
}
switch(e.getKeyCode()){//得到键盘按键信息
case 38:
moveImage(up);//上移
break;
case 40:
moveImage(down);//下移
break;
case 39:
moveImage(right);//右移
break;
case 37:
moveImage(left);//左移
break;
case KeyEvent.VK_R://重新开始
arrangeImage();
steps=0;
repaint();
return;
case KeyEvent.VK_S://显示大图像
isShowBig=true;
playSeconds=0;
steps=0;
repaint();
return;
default:
return;
}
}
public void keyReleased(KeyEvent e) {
/**@todo Implement this java.awt.event.KeyListener method*/
}
public void run() {
/**@todo Implement this java.lang.Runnable method*/
while(Thread.currentThread()==timer){
try{
timer.sleep(1000);//线程睡眠1分钟
String str="你玩了"+playSeconds+"秒的时间,";
showStatus(str);//显示所费的时间
playSeconds++;
}catch(Exception e){
}
}
}
public boolean confirmDir(int row,int col){
System.out.println(col);
System.out.println(row);
if(col>2||row>2){
return false;
}
if(col==1||col==2)
if(arrange[col-1][row]==-1){
arrange[col-1][row] = arrange[col][row];
arrange[col][row]=-1;
return true;
}
if(col==0||col==1)
if(arrange[col+1][row]==-1){
arrange[col+1][row] = arrange[col][row];
arrange[col][row]=-1;
return true;
}
if(row==1||row==2)
if(arrange[col][row-1]==-1){
arrange[col][row-1] = arrange[col][row];
arrange[col][row]=-1;
return true;
}
if(row==1||row==0)
if(arrange[col][row+1]==-1){
arrange[col][row+1] = arrange[col][row];
arrange[col][row]=-1;
return true;
}
return false;
}
public void mouseClicked(MouseEvent e) {
/**@todo Implement this java.awt.event.MouseListener method*/
if(isShowBig)
return;
int x=e.getX()-startx;
int y=e.getY()-starty;
int row=x/width;
int col=y/height;
if(row<=2&&col<=2){
boolean isRepaint = confirmDir(col, row);
if (isRepaint)
steps++;
repaint();
}
}
public boolean isFinish(){
boolean isFinish=true;
int i=0;
for(int j=0;j<3;j++){
for(int k=0;j<3;j++){
if(arrange[k][j]!=i&&arrange[k][j]!=-1)
isFinish=false;
}
}
return isFinish;
}
public void mousePressed(MouseEvent e) {
/**@todo Implement this java.awt.event.MouseListener method*/
}
public void mouseReleased(MouseEvent e) {
/**@todo Implement this java.awt.event.MouseListener method*/
}
public void mouseEntered(MouseEvent e) {
/**@todo Implement this java.awt.event.MouseListener method*/
}
public void mouseExited(MouseEvent e) {
/**@todo Implement this java.awt.event.MouseListener method*/
}
public boolean isFocusTraversable() {
return true;
}
void button1_actionPerformed(ActionEvent e) {
JFileChooser fileChooser1 = new JFileChooser();//定义一个JFileChooser对象
fileChooser1.setCurrentDirectory(new File("."));
//设置可显示的图像文件的后缀名
fileChooser1.setFileFilter(new javax.swing.filechooser.FileFilter(){
public boolean accept(File f){
String name = f.getName().toLowerCase();
return name.endsWith(".gif")|| name.endsWith(".jpg")
|| name.endsWith(".jpeg")|| f.isDirectory();
}
public String getDescription(){ return "Image files";}
});
int t=fileChooser1.showOpenDialog(this);
if(t==JFileChooser.APPROVE_OPTION){
//得到文件后缀名
String name=fileChooser1.getSelectedFile().getAbsolutePath();
bigImage= Toolkit.getDefaultToolkit().getImage(name);//调用图像
MediaTracker mediaTracker = new MediaTracker(this);
mediaTracker.addImage(bigImage,0);
try{//查看图像是否装载成功
mediaTracker.waitForID(0);
}catch(Exception err){
System.out.println("无法装载图片");
}
width=bigImage.getWidth(this)/3;
height=bigImage.getHeight(this)/3;
System.out.println(width);
System.out.println(height);
for(int i=0;i<9;i++){
smallImage[i]=createImage(width,height);//创建小图像
Graphics g=smallImage[i].getGraphics();//获得Graphics对象
int j=i%3;
int k=i/3;
//将大图像的某一区域画到小图像上
g.drawImage(bigImage,0,0,width,height,j*width,k*height,(j+1)*width,(k+1)*height,this);
}
arrangeImage();//随机排列小图像
}
playSeconds=0;
steps=0;
this.requestFocus(true);//窗口获得焦点,响应键盘事件
repaint();
}
void button2_actionPerformed(ActionEvent e) {
arrangeImage();
isShowBig=false;
steps=0;
playSeconds=0;
repaint();
}
}
class Applet1_button1_actionAdapter implements java.awt.event.ActionListener {
Applet1 adaptee;
Applet1_button1_actionAdapter(Applet1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button1_actionPerformed(e);
}
}
class Applet1_button2_actionAdapter implements java.awt.event.ActionListener {
Applet1 adaptee;
Applet1_button2_actionAdapter(Applet1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button2_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -