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

public class Pazzle6 extends Applet implements MouseListener,Runnable,ActionListener{

    private boolean flagX, flagY, p,btnFlag;
    private int size, pnlSize, nullx, nully, width, height;
    private int a, b, k, m, n, s, t, aa, bb;
    private int[] x, y;
    private int[][] cirX, cirY, num;
    
    private Area areaRect, area1, area2;
    private Stroke st;
    private Shape rect;
    private Image img;
    private Thread th;
    private Graphics2D offGr;
    private Button btn;
    
    private final int speed = 10;
    private final int speedPanel = 10;
    private final int block = 5;
    private final int speedCircle = 1;
    private final int wide = 20;
    
    public void init() {
    
        setSize(300,400);
        setLayout(null);
		
        btn = new Button("スタート");
        btn.setBounds(120,350,60,30);
        btn.addActionListener(this);
        add(btn);
        
        width = getWidth();
        height = getHeight();
        if(width > height) {
            size = height;
        } else {
            size = width;
        }
        if(size/block%speedPanel != 0) {
            System.out.println("画面サイズをパネル数で割った値がパネル移動量で割り切れるように設定して下さい");
            throw new RuntimeException("画面サイズとパネル数が一致しません!!");
        }
        bb = size/2;
        x = new int[block+1];
        y = new int[block+1];
        cirX = new int[block+1][block+1];
        cirY = new int[block+1][block+1];
        num = new int[block+2][block+2];
        pnlSize = size/block;
        for(int i=1;i<block+1;i++) {
            x[i] = pnlSize*(i-1);
            y[i] = pnlSize*(i-1);
        }
        
        img = createImage(size,size);
        offGr = (Graphics2D)img.getGraphics();
        addMouseListener(this);
    }
    
    public void start() {
        if(th==null) {
            th = new Thread(this);
        }
    }
    
    public void update(Graphics g) {
        paint(g);
    }
    
    public void paint(Graphics Gr) {
        offGr.clearRect(0,0,size,size);
        showStatus("スライドパズル");
        for(int i=1;i<block+1;i++) {
            for(int j=1;j<block+1;j++) {
                if(num[i][j]!=block*block) {
                    if(num[i][j]==num[s][t]&&(flagX||flagY)) {
                        if(p) {
                            if(flagX) {x[s]+=++m*speedPanel;cirX[s][t]+=speedPanel;}
                            if(flagY) {y[t]+=++n*speedPanel;cirY[s][t]+=speedPanel;}
                        } else {
                            if(flagX) {x[s]+=--m*speedPanel;cirX[s][t]-=speedPanel;}
                            if(flagY) {y[t]+=--n*speedPanel;cirY[s][t]-=speedPanel;}
                        }
                    }
                    offGr.setColor(Color.gray);
                    rect = new Rectangle2D.Double(x[i],y[j],pnlSize,pnlSize);
                    areaRect = new Area(rect);
                    offGr.fill(areaRect);
                    offGr.setColor(Color.black);
                    st = offGr.getStroke();
                    offGr.fill(st.createStrokedShape(areaRect));
                    
                    area1 = Circle.circle(areaRect,cirX[i][j],cirY[i][j],aa,size);
                    area2 = Circle.circle(areaRect,cirX[i][j],cirY[i][j],bb,size);
                    
                    if(flagX&&x[s]==nullx) {
                        flagX=false;
                        x[s]-=m*speedPanel;
                        nullx=x[s];
                        Change.change(num,s,j,a,b);
                        Change.change(cirX,s,j,a,b);
                        Change.change(cirY,i,t,a,b);
                        m=0;
                    }
                    if(flagY&&y[t]==nully) {
                        flagY=false;
                        y[t]-=n*speedPanel;
                        nully=y[t];
                        Change.change(num,i,t,a,b);
                        Change.change(cirX,s,j,a,b);
                        Change.change(cirY,i,t,a,b);
                        n=0;
                    }
                    
                    if(num[i][j]==num[s][t]&&(flagX||flagY)) {x[s]-=m*speedPanel;y[t]-=n*speedPanel;}
                    
                    if(aa>=size) aa=0;
                    if(bb>=size) bb=0;
                    
                    offGr.setPaint(new GradientPaint(0,0,Color.yellow,size,size,Color.green));
                    offGr.fill(area1);
                    offGr.fill(area2);
                }
                if(num[i][j]==block*block) {
                    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();
            aa += speedCircle;
            bb += speedCircle;
        }
    }
    
    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+pnlSize || yy<nully || yy>nully+pnlSize) {
                for(int i=1;i<block+1;i++) {
                    for(int j=1;j<block+1;j++) {
                        if(xx>x[i] && xx<x[i]+pnlSize && yy>y[j] && yy<y[j]+pnlSize) {
                            if(num[i+1][j]==block*block || num[i-1][j]==block*block || num[i][j+1]==block*block || num[i][j-1]==block*block) {
                                if(a!=i) {flagX=true;s=i;t=j;}
                                if(b!=j) {flagY=true;s=i;t=j;}
                                if(num[i+1][j]==block*block || num[i][j+1]==block*block) {
                                    p=true;
                                } else {
                                    p=false;
                                }
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
    public void mouseReleased(MouseEvent e) {
    }
    
    public void actionPerformed(ActionEvent e) {
        if(e.getSource().equals(btn)) {
            if(!btnFlag) {
                for(int j = 1; j < block+1; j++) {
                    for(int i = 1; i < block+1; i++) {
                        num[i][j] = 1 + k;
                        k++;
                        }
                    }
                th.start();
                btnFlag = true;
            }
        }
    }
}
		

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;
    }
}
		

Circle.java
import java.awt.*;
import java.awt.geom.*;

class Circle {
    
    static Area circle(Area area,int x,int y,int a,int size) {
        final int wide = 20;
        int b = a-wide;
        int aX = x+size/2-a/2;
        int aY = y+size/2-a/2;
        int bX = aX+10;
        int bY = aY+10;
        Shape shp1 = new Ellipse2D.Double(aX,aY,a,a);
        Shape shp2 = new Ellipse2D.Double(bX,bY,b,b);
        Area area1 = new Area(shp1);
        Area area2 = new Area(shp2);
     // area1.exclusiveOr(area2);
        area1.subtract(area2);
        area1.intersect(area);
        return area1;
    }
}
		

top

2973