Pazzle2.java

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

public class Pazzle2 extends Applet implements MouseListener,Runnable{
    int x[] = new int[5];
    int y[] = new int[5];
    int pnlWidth,pnlHeight;
    int a,b,s,t,zz,k,m,n,v,w,z;
    int num[][]=new int[5][5];
    int nullx,nully;
    int speed = 10;
    boolean flagX,flagY,p;
    Color clr[] = {Color.red,Color.green,Color.blue,Color.pink,Color.yellow,Color.orange,Color.gray,Color.cyan,Color.white};
    Image img;
    Graphics offGr;
    Thread th;
    
    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 = 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]);
                    offGr.fillRect(x[i],y[j],pnlWidth,pnlHeight);
                    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];
                        zz=num[s][j];
                        num[s][j]=num[a][b];
                        num[a][b]=zz;
                        m=0;
                    }
                    if(flagY&&y[t]==nully) {
                        flagY=false;
                        y[t]-=n;
                        nully=y[t];
                        zz=num[i][t];
                        num[i][t]=num[a][b];
                        num[a][b]=zz;
                        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;
                }
            }
        }
        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(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;
                        }
                    }
                }
            }
        }
        repaint();
    }
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }
    public void mousePressed(MouseEvent e) {
    }
    public void mouseReleased(MouseEvent e) {
    }
}
		
		

top

3180