import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;

public class WacharoPazzle1 extends Applet implements MouseListener {

    int x[] = new int[5];
    int y[] = new int[5];
    int xx[] = new int[5];
    int yy[] = new int[5];
    int pnlWidth,pnlHeight,nullx,nully;
    int a,b,s,t,k,m,n;
    int num[][]=new int[5][5];
    boolean flagX,flagY,p;
    Image img;
    Graphics2D offGr;
    Shape rect;
    Stroke st;
    Image wacharoImg;
    
    int imgX[][] = new int[5][5];
    int imgY[][] = new int[5][5];
    int offimgX[][] = new int[5][5];
    int offimgY[][] = new int[5][5];
    
    public void init() {
        setSize(300,300);
        pnlWidth = getWidth()/3;
        pnlHeight = getHeight()/3;
        for(int i=1;i<4;i++) {
            x[i] = pnlWidth*(i-1);
            y[i] = pnlHeight*(i-1);
        }
        for(int i=1;i<4;i++) {
            for(int j=1;j<4;j++) {
                num[j][i]=k+1;
                k++;
                imgX[i][j] = pnlWidth*(i-1);
                imgY[i][j] = pnlHeight*(j-1);
                offimgX[i][j] = pnlWidth*(i-1);
                offimgY[i][j] = pnlHeight*(j-1);
            }
        }
        img = createImage(300,300);
        offGr = (Graphics2D)img.getGraphics();
        wacharoImg = getImage(getDocumentBase(),"WacharoImageMovie1.gif");
        addMouseListener(this);
    }
    
    public void update(Graphics g) {
        paint(g);
    }
    public void paint(Graphics Gr) {
        offGr.clearRect(0,0,300,300);
        
        for(int i=1;i<4;i++) {
            for(int j=1;j<4;j++) {
                if(num[i][j]!=9) {
                    if(num[i][j]==num[s][t]&&(flagX||flagY)) {
                        if(p) {
                            if(flagX) {x[s]+=++m*5;imgX[s][t]+=5;}
                            if(flagY) {y[t]+=++n*5;imgY[s][t]+=5;}
                        } else {
                            if(flagX) {x[s]+=--m*5;imgX[s][t]-=5;}
                            if(flagY) {y[t]+=--n*5;imgY[s][t]-=5;}
                        }
                    }
                    
                    offGr.drawImage(wacharoImg,imgX[i][j],imgY[i][j],imgX[i][j]+pnlWidth,imgY[i][j]+pnlHeight,offimgX[i][j],offimgY[i][j],offimgX[i][j]+pnlWidth,offimgY[i][j]+pnlHeight,this);
                    
                    rect = new Rectangle2D.Double(x[i],y[j],pnlWidth,pnlHeight);
                    st = offGr.getStroke();
                    offGr.setColor(Color.black);
                    offGr.fill(st.createStrokedShape(rect));
                    
           //         offGr.drawString(new Integer(num[i][j]).toString(),imgX[i][j]+40,imgY[i][j]+40);
                    
                    if(flagX&&x[s]==nullx) {
                        flagX=false;
                        x[s]-=m*5;
                        nullx=x[s];
                        Change.change(num,s,j,a,b);
                        Change.change(imgX,s,j,a,b);
                        Change.change(offimgX,s,j,a,b);
                        Change.change(imgY,i,t,a,b);
                        Change.change(offimgY,i,t,a,b);
                        m=0;
                    }
                    if(flagY&&y[t]==nully) {
                        flagY=false;
                        y[t]-=n*5;
                        nully=y[t];
                        Change.change(num,i,t,a,b);
                        Change.change(imgX,s,j,a,b);
                        Change.change(offimgX,s,j,a,b);
                        Change.change(imgY,i,t,a,b);
                        Change.change(offimgY,i,t,a,b);
                        n=0;
                    }
                    
                    if(num[i][j]==num[s][t]&&(flagX||flagY)) {x[s]-=m*5;y[t]-=n*5;}
                    
                }
                if(num[i][j]==9) {
                    nullx=x[i];
                    nully=y[j];
                    a=i;b=j;
                }
            }
        }
        
        Gr.drawImage(img,0,0,this);
    }
    
    public void mouseClicked(MouseEvent e) {
    }
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }
    public void mousePressed(MouseEvent e) {
        int xx=0,yy=0;
        xx=e.getX();
        yy=e.getY();
        if(!flagX && !flagY) {
            if(xx<nullx || xx>nullx+pnlWidth || yy<nully || yy>nully+pnlHeight) {
                for(int i=1;i<4;i++) {
                    for(int j=1;j<4;j++) {
                        if(xx>x[i] && xx<x[i]+pnlWidth && yy>y[j] && yy<y[j]+pnlHeight) {
                            if(num[i+1][j]==9||num[i-1][j]==9||num[i][j+1]==9||num[i][j-1]==9) {
                                if(a!=i) {flagX=true;s=i;t=j;}
                                if(b!=j) {flagY=true;s=i;t=j;}
                                if(num[i+1][j]==9||num[i][j+1]==9) {
                                    p=true;
                                } else {
                                    p=false;
                                }
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
    public void mouseReleased(MouseEvent e) {
    }
}
		

public class Change {

    
    public static void change(int[] num, int a, int b) {
        int temp;
        temp = num[a];
        num[a] = num[b];
        num[b] = temp;
    }

    public static void change(int[][] num, int a, int b, int c, int d) {
        // TODO 自動生成されたメソッド・スタブ
        int temp;
        temp = num[a][b];
        num[a][b] = num[c][d];
        num[c][d] = temp;
    }

}
		

top

4189