📄 hanoiframe.java
字号:
toStart = false;
towThr.start();}
}
// 绘制初始图形
public void initAnim() {
newButton.setEnabled(false);
diskToY[0] = tBaseLoc.y;
diskToY[1] = tBaseLoc.y;
diskToY[2] = tBaseLoc.y;
msgLabel.setVisible(false);
cleared = false;
disks = new JLabel[numDisk];
for (int i = numDisk - 1; i >= 0; i--) {
disks[i] = new JLabel();
disks[i].setSize(diskW + i * diskIncW, diskH);
disks[i].setBackground(new Color(160, 140, 60));
disks[i].setBorder(new MatteBorder(1, 1, 1, 1, Color.black));
disks[i].setOpaque(true);
disks[i].setLocation( (towTop[0].x + towerW / 2) -
disks[i].getWidth() / 2, towTop[0].y - towerH);
animPanel.add(disks[i]);
diskToY[0] = diskToY[0] - diskH;
Point point = new Point( (tBaseLoc.x + tBaseW / 2) -
disks[i].getWidth() / 2, diskToY[0]);
moving(disks[i], point, 2);
}
runButton.setEnabled(true);
stepButton.setEnabled(true);
newButton.setEnabled(true);
animPanel.repaint();
}
public void start() {
resume();
}
public void run() {
if (toInit) {
initAnim();
stackPanel.push(" " + numDisk, towName[0], towName[2], towName[towNum[1]],
" ");
toInit = false;
toStart = true;
HanoiFrame _tmp = this;
HanoiFrame _tmp1 = this;
codePanel.highlight(1);
HanoiFrame _tmp2 = this;
codePanel.annotate("//" + numDisk + " disks in total", 2,
Color.pink);
HanoiFrame _tmp3 = this;
codePanel.annotate("// Start from tower " + towName[0], 3,
Color.pink);
HanoiFrame _tmp4 = this;
codePanel.annotate("// to move to tower " + towName[2], 4,
Color.pink);
HanoiFrame _tmp5 = this;
codePanel.annotate("// help tower is " + towName[1], 5,
Color.pink);
}
else {
TOH(numDisk, towNum[0], towNum[2], towNum[1]);
if (!cleared)
stackPanel.pop();
HanoiFrame _tmp6 = this;
HanoiFrame _tmp7 = this;
codePanel.highlight(6);
HanoiFrame _tmp8 = this;
codePanel.annotate("", 3);
HanoiFrame _tmp9 = this;
codePanel.annotate("", 4);
HanoiFrame _tmp10 = this;
codePanel.annotate("", 5);
HanoiFrame _tmp11 = this;
codePanel.annotate("// Moving finished", 2, Color.pink);
HanoiFrame _tmp12 = this;
codePanel.highlight(2);
flag=0;
try {
HanoiFrame _tmp13 = this;
Thread.sleep(1600);
}
catch (InterruptedException interruptedexception) {}
if (flag!= 1) {
JOptionPane.showConfirmDialog(this,
" 算法演示完成!\n \n 单击相关按钮重新开始\n",
"演示完成", JOptionPane.DEFAULT_OPTION);
flag = 1;
}
runButton.setEnabled(false);
stepButton.setEnabled(false);
toInit = true;
}
destroy();
}
public void kill() {
towThr.yield();
towThr = null;
stop();
}
public void Start() {
this.start();
}
public void TOH(int i, int j, int k, int l) {
if (i == 0)
return;
if (cleared)
return;
if (i > 1) {
HanoiFrame _tmp = this;
HanoiFrame _tmp1 = this;
codePanel.highlight(3);
try {
HanoiFrame _tmp2 = this;
Thread.sleep(100);
}
catch (InterruptedException interruptedexception) {}
stackPanel.push(" " + (i - 1), towName[j], towName[l], towName[k], " 3 ");
if (cleared)
return;
TOH(i - 1, j, l, k);
while (gostep)
;
if (cleared)
return;
stackPanel.pop();
}
HanoiFrame _tmp3 = this;
if (codePanel != null) {
HanoiFrame _tmp4 = this;
codePanel.annotate("//" + (tBaseLoc.y - diskToY[j]) / diskH +
" disk(s) on tower " + towName[j], 2,
Color.pink);
HanoiFrame _tmp5 = this;
codePanel.annotate("// Top disk from " + towName[j], 3,
Color.pink);
HanoiFrame _tmp6 = this;
codePanel.annotate("// to tower " + towName[k], 4, Color.pink);
HanoiFrame _tmp7 = this;
codePanel.annotate("// help tower is " + towName[l], 5,
Color.pink);
HanoiFrame _tmp8 = this;
codePanel.highlight(4);
}
if (cleared)
return;
towMove(disks[i - 1], j, k);
counterText.setText(Integer.toString(++mvCount));
if (i > 1) {
HanoiFrame _tmp9 = this;
if (codePanel != null) {
HanoiFrame _tmp10 = this;
codePanel.highlight(5);
try {
HanoiFrame _tmp11 = this;
Thread.sleep(100);
}
catch (InterruptedException interruptedexception1) {}
}
stackPanel.push(" " + (i - 1), towName[l], towName[k], towName[j], " 5 ");
if (cleared)
return;
TOH(i - 1, l, k, j);
while (gostep)
;
if (cleared)
return;
stackPanel.pop();
}
HanoiFrame _tmp12 = this;
HanoiFrame _tmp13 = this;
codePanel.highlight(6);
}
public void towMove(JLabel jlabel, int i, int j) {
Point point = new Point(jlabel.getLocation().x, towTop[i].y - 2 * diskH);
Point point1 = new Point( (towTop[j].x + towerW / 2) -
jlabel.getWidth() / 2, towTop[j].y - 2 * diskH);
Point point2 = new Point( (towTop[j].x + towerW / 2) -
jlabel.getWidth() / 2, diskToY[j] - diskH);
while (gostep)
;
gostep = true & instep;
setRecord(towName[i], towName[j]);
moving(jlabel, point, 2);
diskToY[i] += diskH;
moving(jlabel, point1, 2);
moving(jlabel, point2, 2);
diskToY[j] = diskToY[j] - diskH;
}
public void moving(JLabel jlabel, Point point, int i) {
if (disks == null)
return;
Point point1 = jlabel.getLocation();
int i2 = point1.x;
int j2 = point1.y;
int j = point1.x - point.x;
int k = point.y - point1.y;
int l = Math.abs(j) <= Math.abs(k) ? Math.abs(k) : Math.abs(j);
int l1 = i;
int i1 = l / l1;
if (i1 < 1)
l1 = 1;
int k1 = 0;
for (curThre = Thread.currentThread(); towThr == curThre && k1 < i1; ) {
if (cleared)
return;
if (k1 < i1 - 1) {
jlabel.setLocation(i2 - (j * k1) / i1, j2 + (k * k1) / i1);
animPanel.repaint();
try {
HanoiFrame _tmp = this;
controls = controlsPanel.delay /1000;
Thread.sleep(controls);
synchronized (this) {
while (suspended)
wait();
}
}
catch (InterruptedException interruptedexception) {
System.out.println("\n Catched xsleep.");
}
k1++;
}
else {
jlabel.setLocation(point.x, point.y);
animPanel.repaint();
k1++;
}
}
}
public void stop() {
suspend();
}
public void destroy() {
towThr = null;
}
public synchronized void resume() {
if (suspended) {
suspended = false;
notify();
}
}
public synchronized void suspend() {
suspended = true;
}
public void clear() {
flag = 1;
System.out.println("clear()flag==1");
if (cleared)
return;
if (disks == null)
return;
cleared = true;
destroy();
for (int i = 0; i < numDisk; i++) {
/////从容器的组件列表中删除指定索引处的组件
animPanel.remove(disks[i]);
disks[i] = null;
}
disks = null;
HanoiFrame _tmp = this;
codePanel.clear();
record.setText(null);
toInit = true;
toStart = true;
numDisk = 0;
mvCount = 0;
counterText.setText(" ");
newButton.setEnabled(true);
runButton.setEnabled(false);
stepButton.setEnabled(false);
stackPanel.clear();
repaint();
}
public void setRecord(String s, String s1) {
record.append(" Move top disk from " + s + " to " + s1 + "\n");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -