import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.applet.*; public class slider extends Applet implements ActionListener, Runnable { Thread engine = null; Image flourescence; piece pieces[][]; MediaTracker happenings; int cols = 4, rows = 4; int imgHeight, imgWidth; int height, width; int shuffle; int moveCounter = 0; //HIT ME! Button playAgain = new Button("Hit me!"); boolean donep = false; boolean full_repaint = true; Image offImage = null; Graphics offGraphics = null; public void init() { setLayout(new BorderLayout()); playAgain.addActionListener(this); happenings = new MediaTracker(this); Dimension dim = this.size(); offImage = createImage(dim.width,dim.height); offGraphics = offImage.getGraphics(); // find the img String str = getParameter("img"); //set default image if none is instantiated if (str == null) str = "flourescence.jpg"; flourescence = getImage(getCodeBase(), str); // i'm watching you! happenings.addImage(flourescence,0); str = getParameter("rows"); if (str != null) rows = Integer.valueOf(str).intValue(); str = getParameter("cols"); if (str != null) cols = Integer.valueOf(str).intValue(); str = getParameter("shuffle"); if (str != null) shuffle = Integer.valueOf(str).intValue(); else shuffle = 2*cols*rows; this.startItUp(); moveCounter = 0; Panel pnl = new Panel(); pnl.setBackground(Color.black); pnl.add(playAgain); add("South", pnl); } public void startItUp() { // don't do anything until the pic shows up right try { happenings.waitForID(0); } //exception handling...pretty slick, i think :) catch (InterruptedException e) { return; } imgHeight = flourescence.getHeight(this); imgWidth = flourescence.getWidth(this); pieces = new piece[cols][rows]; // if flourescence not on screen yet imgHeight = imgHeight - (imgHeight % rows); imgWidth = imgWidth - (imgWidth % cols); height = imgHeight / rows; width = imgWidth / cols; // build the puzzle ImageFilter sliceImg; ImageProducer prod; Image newbie; for (int r = 0; r < rows; r++) for (int c = 0; c < cols; c++) { sliceImg = new CropImageFilter(c*width, r*height, width, height); prod = new FilteredImageSource(flourescence.getSource(),sliceImg); newbie = createImage(prod); happenings.addImage(newbie,1); pieces[c][r] = new piece(newbie,c,r); } // take away a piece of the puzzle to make 5 pieces so u can shuffle int b_col = cols-1; int b_row = 0; pieces[b_col][b_row] = null; this.shuffleMe(); // make sure pieces are loaded try { happenings.waitForID(1); } catch (InterruptedException e) { return; } donep = false; full_repaint = true; } //shuffle me! public void shuffleMe() { int b_row = 0; int b_col = cols-1; int poss[][] = new int[cols][rows]; int counter; int b_row2 = b_row, b_col2 = b_col; // avoid repeats for(int i = 0; i < shuffle; i++) { counter = 0; if ((b_col+1 < cols) && !((b_col+1 == b_col2) && (b_row == b_row2))) { poss[counter][0] = b_col+1; poss[counter][1] = b_row; counter++; } if ((b_col-1 >= 0) && !((b_col-1 == b_col2) && (b_row == b_row2))) { poss[counter][0] = b_col-1; poss[counter][1] = b_row; counter++; } if ((b_row+1 < rows) && !((b_col == b_col2) && (b_row+1 == b_row2))) { poss[counter][0] = b_col; poss[counter][1] = b_row+1; counter++; } if ((b_row-1 >= 0) && !((b_col == b_col2) && (b_row-1 == b_row2))) { poss[counter][0] = b_col; poss[counter][1] = b_row-1; counter++; } counter = (int)Math.round((counter-1)*Math.random()); pieces[b_col][b_row] = pieces[poss[counter][0]][poss[counter][1]]; b_col2 = b_col; b_row2 = b_row; b_col = poss[counter][0]; b_row = poss[counter][1]; pieces[b_col][b_row] = null; } donep = false; full_repaint = true; } public void paint(Graphics g) { full_repaint = false; if (flourescence == null) return; if (donep) { g.setColor(Color.black); g.drawImage(flourescence,0,0,this); return; } // draw pieces for (int r = 0; r < rows; r++) for (int c = 0; c < cols; c++) if (pieces[c][r] == null) { offGraphics.setColor(Color.black); offGraphics.fillRect(c*width, r*height, width, height); } else offGraphics.drawImage(pieces[c][r].pict,c*width,r*height,this); // draw grid for(int i = 0; i <= cols; i++) offGraphics.drawLine(i*width,0,i*width,imgHeight); for(int i = 0; i <= rows; i++) offGraphics.drawLine(0,i*height,imgWidth,i*height); g.drawImage(offImage,0,0,this); String moveDisplay = "Moves: " + moveCounter; g.drawString(moveDisplay, (imgWidth / 2) - 20, imgHeight + 15); } public void update(Graphics g) { paint(g); } public boolean mouseDown(java.awt.Event evt, int x, int y) { int c = x / width; int r = y / height; if ((c > cols-1) || (r > rows-1)) return true; if (donep) { moveCounter = 0; this.startItUp(); full_repaint = true; return true; } // check for move boolean movep = false; if (pieces[c][r] == null) return true; if ((c < cols-1) && (pieces[c+1][r] == null)) { pieces[c+1][r] = pieces[c][r]; movep = true; } if ((c > 0) && (pieces[c-1][r] == null)) { pieces[c-1][r] = pieces[c][r]; movep = true; } if ((r < rows-1) && (pieces[c][r+1] == null)) { pieces[c][r+1] = pieces[c][r]; movep = true; } if ((r > 0) && (pieces[c][r-1] == null)) { pieces[c][r-1] = pieces[c][r]; movep = true; } if (movep) { pieces[c][r] = null; full_repaint = true; moveCounter++; // check for done donep = true; piece p; for (r = 0; r < rows; r++) for (c = 0; c < cols; c++) { p = pieces[c][r]; if ((p != null) && ((p.col != c) || (p.row != r))) donep = false; } } return true; } public void start() { if (engine == null) { engine = new Thread(this); engine.start(); } } public void stop() { if (engine != null && engine.isAlive()) { engine.stop(); } engine = null; } public void run() { Thread me = Thread.currentThread(); while (engine == me) { try {Thread.currentThread().sleep(100);} catch (InterruptedException e){} if (full_repaint) { repaint(); } } } public void actionPerformed(ActionEvent e) { if("Hit me!".equals(e.getActionCommand())) { donep = false; moveCounter = 0; this.startItUp(); full_repaint = true; } } } class piece { public Image pict; public int col,row; piece(Image p, int c, int r) { pict = p; col = c; row = r; } }