import java.applet.*; import java.awt.*; import java.awt.event.*; import java.lang.Math; public class crush extends Applet implements Runnable{ int mtd[][] = new int[5][5]; Button rep; Color bg = new Color(30,30,30); Color gl = new Color(200,200,200); Image img[] = new Image[2]; int sx = 4,sy = 0; int px,py,cn; Thread th = null; Image os; Graphics og; public void init(){ setBackground(Color.white); os = createImage(340,360); og = os.getGraphics(); img[0] = getImage(getDocumentBase(),"Picture/knight_g.gif"); img[1] = getImage(getDocumentBase(),"Picture/knight_s.gif"); for(int ix = 0;ix < 5;ix ++){ for(int iy = 0;iy < 5;iy ++){ mtd[iy][ix] = 2; } } mtd[0][4] = 0 ; mtd[4][0] = 1; setLayout(new BorderLayout()); Panel pan = new Panel(); pan.setLayout(new FlowLayout()); pan.add(rep = new Button(" replay ")); add("South",pan); rep.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ for(int ix = 0;ix < 5;ix ++){ for(int iy = 0;iy < 5;iy ++){ mtd[iy][ix] = 2; } } mtd[0][4] = 0 ; mtd[4][0] = 1; sx = 4;sy = 0; repaint(); } }); addMouseListener( new MouseAdapter(){ public void mousePressed(MouseEvent e){ px = (e.getX() - 20) / 60; py = (e.getY() - 20) / 60; if((Math.abs(sx - px) + Math.abs(sy - py)) == 3){ if((sx != px) && (sy != py)){ mtd[sy][sx] = mtd[py][px]; mtd[py][px] = 0; repaint(1,px * 60 + 20,py * 60 + 20,60,60); repaint(1,sx * 60 + 20,sy * 60 + 20,60,60); sx = px ; sy = py ; } } } } ); } public void paint(Graphics g){ og.setColor(Color.white); og.fillRect(0,0,340,360); for(int ix = 0;ix < 5;ix ++){ for(int iy = 0;iy < 5;iy ++){ og.setColor(bg); og.fill3DRect(ix * 60 + 20,iy * 60 + 20,59,59,true); } } og.setColor(gl); og.fill3DRect(4 * 60 + 20,0 * 60 + 20,59,59,true); og.setColor(Color.black); og.drawString("GOAL",4 * 60 + 35,0 * 60 + 55); for(int ix = 0;ix < 5;ix ++){ for(int iy = 0;iy < 5;iy ++){ if(mtd[iy][ix] == 1){ og.drawImage(img[0],ix * 60 + 25,iy * 60 + 25,this); }else if(mtd[iy][ix] == 2){ og.drawImage(img[1],ix * 60 + 25,iy * 60 + 25,this); } } } g.drawImage(os,0,0,this); } public void start(){ if(th == null){ th = new Thread(this); th.start(); } } public void stop(){ th = null; } public void run(){ while(th != null){ try{ Thread.sleep(100); } catch (InterruptedException e){ } } } public void update( Graphics g ){ paint( g ) ; } }