⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hannoitowerpanel.java

📁 自己写的汉诺塔java程序。通过调用ShowHanoiFrame开启主界面及可运行程序。软件语言java
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        autoHanoiDemo(numDisks, towerNames[0], towerNames[2], towerNames[1]);
        if(!stop)
            JOptionPane.showConfirmDialog(HannoiTowerPanel.this, "成功完成汉诺塔自动搬运演示!", "完成提示",
                          JOptionPane.CLOSED_OPTION,JOptionPane.INFORMATION_MESSAGE);
    }
    //得到指定的顶上盘子
    public Disk getTopDisk(char towerName) {
        Disk disk = null;
        if (towerName == towerNames[0]) {
            for (int i = 0; i < numDisks; i++) {
                if (point[i].isExsitDisk() == true) {
                    disk = point[i].getDisk();
                     return disk;
                }
            }
        }
        if (towerName == towerNames[1]) {
            for (int i = numDisks; i < 2 * numDisks; i++) {
                if (point[i].isExsitDisk() == true) {
                    disk = point[i].getDisk();
                     return disk;
                }
            }
        }
        if (towerName == towerNames[2]) {
            for (int i = 2 * numDisks; i < 3 * numDisks; i++) {
                if (point[i].isExsitDisk() == true) {
                    disk = point[i].getDisk();
                    break;
                }
            }
        }
        return disk;
    }

    //获取指定塔中顶上盘子的位置和上方位置
    public int[] getDiskPosition(char towerName) {
        //position[0]塔中最上面盘子的上方位置
        //position[1]塔中最上面盘子的位置
        int[] position = new int[2];
        if (towerName == towerNames[0]) {
            int i = 0;
            for (i = 0; i < numDisks; i++) {
                if (point[i].isExsitDisk() == true) {
                    position[0] = Math.max(i - 1, 0);
                    position[1]=i;
                    break;
                }
            }
            if (i == numDisks) {
                position[0] = numDisks - 1;
                position[1] = numDisks - 1;
            }
            return position;
        }
        if (towerName == towerNames[1]) {
            int i = 0;
            for (i = numDisks; i < 2 * numDisks; i++) {
                if (point[i].isExsitDisk() == true) {
                    position[0] = Math.max(i - 1, 0);
                    position[1]=i;
                    break;
                }
            }
            if (i == 2 * numDisks) {
                position[0] = 2 * numDisks - 1;
                position[1] = 2*numDisks - 1;
            }
            return position;
        }
        if (towerName == towerNames[2]) {
            int i = 0;
            for (i = 2 * numDisks; i < 3 * numDisks; i++) {
                if (point[i].isExsitDisk() == true) {
                    position[0] = Math.max(i - 1, 0);
                    position[1]=i;
                    break;
                }
            }
            if (i == 3 * numDisks) {
                position[0] = 3 * numDisks - 1;
                position[1] = 3*numDisks - 1;
            }
        }

        return position;
    }

    //添加鼠标事件,按下鼠标
    public void mousePressed(MouseEvent e) {
        if(auto) return;
        Disk disk = null;
        Rectangle rect = null;
        if (e.getSource() == this)
            move = false;
        if (move == false)
            if (e.getSource() instanceof Disk) {
                disk = (Disk) e.getSource();
                startX = disk.getBounds().x;
                startY = disk.getBounds().y;
                rect = disk.getBounds();
                for (int i = 0; i < 3 * numDisks; i++) {
                    int x = point[i].getX();
                    int y = point[i].getY();
                    if (rect.contains(x, y)) {
                        startI = i;
                        break;
                    }
                }
            }
    }

    //添加鼠标事件,按下拖动鼠标
    public void mouseDragged(MouseEvent e) {
        if(auto) return;
        Disk disk = null;
        if (e.getSource() instanceof Disk) {
            disk = (Disk) e.getSource();
            move = true;
            e = SwingUtilities.convertMouseEvent(disk, e, this);
        }

        if (e.getSource() == this) {
            if (move && disk != null) {

                x = e.getX();
                y = e.getY();
                if (disk.getUnderDisk() == false) {
                    disk.setLocation(x - disk.getWidth() / 2,
                                     y - disk.getHeight() / 2);
                }
                validate();
            }
        }
    }

    //添加鼠标事件,松开鼠标
    public void mouseReleased(MouseEvent e) {
        if(auto) return;

        Disk disk = null;
        move = false;
        Rectangle rect = null;
        if (e.getSource() instanceof Disk) {
            disk = (Disk) e.getSource();
            rect = disk.getBounds();
            e = SwingUtilities.convertMouseEvent(disk, e, this);
        }

        if (e.getSource() == this) {
            boolean containTowerPoint = false;
            int x = 0, y = 0;
            int endI = 0;
            if (disk != null) {
                for (int i = 0; i < 3 * numDisks; i++) {
                    x = point[i].getX();
                    y = point[i].getY();
                    if (rect.contains(x, y)) {
                        containTowerPoint = true;
                        endI = i;
                        break;
                    }
                }
            }
            if (disk != null && containTowerPoint) {
                if (point[endI].isExsitDisk() == true) {
                    disk.setLocation(startX, startY);
                } else {

                    if (endI == numDisks - 1 || endI == 2 * numDisks - 1 ||
                        endI == 3 * numDisks - 1) {
                        point[endI].placeDisk(disk, this);

                        if (startI != numDisks - 1 && startI != 2 * numDisks - 1 &&
                            startI != 3 * numDisks - 1) {
                            (point[startI + 1].getDisk()).setUnderDisk(false);
                            point[startI].setExsitDisk(false);
                        } else {
                            point[startI].setExsitDisk(false);
                        }
                    } else {
                        if (point[endI + 1].isExsitDisk() == true) {
                            Disk tempDisk = point[endI + 1].getDisk();
                            if ((tempDisk.getNumber() - disk.getNumber()) >= 1) {
                                point[endI].placeDisk(disk, this);
                                if (startI != numDisks - 1 &&
                                    startI != 2 * numDisks - 1 &&
                                    startI != 3 * numDisks - 1) {
                                    (point[startI + 1].getDisk()).setUnderDisk(false);
                                    point[startI].setExsitDisk(false);
                                    tempDisk.setUnderDisk(true);
                                } else {
                                    point[startI].setExsitDisk(false);
                                    tempDisk.setUnderDisk(true);
                                }
                            } else {
                                disk.setLocation(startX, startY);
                            }
                        } else {
                            disk.setLocation(startX, startY);
                        }
                    }
                }
            }

            if (disk != null && !containTowerPoint) {
                disk.setLocation(startX, startY);
            }
        }

        //判断手动搬运时,是否完成
        if(!point[numDisks-1].isExsitDisk()&&!point[3 * numDisks-1].isExsitDisk()){
            JOptionPane.showConfirmDialog(HannoiTowerPanel.this, "汉诺塔手动搬运演示成功!", "完成提示",
                          JOptionPane.CLOSED_OPTION,JOptionPane.INFORMATION_MESSAGE);
        }
    }

    public void mouseMoved(MouseEvent e) {
    }
    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void setMyThreadStop(){
        this.stop=true;
    }

    public Thread getMyThread(){
        return this.myThread;
    }

    public void setThreadState(int threadState){
        this.threadState=threadState;
    }

    public int getThreadState(){
        return this.threadState;
    }

    public void setPause(boolean pause){
        this.pause=pause;
    }

    public boolean getPause(){
        return this.pause;
    }

    public void setNumDisks(int numDisks){
        this.numDisks=numDisks;
    }

    public int getNumDisks(){
        return this.numDisks;
    }

    public void setSpeed(int speed){
        this.speed=speed;
    }

    public int getSpeed(){
        return this.speed;
    }

    public void setLimitState(int limitState){
        this.limitState=limitState;
    }

    public int getLimitState(){
        return this.limitState;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -