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

/**
 * @author ワチャロー
 *
 * この生成されたコメントの挿入されるテンプレートを変更するため
 * ウィンドウ > 設定 > Java > コード生成 > コードとコメント
 */
public class Pazzle10 extends Applet implements Runnable, MouseListener ,ActionListener,ItemListener {

    private boolean flagX, flagY, plusFlag,btnFlag,endFlag;
    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,start;
    private int startX,count;
    private long seed;

    private Area areaRect, area1, area2;
    private Stroke st;
    private Shape rect;
    private Image img;
    private Thread th;
    private Graphics2D offGr;
    private Button btn1,btn2;
    private Checkbox cb1,cb2;
    private CheckboxGroup cbg;
    private ArrayList array,arrayCopy;
    private Integer integer,in;
    private RandomPanel ran;
    private Check chk;
    private String strS,strL;
    private GradientPaint gradient;

    private final int speed = 10;
    private final int speedPanel = 10;
    private int block = 3;
    private final int speedCircle = 1;

    /* (非 Javadoc)
     * @see java.lang.Runnable#run()
     */
    public void init() {
        setSize(300,400);
        setLayout(null);
        setBackground(Color.LIGHT_GRAY);
        
        btn1 = new Button("スタート");
        btn1.setBounds(60,350,60,30);
        btn1.addActionListener(this);
        add(btn1);
        
        btn2 = new Button("リセット");
        btn2.setBounds(180,350,60,30);
        btn2.addActionListener(this);
        add(btn2);
        
        cbg = new CheckboxGroup();
        
        cb1 = new Checkbox("3×3", cbg, true);
        cb2 = new Checkbox("5×5", cbg, false);
        
        cb1.setBounds(60,320,60,30);
        cb2.setBounds(180,320,60,30);
        
        cb1.addItemListener(this);
        cb2.addItemListener(this);
        
        add(cb1);
        add(cb2);
                
        width = getWidth();
        height = getHeight();
        if (width > height) {
            size = height;
        } else {
            size = width;
        }

        clear();
    }
    
    public void clear() {
        flagX = false;
        flagY = false;
        plusFlag=false;
        a=0; b=0; k=0; m=0; n=0; s=0; t=0; aa=0; bb=0;
        
        pnlSize = size / block;
        if (pnlSize % 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];
        start = new int[block + 2][block + 2];
        for (int i = 1; i < block+1; i++) {
            x[i] = (i - 1) * pnlSize;
            y[i] = (i - 1) * pnlSize;
        }
        gradient = new GradientPaint(0,0,Color.yellow,size,size,Color.green);
        img = createImage(size, size);
        offGr = (Graphics2D) img.getGraphics();
        addMouseListener(this);
    }
    
    public void start() {
        if (th == null) {
            th = new Thread(this);
        }
    }
    
    public void stop() {
        if(th!=null) {
            th=null;
        }
        img.flush();
    }
    
    public void update(Graphics g) {
        paint(g);
    }
    public void paint(Graphics Gr) {
        offGr.setColor(Color.DARK_GRAY);
        offGr.fillRect(0,0,size,size);
        showStatus("スライドパズル");
        if(!endFlag) {
            for(int j=1;j<block+1;j++) {
                for(int i=1;i<block+1;i++) {
                    if(num[i][j]!=block*block) {
                        if(num[i][j]==num[s][t]&&(flagX||flagY)) {
                            if(plusFlag) {
                                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);
                        st = offGr.getStroke();
                        areaRect = new Area(rect);
                        offGr.fill(areaRect);
                        
                        offGr.setColor(Color.black);
                        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(gradient);
                        offGr.fill(area1);
                        offGr.fill(area2);
                    }
                    
                    if(num[i][j]==block*block) {
                        nullx=x[i];
                        nully=y[j];
                        a=i;b=j;
                    }
                }
            }
        }
        if(endFlag) {
            aa += speedCircle*2;
            bb += speedCircle*2;
            offGr.setPaint(gradient);
            Circle.wholeCircle(offGr,aa,size);
            Circle.wholeCircle(offGr,bb,size);
        }
        if(aa>=size) aa=0;
        if(bb>=size) bb=0;
        Gr.drawImage(img,0,0,this);
        Gr.drawLine(0,300,300,300);
    }
        
    public void draw() {
        
    }
        
    public void run() {
        // TODO 自動生成されたメソッド・スタブ
        while(th == Thread.currentThread()) {
            try {
                Thread.sleep(speed);
            } catch (InterruptedException e) {
                e.printStackTrace();
                System.out.println("パズルが割り込まれた。");
            }
            
            if(!flagX&&!flagY&&chk.currentCheck(num,block)) {
                endFlag=true;
                btnFlag=true;
            //    th=null;
            }
            
            repaint();
            aa += speedCircle;
            bb += speedCircle;
        }
    }

    /* (非 Javadoc)
     * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
     */
    public void mouseClicked(MouseEvent e) {
        // TODO 自動生成されたメソッド・スタブ
    }

    /* (非 Javadoc)
     * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
     */
    public void mouseEntered(MouseEvent e) {
        // TODO 自動生成されたメソッド・スタブ

    }

    /* (非 Javadoc)
     * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
     */
    public void mouseExited(MouseEvent e) {
        // TODO 自動生成されたメソッド・スタブ

    }

    /* (非 Javadoc)
     * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
     */
    public void mousePressed(MouseEvent e) {
        // TODO 自動生成されたメソッド・スタブ
        int xx = 0, yy = 0;
        xx = e.getX();
        yy = e.getY();
        if (!flagX && !flagY && !endFlag) {
            if (xx<nullx || xx>nullx+pnlSize || yy<nully || yy>nully+pnlSize) {
                for (int j = 1; j < block + 1; j++) {
                    for (int i = 1; i < block + 1; i++) {
                        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) {
                                    plusFlag = true;
                                } else {
                                    plusFlag = false;
                                }
                                break;
                            }
                        }
                    }
                }
            }
        }
    }

    /* (非 Javadoc)
     * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
     */
    public void mouseReleased(MouseEvent e) {
        // TODO 自動生成されたメソッド・スタブ

    }
    
    public void actionPerformed(ActionEvent e) {
        boolean bool = false;
        int kk=0;
        if(e.getSource().equals(btn1) && !btnFlag) {
            while(!bool) {
                showStatus("しばらくお待ちください。");
                ran = new RandomPanel();
                ran.setSeed(++seed + (new Date()).getTime());
                chk = new Check(block);
                array = ran.random(block * block -1);
                
                arrayCopy = (ArrayList)array.clone();
                strS = chk.toString(arrayCopy,block);
                
                count = chk.sort(arrayCopy,block);
                
                strL = chk.toString(arrayCopy,block);
                if(count%2==0 && strL.equals(chk.getLast())) {
                    bool=true;
                }
                chk.OK_FLAG = true;
                System.out.println(count);
                System.out.println(strS);
                System.out.println(strL);
        //        bool = chk.check(arrayCopy,block);
            }
            
            for(int j=1;j<block+1;j++) {
                for(int i=1;i<block+1;i++) {
                    if(kk!=block*block-1) {
                        num[i][j] = ((Integer)array.get(kk++)).intValue();
                    } else {
                        num[i][j] = block*block;
                    }
                    System.out.println(num[i][j]);
                }
            }
            
            startX = 0;
            for (int j = 1; j < block + 1; j++) {
                for (int i = 1; i < block + 1; i++) {
                    k++;
                    if (i != block || j != block) {
                        integer = (Integer) array.get(k - 1);
                        start[i][j] = integer.intValue();

                        for (int z = 0; z < block; z++) {
                            if (start[i][j] > block * z
                                && start[i][j] < block * (z + 1) + 1) {
                                startX = start[i][j] - block * z;
                            }
                        }

                        cirX[i][j] = (k - startX - (j - 1) * block) * pnlSize;
                                    
                        cirY[i][j] = ((k - 1) / block
                        - (start[i][j] - 1) / block)
                        * pnlSize;
                                    
                    }
                //    num[i][j] = k;
                }
            }
            if(th == null) {
                th = new Thread(this);
            }
            
            btnFlag = true;
            th.start();
            
        }
        
        if(e.getSource().equals(btn2) && btnFlag) {
            btnFlag = false;
            endFlag = false;
            clear();
            repaint();
            th = null;
        }
    }
    
    public void itemStateChanged(ItemEvent e) {
        if(cb1 == e.getSource() && !btnFlag) {
            block = 3;
            clear();
            repaint();
        }
        
        if(cb2==e.getSource() && !btnFlag) {
            block = 5;
            clear();
            repaint();
        }
        
        if(cb1 == e.getSource() && btnFlag) {
            if(block == 5) {
                cb2.setState(true); 
            }
        }
        
        if(cb2 == e.getSource() && btnFlag) {
            if(block == 3) {
                cb1.setState(true); 
            }
        }
    }
}
		

top

4001