是有图像的,就像动画一样让他动起来,因为是递规算法,所以我不知道怎么控制,好像在线程里面不好控制
可以啊,只是我现在有这个想法,不知道怎么实现,请高人指点,希望有兴趣的同仁共同探讨和研究。因为我现在在学习算法分析与设计,我想做好多经典算法的演示程序,也希望有兴趣的同仁共同探讨和研究。
我以前做过一个类似的程序,既可以手动移动塔,也可以自动移塔,移动时间也可以调节。
我也传上去源代码,望大家指点
package hannuota;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Hanoi
extends JFrame
implements ActionListener {
private Thread auto;
private int n = 4;
private int[] a = new int[3];
private int[][] x;
private int[] y;
private int[][] w;
private int h = 5;
private void initial() {
a[0] = n;
a[1] = 0;
a[2] = 0;
x = new int[3][n];
y = new int[n];
w = new int[3][n];
for (int i = 0; i < n; i++) {
x[0][i] = 75 - 5 * (n - i);
y[i] = 185 - i * h;
w[0][i] = 10 + 10 * (n - i);
}
}
private static int FROM;
private static int TO;
private int line_1_x = 80, line_2_x = 190, line_3_x = 300, line_y1 = 10,
line_y2 = 190;
private int fk_height = 5;
private boolean[] b = new boolean[3];
private boolean started;
private boolean stopAuto;
private Thread colorControler = null;
private Time clock = new Time();
private JTextField counter = new JTextField(5);
private int c;
private JPanel0 jp = new JPanel0();
private JButton[] jb = {
null, null, null};
private Box box = Box.createHorizontalBox();
private JMenuBar jmb = new JMenuBar();
private JMenu jm = new JMenu("文件(F)");
private JMenuItem jmi[] = {
null, null, null, null, null, null};
JMenuItem stop = new JMenuItem("stop");
JMenuItem goOn = new JMenuItem("goOn");
public Hanoi() {
setSize(500, 300);
setTitle("汉诺塔");
addWindowListener(new WindowDestroyer());
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
auto = new Auto();
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(new JLabel("时间"));
panel.add(clock);
panel.add(new JLabel("次数"));
panel.add(counter);
initial();
jp.add(panel, BorderLayout.NORTH);
counter.setEditable(false);
counter.setBackground(Color.white);
counter.setText("0");
jp.setBackground(Color.blue);
cp.add(jp, BorderLayout.CENTER);
jb[0] = new JButton("0");
jb[0].addActionListener(this);
box.add(Box.createHorizontalStrut(60));
box.add(jb[0]);
jb[1] = new JButton("1");
jb[1].addActionListener(this);
box.add(Box.createHorizontalStrut(70));
box.add(jb[1]);
jb[2] = new JButton("2");
jb[2].addActionListener(this);
box.add(Box.createHorizontalStrut(70));
box.add(jb[2]);
jmi[0] = new JMenuItem("开始");
jmi[0].addActionListener(this);
jm.add(jmi[0]);
stop.addActionListener(this);
//stop.setEnabled(false);
jm.add(stop);
goOn.addActionListener(this);
//stop.setEnabled(false);
jm.add(goOn);
jmi[1] = new JMenuItem("自动");
jmi[1].addActionListener(this);
jm.add(jmi[1]);
jmi[2] = new JMenuItem("退步");
jmi[2].addActionListener(this);
jm.add(jmi[2]);
jmi[3] = new JMenuItem("重新开始");
jmi[3].addActionListener(this);
jm.add(jmi[3]);
jmi[4] = new JMenuItem("退出");
jmi[4].addActionListener(this);
jm.add(jmi[4]);
jm.setMnemonic('F');
jmb.add(jm);
jmi[5] = new JMenu("级别(L)");
jmi[5].setMnemonic('L');
JMenuItem mi;
mi = new JMenuItem("3");
mi.addActionListener(this);
jmi[5].add(mi);
mi = new JMenuItem("4");
mi.addActionListener(this);
jmi[5].add(mi);
mi = new JMenuItem("5");
mi.addActionListener(this);
jmi[5].add(mi);
mi = new JMenuItem("6");
mi.addActionListener(this);
jmi[5].add(mi);
mi = new JMenuItem("7");
mi.addActionListener(this);
jmi[5].add(mi);
mi = new JMenuItem("8");
mi.addActionListener(this);
jmi[5].add(mi);
mi = new JMenuItem("9");
mi.addActionListener(this);
jmi[5].add(mi);
mi = new JMenuItem("10");
mi.addActionListener(this);
jmi[5].add(mi);
mi = new JMenuItem("11");
mi.addActionListener(this);
jmi[5].add(mi);
mi = new JMenuItem("12");
mi.addActionListener(this);
jmi[5].add(mi);
jmb.add(jmi[5]);
setJMenuBar(jmb);
cp.add(box, BorderLayout.SOUTH);
//colorControler = new ColorControler();
//colorControler.start();
}
class Auto
extends Thread {
public void run() {
m(n, 0, 2, 1);
}
private void m(int N, int from, int to, int spare) {
if (N == 1) {
synchronized (auto) {
try {
auto.sleep(500);
if (stopAuto) {
wait();
}
}
catch (Exception e) {
System.out.println("m() wrong");
}
move1(from, to);
}
}
else {
m(N - 1, from, spare, to);
synchronized (auto) {
try {
auto.sleep(500);
if (stopAuto) {
wait();
}
}
catch (Exception e) {
System.out.println("m() wrong");
}
move1(from, to);
}
m(N - 1, spare, to, from);
}
}
}
/*private class ColorControler
extends Thread {
public void run() {
while (true) {
if (a[2] == n) {
clock.stop();
}
for (int i = 0; i < 3; i++) {
if (b[i]) {
jb[i].setBackground(Color.red);
}
else {
jb[i].setBackground(Color.lightGray);
}
}
}
}
}*/
private class JPanel0
extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(line_1_x, line_y1, line_1_x, line_y2);
g.drawLine(line_2_x, line_y1, line_2_x, line_y2);
g.drawLine(line_3_x, line_y1, line_3_x, line_y2);
g.drawLine(0, line_y2, line_3_x + 100, line_y2);
g.setColor(Color.yellow);
for (int j = 0; j < 3; j++) {
for (int i = 0; i < a[j]; i++) {
g.fillRect(x[j][i], y[i], w[j][i], h);
}
}
}
}
public void actionPerformed(ActionEvent e) {
String ac = e.getActionCommand();
if (ac.equals("开始")) {
stop.setEnabled(true);
started = true;
clock.start();
}
else if (ac.equals("stop")) {
stopAuto = true;
//clock.stop();
}
else if (ac.equals("goOn")) {
stopAuto = false;
synchronized (auto) {
try {
auto.notify();
}
catch (Exception ef) {}
}
synchronized (clock.time) {
try {
clock.time.notify();
}
catch (Exception ef) {}
}
}
else if (ac.equals("3") || ac.equals("4") ||
ac.equals("5") || ac.equals("6") || ac.equals("7") ||
ac.equals("8") || ac.equals("9") || ac.equals("10") ||
ac.equals("11") || ac.equals("12")) {
n = Integer.parseInt(ac);
initial();
jp.repaint();
/*Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int height = screenSize.height;
int width = screenSize.width;
setLocation( (height - 50) / 2, (width - 30) / 2);*/
}
else if (ac.equals("自动")) {
try {
clock.start();
auto.start();
}
catch (Exception ef) {
System.out.println("m wrong");
}
}
else if (ac.equals("退步")) {
move1(TO, FROM);
c -= 2;
counter.setText("" + c);
}
else if (ac.equals("重新开始")) {
dispose();
(new Hanoi()).setVisible(true);
}
else if (ac.equals("退出")) {
System.exit(0);
}
else if (started && ac.equals("0")) {
b[0] = !b[0];
changeColor();
//jb[0].setBackground(Color.red);
//jb[0].setBackground(Color.lightGray);
if (b[1]) {
if (a[0] == 0 || w[0][a[0] - 1] > w[1][a[1] - 1]) {
move1(1, 0);
}
else {
System.out.println("can't do that");
}
b[0] = b[1] = b[2] = false;
changeColor();
}
else if (b[2]) {
if (a[0] == 0 || w[0][a[0] - 1] > w[2][a[2] - 1]) {
move1(2, 0);
}
else {
System.out.println("can't do that");
}
b[0] = b[1] = b[2] = false;
changeColor();
}
}
else if (started && ac.equals("1")) {
b[1] = !b[1];
changeColor();
if (b[0]) {
if (a[1] == 0 || w[1][a[1] - 1] > w[0][a[0] - 1]) {
move1(0, 1);
}
else {
System.out.println("can't do that");
}
b[0] = b[1] = b[2] = false;
changeColor();
}
else if (b[2]) {
if (a[1] == 0 || w[1][a[1] - 1] > w[2][a[2] - 1]) {
move1(2, 1);
}
else {
System.out.println("can't do that");
}
b[0] = b[1] = b[2] = false;
changeColor();
}
}
else if (started && ac.equals("2")) {
b[2] = !b[2];
changeColor();
if (b[0]) {
if (a[2] == 0 || w[2][a[2] - 1] > w[0][a[0] - 1]) {
move1(0, 2);
}
else {
System.out.println("can't do that");
}
b[0] = b[1] = b[2] = false;
changeColor();
changeColor();
}
else if (b[1]) {
if (a[2] == 0 || w[2][a[2] - 1] > w[1][a[1] - 1]) {
move1(1, 2);
}
else {
System.out.println("can't do that");
}
b[0] = b[1] = b[2] = false;
changeColor();
}
}
}
private void changeColor() {
for (int i = 0; i < 3; i++) {
if (!b[i]) {
jb[i].setBackground(Color.lightGray);
}
//if( b.getBackground().equals(Color.lightGray)){
else {
jb[i].setBackground(Color.red);
}
}
}
private void move1(int from, int to) {
FROM = from;
TO = to;
c++;
counter.setText("" + c);
x[to][a[to]] = x[from][a[from] - 1] + (to - from) * 110;
w[to][a[to]] = w[from][a[from] - 1];
a[from]--;
a[to]++;
jp.repaint();
}
private class Time
extends JTextField
implements Runnable {
Thread time = null;
private int i;
public Time() {
super(5);
setEditable(false);
setBackground(Color.white);
setText(0 + "");
}
public void start() {
if (time == null) {
time = new Thread(this);
time.start();
}
}
public void stop() {
time = null;
}
public void run() {
while (time != null) {
setText(i + "");
synchronized (time) {
try {
time.sleep(1000);
time.wait();
}
catch (InterruptedException e) {
setText("clock broken");
}
}
i++;
}
}
}
public static void main(String[] args) {
Hanoi hanoi1 = new Hanoi();
hanoi1.setVisible(true);
}
}
class WindowDestroyer
extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
望众高手不吝赐教!