DrawingFrame.java~109~
资源名称:汉诺塔图形演示.rar [点击查看]
上传用户:qiye11114
上传日期:2010-01-20
资源大小:126k
文件大小:6k
源码类别:
其他智力游戏
开发平台:
Java
- package hanoi;
- import java.awt.BorderLayout;
- import javax.swing.JFrame;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.*;
- /**
- * <p>Title: Hanoi Pan</p>
- *
- * <p>Description: </p>
- *
- * <p>Copyright: Copyright (c) 2005</p>
- *
- * <p>Company: </p>
- *
- * @author not attributable
- * @version 1.0
- */
- class DrawPanel extends JPanel {
- private Hanoi_pan refHanoi_pan[];
- public DrawPanel(Hanoi_pan myHanoi_pan[]) {
- refHanoi_pan=myHanoi_pan;
- }
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- g.setColor(Color.black);
- g.drawLine(35,150,125,150);
- g.drawLine(155,150,245,150);
- g.drawLine(275,150,365,150);
- g.drawLine(80,50,80,150);
- g.drawLine(200,50,200,150);
- g.drawLine(320,50,320,150);
- g.setColor(Color.blue);
- for (int i=1;i<=refHanoi_pan.length;i++) {
- g.drawRect(refHanoi_pan[i-1].x,refHanoi_pan[i-1].y,refHanoi_pan[i-1].width,refHanoi_pan[i-1].height);
- }
- }
- }
- public class DrawingFrame extends JFrame implements Runnable {
- private int level;
- private DrawPanel myDrawPanel;
- JButton jButton1 = new JButton();
- private Hanoi_pan myHanoi_pan[];
- int bench[] = new int[3];
- Thread PaintThread;
- JButton jButton2 = new JButton();
- public void run() {
- Hanoi(level,1,2,3);
- }
- public DrawingFrame(int n) {
- try {
- bench[0]=level=n;
- myHanoi_pan = new Hanoi_pan[level];
- myDrawPanel=new DrawPanel(myHanoi_pan);
- jbInit();
- } catch (Exception exception) {
- exception.printStackTrace();
- }
- }
- private void Hanoi(int n,int x,int y,int z) {
- if (n == 1) {
- Move(x, 1, z);
- } else {
- Hanoi(n - 1, x, z, y);
- Move(x, n, z);
- Hanoi(n - 1, y, x, z);
- }
- }
- private void Move(int x,int n,int z) {
- SetLocation(x,n,z);
- try {
- repaint();
- Thread.sleep(500);
- } catch (Exception e) {
- if (!Thread.currentThread().isAlive()) {
- System.exit(0);
- }
- }
- //System.out.println("move pan "+ n + " from " + x + " to " + z);
- }
- private void SetLocation(int x,int n,int z) {
- switch (x) {
- case 1:
- bench[0]--;
- break;
- case 2:
- bench[1]--;
- break;
- case 3:
- bench[2]--;
- break;
- }
- switch (z) {
- case 1:
- myHanoi_pan[n-1].x=80-myHanoi_pan[n-1].width/2;
- myHanoi_pan[n-1].y=140-bench[0]*10;
- bench[0]++;
- break;
- case 2:
- myHanoi_pan[n-1].x=200-myHanoi_pan[n-1].width/2;
- myHanoi_pan[n-1].y=140-bench[1]*10;
- bench[1]++;
- break;
- case 3:
- myHanoi_pan[n-1].x=320-myHanoi_pan[n-1].width/2;
- myHanoi_pan[n-1].y=140-bench[2]*10;
- bench[2]++;
- break;
- }
- }
- private void Init() {
- for (int i=1;i<=level;i++) {
- myHanoi_pan[i-1]= new Hanoi_pan(80-5*i,150-10*(level-i+1),10*i,10);
- }
- }
- private void jbInit() throws Exception {
- Init();
- setSize(new Dimension(400, 300));
- this.setTitle("Hanoi");
- getContentPane().setLayout(null);
- jButton1.setBounds(new Rectangle(118, 219, 60, 27));
- jButton1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
- jButton1.setText("开始");
- jButton1.addActionListener(new DrawingFrame_jButton1_actionAdapter(this));
- jButton2.setBounds(new Rectangle(191, 219, 60, 27));
- jButton2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
- jButton2.setText("退出");
- jButton2.addActionListener(new DrawingFrame_jButton2_actionAdapter(this));
- this.getContentPane().add(myDrawPanel, null);
- this.getContentPane().add(jButton1);
- this.getContentPane().add(jButton2);
- this.setResizable(false);
- myDrawPanel.setBounds(new Rectangle(0, 0, 400, 200));
- myDrawPanel.setLayout(null);
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
- Dimension frameSize = this.getSize();
- if (frameSize.height > screenSize.height) {
- frameSize.height = screenSize.height;
- }
- if (frameSize.width > screenSize.width) {
- frameSize.width = screenSize.width;
- }
- this.setLocation((screenSize.width - frameSize.width) / 2,
- (screenSize.height - frameSize.height) / 2);
- this.setVisible(true);
- }
- public void jButton1_actionPerformed(ActionEvent e) {
- if (PaintThread==null) {
- PaintThread=new Thread(this);
- PaintThread.start();
- this.jButton1.setText("停止");
- this.jButton2.setEnabled(false);
- }
- else if (!PaintThread.isInterrupted()) {
- PaintThread.destroy();
- this.jButton2.setText("开始");
- }
- }
- public void jButton2_actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- }
- class DrawingFrame_jButton2_actionAdapter implements ActionListener {
- private DrawingFrame adaptee;
- DrawingFrame_jButton2_actionAdapter(DrawingFrame adaptee) {
- this.adaptee = adaptee;
- }
- public void actionPerformed(ActionEvent e) {
- adaptee.jButton2_actionPerformed(e);
- }
- }
- class DrawingFrame_jButton1_actionAdapter implements ActionListener {
- private DrawingFrame adaptee;
- DrawingFrame_jButton1_actionAdapter(DrawingFrame adaptee) {
- this.adaptee = adaptee;
- }
- public void actionPerformed(ActionEvent e) {
- adaptee.jButton1_actionPerformed(e);
- }
- }