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

📄 hanoiframe.java

📁 本软件是使用java 开发的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package datastructure;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;

public class HanoiFrame
    extends AnimationFrame
    implements ActionListener, Runnable {
  // 控制按钮
  JButton newButton;
  JButton runButton;
  JButton stepButton;
  JButton countButton;
  JButton helpButton;
  JButton closeButton;
  private boolean flag1 = true;
  private JButton musicButton = new JButton("播放音乐");

  int flag = 0;

  hnData data;
  // 显示移动次数的文本框
  JTextField counterText;
  //控制演示速度
  private GlobalControls controlsPanel = new GlobalControls();
  private int controls;

  JPanel animPanel;
  JPanel centerPanel;
  JLayeredPane Panel1;
  String as[] = {
      "void TOH(int n, Tower from, Tower to, Tower help) {",
      "   if (n==0) return; ", "   if (n > 1) TOH(n-1, from, help, to);",
      "   move(from, to);", "   if (n > 1) TOH(n-1, help, to, from);", "}"};
  HanoiCodeAnimationPanel codePanel = new HanoiCodeAnimationPanel(as);
  ///
  JPanel panel2;
  Thread towThr;
  Thread curThre;
  JLabel msgLabel;
  JLabel towers[];
  JLabel towBases[];
  JLabel disks[];
  JLabel towTitle[];
  JPanel towPanel;
  Point tBaseLoc;
  int towSpan;
  //   盘子的个数
  int numDisk;
  //   盘子的高度
  int diskH;
  //   盘子的宽度
  int diskW;
  int diskIncW;
  int towerH;
  int towerW;
  int tBaseH;
  int tBaseW;
  int towNum[] = {
      0, 1, 2};
  int mvCount;
  int diskToY[];
  Point towTop[];
  boolean cleared;
  boolean stepped;
  boolean instep;
  boolean toStart;
  boolean toInit;
  boolean gostep;
  boolean suspended;
  TowerStackPanel stackPanel;
  //record用于记录Hanoi的移动步骤
  TextArea record;
  String towName[] = {
      "  A ", "  B ", "  C "};

  public HanoiFrame(Root rootui) {
    super("Hanoi算法演示窗口 (南昌航空工业学院-软件学院)", rootui);
    newButton = new JButton("新 建");
    runButton = new JButton("运 行");
    stepButton = new JButton("单 步");
    countButton = new JButton("移动次数");
    helpButton = new JButton("帮 助");
    closeButton = new JButton("退 出");
    closeButton.setToolTipText("退出演示窗口");
    closeButton.addActionListener(this);
    musicButton.addActionListener(this);
    counterText = new JTextField(3);
    animPanel = new JPanel();
    towThr = null;//new Thread(this, "Tower Thread");
    curThre = null;
    msgLabel = new JLabel();
    towers = new JLabel[3];
    towBases = new JLabel[3];
    towTitle = new JLabel[3];
    tBaseLoc = new Point(80, 120);
    towSpan = 180;
    diskH = 6;
    diskW = 22;
    diskIncW = 8;
    towerH = 80;
    towerW = 2;
    tBaseH = 3;
    tBaseW = 100;
    mvCount = 0;
    diskToY = new int[3];
    towTop = new Point[3];
    cleared = false;
    stepped = false;
    instep = false;
    toStart = true;
    toInit = true;
    gostep = true;
    suspended = false;
    towPanel = new JPanel();
    stackPanel = new TowerStackPanel();
    record = new TextArea("         \n", 8, 20, 1);
    record.append(" 移 动 步 骤 记 录:" + "\n");
    init();
  }
  public void addListener()//加上windows监听器
    {
         addWindowListener(new WindowClose(this, false));
    }

  public void init() {
    super.initBase();
    centerPanel = new JPanel();
    centerPanel = new JPanel();
    centerPanel.setBackground(Color.lightGray);
    centerPanel.setOpaque(true);
    centerPanel.setLayout(new GridLayout(3, 1));
    data = new hnData(this, "建立演示数据对话框", true);
    numDisk = data.desk;
    controlsPanel.delay = 900;
    Panel1 = new JLayeredPane();
    codePanel.setOpaque(false);
    panel2 = new JPanel();
    panel2.setOpaque(false);
    animPanel.setLayout(null);
    super.controlPanel.add(newButton);
    newButton.addActionListener(this);
    super.controlPanel.add(runButton);
    runButton.addActionListener(this);
    super.controlPanel.add(stepButton);
    stepButton.addActionListener(this);
    super.controlPanel.add(helpButton);
    super.controlPanel.add(closeButton);
    super.controlPanel.add(musicButton);
    helpButton.addActionListener(this);
    counterText.setBackground(Color.cyan);
    counterText.setForeground(Color.magenta);
    counterText.setBorder(new EtchedBorder());
    counterText.setEditable(false);
    countButton.setForeground(Color.gray);
    super.controlPanel.add(countButton);
    super.controlPanel.add(counterText);

    for (int i = 0; i < 3; i++) {
      towTop[i] = new Point( ( (tBaseLoc.x + tBaseW / 2) - towerW / 2) +
                            towSpan * i, (tBaseLoc.y - towerH) + 1);
      towers[i] = new JLabel();
      towers[i].setSize(towerW, towerH);
      towers[i].setLocation(towTop[i]);
      towers[i].setBorder(new MatteBorder(1, 1, 1, 1, Color.blue));
      towBases[i] = new JLabel();
      towBases[i].setBackground(new Color(160, 200, 200));
      towBases[i].setSize(tBaseW, tBaseH);
      towBases[i].setLocation(tBaseLoc.x + towSpan * i, tBaseLoc.y);
      towBases[i].setBorder(new MatteBorder(1, 1, 2, 1, Color.blue));
      // 设置组件是否为不透明的
      towBases[i].setOpaque(true);
      towTitle[i] = new JLabel();
      towTitle[i].setSize(20, 20);
      towTitle[i].setForeground(Color.blue);
      towTitle[i].setLocation( ( (tBaseLoc.x + tBaseW / 2) - 2) + towSpan * i,
                              tBaseLoc.y + tBaseH + 2);
      towPanel.add(towers[i]);
      towPanel.add(towBases[i]);
      towPanel.add(towTitle[i]);
    }
    towTitle[0].setText("A");
    towTitle[1].setText("B");
    towTitle[2].setText("C");

    animPanel.setLocation(0, 0);
    animPanel.setDoubleBuffered(true);
    animPanel.setSize(600, 160);
    animPanel.setOpaque(false);
    towPanel.setSize(600, 500);
    towPanel.setOpaque(false);
    towPanel.setLayout(null);
    stackPanel.setSize(260, 160);
    stackPanel.setLocation(30, animPanel.getLocation().y + animPanel.getHeight());

    stackPanel.setBackground(Color.lightGray);
    record.setSize(260, 160);
    record.setLocation(310, animPanel.getLocation().y + animPanel.getHeight());
    record.setBackground(Color.white);
    record.setEditable(false);

    Panel1.add(towPanel, new Integer(1));
    Panel1.add(animPanel, new Integer(2));
    panel2.setLayout(new GridLayout(1, 2));
    panel2.add(stackPanel);
    panel2.add(record);

    centerPanel.add(Panel1);
    centerPanel.add(codePanel);
    centerPanel.add(panel2);
    this.getContentPane().add(centerPanel, BorderLayout.CENTER);

    runButton.setEnabled(false);
    stepButton.setEnabled(false);

    validate();
    animPanel.repaint();
/////////////////////////////////////////////////////
    curThre = Thread.currentThread();
  }

  public void actionPerformed(ActionEvent actionevent) {
    Object obj = actionevent.getSource();

    if (obj == newButton) {
      clear();
     // flag = 0;
      data.setVisible(true);
      numDisk = this.data.desk;
      toInit = true;
     if (toStart)
        myStart();
    }

    if (obj == runButton) {

      instep = false;
      gostep = false;
      if (toStart)
        myStart();
    }

    if (obj == closeButton) {
      Root.clip.stop();
      this.setVisible(false);
    }
    if (obj == stepButton) {

      instep = true;
      gostep = !gostep;
      if (toStart)
        myStart();
    }
    if (obj == helpButton) {
      HanoiFrame _tmp5 = this;
      if (Root.helpFrame != null) {
        HanoiFrame _tmp6 = this;
        Root.helpFrame.dispose();
      }
      HanoiFrame _tmp7 = this;
      Root.helpFrame = new HelpFrame("/help/Hanoi.html", super.root);
      HanoiFrame _tmp8 = this;
      Root.helpFrame.setLocation(200, 100);
      HanoiFrame _tmp9 = this;
      Root.helpFrame.showNewHtmlPage("/help/Hanoi.html");
      HanoiFrame _tmp10 = this;
      Root.helpIsOn = true;
      HanoiFrame _tmp11 = this;
      Root.helpFrame.setVisible(true);
    }
    if (obj == musicButton) {
      if (flag1) {
        Root.clip.loop();
        musicButton.setText("关闭音乐");
        flag1 = !flag1;
      }
      else {
        Root.clip.stop();
        musicButton.setText("播放音乐");
        flag1 = !flag1;
      }
    }

  }

  public void myStart() {
    if (towThr == null)
    {  towThr = new Thread(this, "Tower of Hanoi Animation");
      towThr.setPriority(1);

⌨️ 快捷键说明

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