数据结构

package com.struc;

import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;

public class Hanoi extends JFrame {

private static final long serialVersionUID = 1L;

static int n = Integer.parseInt(JOptionPane
.showInputDialog("输入盘子数目 int:"));

static int time = Integer.parseInt(JOptionPane
.showInputDialog("输入移动盘子的频率 int:"));
static int num = 0;

static char[] array = new char[n];

MyPanel panel = new MyPanel();
static String string = "";
static JTextArea text = new JTextArea();

public Hanoi() {
getContentPane().setLayout(new BorderLayout());
getContentPane().add(new JScrollPane(text), BorderLayout.EAST);
getContentPane().add(panel, BorderLayout.CENTER);
}

public static void main(String[] args) {
for (int i = 0; i < n; i++) {
array[i] = 'A';
}
Hanoi hanoi = new Hanoi();
hanoi.setTitle("HanoiZiJiZuo Tower");
hanoi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
hanoi.setSize(1450, 400);
hanoi.setVisible(true);
hanoi.moveDisks(n, 'A', 'C', 'B');
}

public class MyPanel extends JPanel {

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.drawLine(50, 310, 1100, 310);
g.fillRect(200, 10, 10, 300);
g.drawString("A", 203, 340);
g.fillRect(500, 10, 10, 300);
g.drawString("B", 503, 340);
g.fillRect(800, 10, 10, 300);
g.drawString("C", 803, 340);
draw(g);
repaint();
}

void draw(Graphics g) {
g.setColor(Color.YELLOW);
num = 0;
for (int i = n; i > 0; i--) {

if (array[(int) (i - 1.0)] == 'A') {
g.fillOval(205 - (int) ((i - 1 + 1.0) / n * 150),
290 - 20 * num,
2 * (int) ((i - 1 + 1.0) / n * 150), 20);

g.setColor(Color.BLACK);
g.drawString("" + i, 203, 304 - 20 * num);
g.setColor(Color.YELLOW);
num++;
}

}
num = 0;
for (int i = n; i > 0; i--) {

if (array[(int) (i - 1)] == 'B') {
g.fillOval(505 - (int) ((i - 1 + 1.0) / n * 150),
290 - 20 * num,
2 * (int) ((i - 1 + 1.0) / n * 150), 20);
g.setColor(Color.BLACK);
g.drawString("" + i, 503, 304 - 20 * num);
g.setColor(Color.YELLOW);
num++;
}

}
num = 0;

for (int i = n; i > 0; i--) {
if (array[(int) (i - 1)] == 'C') {
g.fillOval(805 - (int) ((i - 1 + 1.0) / n * 150),
290 - 20 * num,
2 * (int) ((i - 1 + 1.0) / n * 150), 20);
g.setColor(Color.BLACK);
g.drawString("" + i, 803, 304 - 20 * num);
g.setColor(Color.YELLOW);
num++;
}
}
g.setColor(Color.BLACK);
}

}

public void moveDisks(int a, char fromTower, char toTower, char auxTower) {
// Stopping condition
if (a == 1) {
System.out.println("Move disk " + a + " from " + fromTower + " to "
+ toTower);
string += "Move disk " + a + " from " + fromTower + " to "
+ toTower + "         .\n";
text.setText(string);
array[a - 1] = toTower;
pause();
} else {
moveDisks(a - 1, fromTower, auxTower, toTower);
System.out.println("Move disk " + a + " from " + fromTower + " to "
+ toTower);
string += "Move disk " + a + " from " + fromTower + " to "
+ toTower + "         .\n";
text.setText(string);
array[a - 1] = toTower;
pause();
moveDisks(a - 1, auxTower, toTower, fromTower);

}

}

public static void pause() {
try {
Thread.sleep(time * 100);// 可以修改此值加快盘子移动的速度
} catch (InterruptedException e) {
}
}
}

你可能感兴趣的:(数据结构,C++,c,swing,C#)