import java.awt.*; import java.applet.*; import java.util.Random; public class Puzzle extends Applet { int picturewidth; int pictureheight; int xpieces; int ypieces; int piecewidth; int pieceheight; int offscreenwidth; int offscreenheight; Color backgroundcolor; Piece piece[]; Image pictureimage; Image offscreenimage; Graphics offscreen; boolean loaded; boolean pieceisfloating; int cursorxinpiece; int cursoryinpiece; int groupcount; int joincount; public void init() { try{ // イメージローディング loaded = false; pictureimage = getImage(getCodeBase(), getParameter("picture")); // パラメータ読み込み xpieces = Integer.parseInt(getParameter("xpieces")); ypieces = Integer.parseInt(getParameter("ypieces")); offscreenwidth = this.size().width; offscreenheight = this.size().height; backgroundcolor = new Color( Integer.parseInt(getParameter("bgcolorR")), Integer.parseInt(getParameter("bgcolorG")), Integer.parseInt(getParameter("bgcolorB"))); // ピース配列作成 piece = new Piece[xpieces*ypieces]; // オフスクリーンの作成 offscreenimage = createImage(offscreenwidth, offscreenheight); offscreen = offscreenimage.getGraphics(); if(offscreen.drawImage(pictureimage, 0, 0, this) == true) makepieces(); }catch(Exception e){e.printStackTrace(System.out);} } public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { // 読み込み完了しているか確認 if((infoflags & ALLBITS) == ALLBITS) { makepieces(); return false; } else return true; } private void makepieces() { try{ picturewidth = pictureimage.getWidth(null); pictureheight = pictureimage.getHeight(null); piecewidth = picturewidth/xpieces; pieceheight = pictureheight/ypieces; Random rnd = new Random(); for(int i = 0; i < xpieces; i++) for(int j = 0; j < ypieces; j++) { int k = j*xpieces+i; piece[k] = new Piece(); piece[k].group = -1; piece[k].pxpos = i; piece[k].pypos = j; piece[k].left = (int)(rnd.nextDouble()*(offscreenwidth-piecewidth)); piece[k].top = (int)(rnd.nextDouble()*(offscreenheight-pieceheight)); piece[k].image = createImage(piecewidth, pieceheight); piece[k].image.getGraphics().drawImage(pictureimage, -piecewidth*i, -pieceheight*j, null); } pieceisfloating = false; groupcount = 0; joincount = 0; // 読み込み完了フラグon loaded = true; repaint(); }catch(Exception e){e.printStackTrace(System.out);} } public void update(Graphics g) { if(loaded) { // オフスクリーン消去 offscreen.setColor(backgroundcolor); offscreen.fillRect(0, 0, offscreenwidth, offscreenheight); // ピース描画、後ろの番号のピースを先に描く for(int i = xpieces*ypieces-1; i >= 0; i--) { offscreen.drawImage(piece[i].image, piece[i].left, piece[i].top, null); //debug offscreen.setColor(Color.red); //debug offscreen.drawString("("+piece[i].pxpos+","+piece[i].pypos+")", piece[i].left, piece[i].top); //debug offscreen.setColor(Color.white); //debug offscreen.drawString(Integer.toString(i), piece[i].left, piece[i].top+10); //debug offscreen.setColor(Color.yellow); //debug offscreen.drawString(Integer.toString(piece[i].group), piece[i].left, piece[i].top+20); } } paint(g); } public void paint(Graphics g) { g.drawImage(offscreenimage, 0, 0, null); } public boolean mouseDown(Event e, int x, int y) { try{ for(int i = 0; i < xpieces*ypieces; i++) if((piece[i].left < x)&&(x < piece[i].left+piecewidth)&& (piece[i].top < y)&&(y < piece[i].top+pieceheight)) { // 選択ピースを最前面(0番ピース)に移動 Piece tmppiece; tmppiece = piece[i]; for(int j = i; j > 0; j--) piece[j] = piece[j-1]; piece[0] = tmppiece; // ピースフローティングon、カーソル位置保存 pieceisfloating = true; cursorxinpiece = x-piece[0].left; cursoryinpiece = y-piece[0].top; repaint(); break; } }catch(Exception ex){ex.printStackTrace(System.out);} return true; } public boolean mouseDrag(Event e, int x, int y) { if(pieceisfloating) { // フロートピースをカーソル位置に移動 piece[0].left = x-cursorxinpiece; piece[0].top = y-cursoryinpiece; // 他のピースとつながってグループになっている場合、それらも移動 if(piece[0].group != -1) { int ox = piece[0].left-piece[0].pxpos*piecewidth; int oy = piece[0].top-piece[0].pypos*pieceheight; for(int i = 1; i