package ui;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import logic.Pillar;
public class WinMain extends JFrame {
public static final int DISC_HEIGHT = 10;// 碟子的厚度
public static final int X = 30;
public static final int Y = 150;// 柱子基部的Y坐标
private int speed = 100;
private List list = new ArrayList();
private Pillar A = new Pillar(X + 80, Y, 0, "A");// 起始柱子
private Pillar B = new Pillar(X + 200, Y, 0, "B");// 经过柱子
private Pillar C = new Pillar(X + 320, Y, 0, "C");// 目标柱子
JButton button1 = new JButton("Go");
// 碟子的集合
Rectangle[] rects = new Rectangle[] { //new Rectangle(0, 0, 30, DISC_HEIGHT),
//new Rectangle(0, 0, 50, DISC_HEIGHT),
new Rectangle(0, 0, 70, DISC_HEIGHT),
new Rectangle(0, 0, 90, DISC_HEIGHT),
new Rectangle(0, 0, 110, DISC_HEIGHT) };
public WinMain() throws HeadlessException {
super();
init();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(0, 0);
this.setSize(450, 350);
this.getContentPane().setLayout(null);
this.getContentPane().add(button1);
button1.setSize(200, 100);
button1.setLocation(this.getWidth() / 2 - button1.getWidth() / 2, 200);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
init();
go(rects.length - 1, A, C);
}
});
this.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent arg0) {
WinMain.this.repaint();
}
public void focusLost(FocusEvent arg0) {
}
});
}
@Override
public void paint(Graphics g) {
super.paint(g);
int delt = 15;
g.drawString(A.getName(), A.getX(), A.getY() + delt);
g.drawString(B.getName(), B.getX(), B.getY() + delt);
g.drawString(C.getName(), C.getX(), C.getY() + delt);
for (Rectangle r : rects) {
g.drawRect(r.x, r.y, (int) r.getWidth(), (int) r.getHeight());
}
}
/**
* 设置初始位置
*
*/
private void init() {
A.setNum(0);
B.setNum(0);
C.setNum(0);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = rects.length - 1; i >= 0; i--) {
addDiscToZhu(rects[i], A);
}
}
/**
* 把r加到目标柱子上
*
* @param r
* @param dest
*/
private void addDiscToZhu(Rectangle r, Pillar dest) {
dest.setNum(dest.getNum() + 1);
r.setLocation(dest.getX() - (int) r.getWidth() / 2, Y - dest.getNum()
* WinMain.DISC_HEIGHT);
}
private Pillar getOher(Pillar a, Pillar b) {
list.clear();
list.add(A);
list.add(B);
list.add(C);
list.remove(a);
list.remove(b);
return (Pillar) list.get(0);
}
/**
* 把0-i的元素从from移到to
*
* @param i
* @param A
* @param C
*/
private void go(int i, Pillar from, Pillar to) {
if (i == 0) {
moveTo(i, from, to);
} else {
go(i - 1, from, this.getOher(from, to));
moveTo(i, from, to);
go(i - 1, this.getOher(from, to), to);
}
}
/**
* 将一个元素i从from移到to
*
* @param i
* @param from
* @param to
*/
private void moveTo(int i, Pillar from, Pillar to) {
from.setNum(from.getNum() - 1);
addDiscToZhu(rects[i], to);
repaint();
this.update(this.getGraphics());
try {
Thread.sleep(500 - speed);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Thread(new Runnable(){
public void run() {
JFrame win = new WinMain();
win.setVisible(true);
}
}).start();
}
}