import java.awt.*; import java.util.*; import java.net.*; public class Flip extends java.applet.Applet implements Runnable{ Image offscreen; // drawing occurs here before copying to screen Graphics offG; // graphics object of offscreen image Image show,hide,draw; Graphics dg; int width=256,height=256,size,drawLevel=3; Random rnd = new Random(System.currentTimeMillis()); Thread runner; URL picURL; boolean goFlip=false,solved=false,paintAll=true; int fi,fj; tile[][] map; //======================================================== //--- primary functions --- public Flip(){} public void init(){ String val = getParameter("size"); if (val==null) size = 4; else size = Integer.parseInt(val); picURL = selectPicture(); show = getImage(picURL); draw = createImage(width,height); hide = createHide(); dg = draw.getGraphics(); offscreen = createImage(width,height); offG = offscreen.getGraphics(); //showToDraw(); map = new tile[size][size]; for (int i=0;i0;count--) { int i = (rnd.nextInt()&0x7fffffff)%size; int j = (rnd.nextInt()&0x7fffffff)%size; flip(i,j); } } public void update(Graphics g){ if (show!=null) { showToDraw(); paintAll = true; } paint(g); } public void paint(Graphics g){ if (show==null) show = getImage(picURL); if (solved) { offG.drawImage(show,0,0,width,height,this); } else for (int i=0;i=0;drawLevel--) { paintAll = true; showToDraw(); repaint(); try {runner.sleep(500,0);} catch (InterruptedException e){} } } public boolean mouseDown(Event evt,int x,int y){ if (goFlip || solved) return true; flip(x*size/width,y*size/height); return true; } //======================================================== //--- private functions --- tilt[][] alter = { {}, {}, {new tilt(0,0,0)}, {new tilt(0,-1,0),new tilt(1,0,1), new tilt(0,1,2),new tilt(-1,0,3),}, {new tilt(0,-1,0),new tilt(1,0,1), new tilt(0,1,2),new tilt(-1,0,3),new tilt(0,0,0),}, {new tilt(0,-1,0),new tilt(0,-2,0), new tilt(1,0,1),new tilt(2,0,1), new tilt(0,1,2),new tilt(0,2,2), new tilt(-1,0,3),new tilt(-2,0,3),}, {new tilt(1,-1,0),new tilt(0,-2,0), new tilt(1,1,1),new tilt(2,0,1), new tilt(-1,1,2),new tilt(0,2,2), new tilt(-1,-1,3),new tilt(-2,0,3),new tilt(0,0,0),}, {new tilt(1,-1,0),new tilt(0,-2,0),new tilt(0,-1,0), new tilt(1,1,1),new tilt(2,0,1),new tilt(1,0,1), new tilt(-1,1,2),new tilt(0,2,2),new tilt(0,1,2), new tilt(-1,-1,3),new tilt(-2,0,3),new tilt(-1,0,3),}, {new tilt(1,-1,0),new tilt(1,-2,0),new tilt(2,-1,0), new tilt(1,1,1),new tilt(1,2,1),new tilt(2,1,1), new tilt(-1,1,2),new tilt(-1,2,2),new tilt(-2,1,2), new tilt(-1,-1,3),new tilt(-2,-1,3),new tilt(-1,-2,3),}, }; void flip(int x,int y){ boolean more = true; tile temp = new tile(); tilt[] tp; goFlip = true; tp = alter[size]; for (int i=0;i=size) continue; int dy = y + tp[i].y; if (dy<0 || dy>=size) continue; map[dx][dy].flip(tp[i].dir); } } void animateFlip(){ goFlip = false; for (int i=0;i