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

public class Pazzle3 extends Applet implements MouseListener,Runnable{
    int x[] = new int[5];
    int y[] = new int[5];
    int pnlWidth,pnlHeight,nullx,nully;
    int a,b,s,t,zz,k,m,n,v,w,z,aa,bb=150;
    int num[][]=new int[5][5];
    int speed = 1;
    boolean flagX,flagY,p;
    Color clr[] = {Color.red,Color.green,Color.blue,Color.pink,Color.magenta,Color.orange,Color.gray,Color.cyan,Color.white};
    Image img;
    Graphics2D offGr;
    Thread th;
    Area areaRect,area1,area2,area3,area4;
    Shape shp1,shp2,shp3,shp4,rect;
    
    public void init() {
        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++;
            }
        }
        img = createImage(300,300);
        offGr = (Graphics2D)img.getGraphics();
        addMouseListener(this);
    }

    public void start() {
        if(th==null) {
            th = new Thread(this);
            th.start();
        }
    }

    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;
                            if(flagY) y[t]+=++n;
                        } else {
                            if(flagX) x[s]+=--m;
                            if(flagY) y[t]+=--n;
                        }
                    }
                    
                    offGr.setColor(clr[num[i][j]-1]);
                    rect = new Rectangle2D.Double(x[i],y[j],pnlWidth,pnlHeight);
                    areaRect = new Area(rect);
                    offGr.fill(rect);
                    offGr.setColor(Color.black);
                    offGr.setFont(new Font("Serif",Font.ITALIC,50));
                    offGr.drawString(Integer.toString(num[i][j]),x[i]+pnlWidth/3,y[j]+pnlHeight*2/3);
                    if(flagX&&x[s]==nullx) {
                        flagX=false;
                        x[s]-=m;
                        nullx=x[s];
                        Change.change(num,s,j,a,b);
                        m=0;
                    }
                    if(flagY&&y[t]==nully) {
                        flagY=false;
                        y[t]-=n;
                        nully=y[t];
                        Change.change(num,i,t,a,b);
                        n=0;
                    }
                    if(num[i][j]==num[s][t]&&(flagX||flagY)) {x[s]-=m;y[t]-=n;}
                }
                if(num[i][j]==9) {
                    nullx=x[i];
                    nully=y[j];
                    a=i;b=j;
                }
            }
        }
        
        shp1 = new Ellipse2D.Double(150-aa/2,150-aa/2,aa,aa);
        shp2 = new Ellipse2D.Double(160-aa/2,160-aa/2,aa-20,aa-20);
        area1 = new Area(shp1);
        area2 = new Area(shp2);
        area1.exclusiveOr(area2);
        if(aa>=300) aa=0;
        shp3 = new Ellipse2D.Double(150-bb/2,150-bb/2,bb,bb);
        shp4 = new Ellipse2D.Double(160-bb/2,160-bb/2,bb-20,bb-20);
        area3 = new Area(shp3);
        area4 = new Area(shp4);
        area3.exclusiveOr(area4);
        if(bb>=300) bb=0;
        
        offGr.setPaint(Color.yellow);
        offGr.fill(area1);
        offGr.fill(area3);
        
        aa += 3;
        bb += 3;
        
        Gr.drawImage(img,0,0,this);
        }
        
        public void run() {
            while(th==Thread.currentThread()) {
                try {
                    Thread.sleep(speed);
                } catch (InterruptedException e) {
            }
            repaint();
        }
    }
    
    public void mouseClicked(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 mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }
    public void mousePressed(MouseEvent e) {
    }
    public void mouseReleased(MouseEvent e) {
    }
}
		

Change.java
public class Change {
    public static void change(int num[][],int a,int b,int c,int d) {
        int temp;
        temp = num[a][b];
        num[a][b] = num[c][d];
        num[c][d] = temp;
    }
}
		

top

2744