package lession.game.game1;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ppuzzle extends Applet{
Image imgppuzzle,buffer;
Point fifteen=new Point(3,3);
Canvas screen;
Graphics gs,gb;
Button bstart=new Button("开始游戏");
Button bsee=new Button("正确图象");
boolean running;
int sx,sy;
int[][]map={{0,4,8,12},{1,5,9,13},{2,6,10,14},{3,7,11,15}};
public void init(){
PrepareImage();
sx=imgppuzzle.getWidth(this)/4; //?????????????????????????????
sy=imgppuzzle.getHeight(this)/4;
this.setBackground(Color.blue);
InitScreen();
InitButtons();
add(screen);
add(bstart);
add(bsee);
this.setSize(sx*6,sy*4+10);
// this.setVisible(true);
//System.out.println("ddddddd");
}
public void PrepareImage(){
/*Class cl=this.getClass();
URL url=cl.getResource("2.gif");
imgppuzzle=getImage(url);
*/
imgppuzzle=getImage(getCodeBase(),"image/2.gif");
MediaTracker mt=new MediaTracker(this);
mt.addImage(imgppuzzle,0);
try{
mt.waitForAll();
}catch(Exception e){}
buffer=createImage(imgppuzzle.getWidth(this),imgppuzzle.getHeight(this));
gb=buffer.getGraphics();
}
public void InitMap(){
Random rnd=new Random();
int temp,x1,y1,x2,y2;
for(int i=0;i<100;i++){
x1=rnd.nextInt(4);
x2=rnd.nextInt(4);
y1=rnd.nextInt(4);
y2=rnd.nextInt(4);
temp=map[x1][y1];
map[x1][y1]=map[x2][y2];
map[x2][y2]=temp;
}
outer:
for(int j=0;j<4;j++)
for(int i=0;i<4;i++){
if(map[i][j]==15){
fifteen.setLocation(i,j); //设置第15快图象的显示位置
break outer;
}
}
}
public void InitScreen(){
screen=new Canvas(){
public void paint(Graphics g){
if(gs==null) gs=getGraphics();
if(running) DrawScreen();
else //如果游戏没开始显示整个图象
g.drawImage(imgppuzzle,0,0,this);
}
};
screen.setSize(imgppuzzle.getWidth(this),imgppuzzle.getHeight(this));
screen.addMouseListener(new MouseAdapter (){
public void mousePressed(MouseEvent me){
// System.out.println("选择了块1");
if(running==false) return;
// System.out.println("选择了块2");
int x=me.getX()/sx;
int y=me.getY()/sy;
int fx=(int)fifteen.getX();
int fy=(int)fifteen.getY();
// System.out.println(x+","+y+" "+fx+","+fy);
if((Math.abs(fx-x)+Math.abs(fy-y))>=2) return;
// System.out.println("选择了块3");
if(map[x][y]==15) return ;
// System.out.println("选择了块4");
map[fx][fy]=map[x][y]; //交换两快图象的位置 重新设置
map[x][y]=15;
fifteen.setLocation(x,y);
//System.out.println("选择了块");
DrawScreen();
}
});
}
public void InitButtons(){
bstart.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
InitMap();
DrawScreen();
running=true;
bsee.setLabel("显示图象");
}
});
bsee.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(bsee.getLabel().equals("继续")){//??????????????????????????????
DrawScreen();
bsee.setLabel("显示图象");
}
else{
gs.drawImage(imgppuzzle,0,0,screen);
bsee.setLabel("继续");
}
}
});
}
public void DrawScreen(){
gb.clearRect(0,0,sx*4,sy*4);
for(int j=0;j<4;j++)
for(int i=0;i<4;i++){
if(map[i][j]!=15) DrawSegment(map[i][j],i,j);
}
gs.drawImage(buffer,0,0,screen);
}
public void DrawSegment(int seg,int x,int y){
int dx=seg%4 *sx;
int dy=seg/4 *sy;
gb.drawImage(imgppuzzle,x*sx,y*sy,x*sx+sx-1,y*sy+sy-1,dx,dy,dx+sx-1,dy+sy-1,screen);
}
}