DrawingFrame.java~110~
上传用户:qiye11114
上传日期:2010-01-20
资源大小:126k
文件大小:6k
源码类别:

其他智力游戏

开发平台:

Java

  1. package hanoi;
  2. import java.awt.BorderLayout;
  3. import javax.swing.JFrame;
  4. import javax.swing.*;
  5. import java.awt.*;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.util.*;
  9. /**
  10.  * <p>Title: Hanoi Pan</p>
  11.  *
  12.  * <p>Description: </p>
  13.  *
  14.  * <p>Copyright: Copyright (c) 2005</p>
  15.  *
  16.  * <p>Company: </p>
  17.  *
  18.  * @author not attributable
  19.  * @version 1.0
  20.  */
  21. class DrawPanel extends JPanel {
  22.     private Hanoi_pan refHanoi_pan[];
  23.     public DrawPanel(Hanoi_pan myHanoi_pan[]) {
  24.         refHanoi_pan=myHanoi_pan;
  25.     }
  26.     public void paintComponent(Graphics g) {
  27.         super.paintComponent(g);
  28.         g.setColor(Color.black);
  29.         g.drawLine(35,150,125,150);
  30.         g.drawLine(155,150,245,150);
  31.         g.drawLine(275,150,365,150);
  32.         g.drawLine(80,50,80,150);
  33.         g.drawLine(200,50,200,150);
  34.         g.drawLine(320,50,320,150);
  35.         g.setColor(Color.blue);
  36.         for (int i=1;i<=refHanoi_pan.length;i++) {
  37.             g.drawRect(refHanoi_pan[i-1].x,refHanoi_pan[i-1].y,refHanoi_pan[i-1].width,refHanoi_pan[i-1].height);
  38.         }
  39.     }
  40. }
  41. public class DrawingFrame extends JFrame implements Runnable {
  42.     private int level;
  43.     private DrawPanel myDrawPanel;
  44.     JButton jButton1 = new JButton();
  45.     private Hanoi_pan myHanoi_pan[];
  46.     int bench[] = new int[3];
  47.     Thread PaintThread;
  48.     JButton jButton2 = new JButton();
  49.     public void run() {
  50.         Hanoi(level,1,2,3);
  51.     }
  52.     public DrawingFrame(int n) {
  53.         try {
  54.             bench[0]=level=n;
  55.             myHanoi_pan = new Hanoi_pan[level];
  56.             myDrawPanel=new DrawPanel(myHanoi_pan);
  57.             jbInit();
  58.         } catch (Exception exception) {
  59.             exception.printStackTrace();
  60.         }
  61.     }
  62.     private void Hanoi(int n,int x,int y,int z) {
  63.         if (n == 1) {
  64.             Move(x, 1, z);
  65.         } else {
  66.             Hanoi(n - 1, x, z, y);
  67.             Move(x, n, z);
  68.             Hanoi(n - 1, y, x, z);
  69.         }
  70.     }
  71.     private void Move(int x,int n,int z) {
  72.         SetLocation(x,n,z);
  73.         try {
  74.             repaint();
  75.             Thread.sleep(500);
  76.         } catch (Exception e) {
  77.             if (!Thread.currentThread().isAlive()) {
  78.                 System.exit(0);
  79.             }
  80.         }
  81.         //System.out.println("move pan "+ n + " from " + x + " to " + z);
  82.     }
  83.     private void SetLocation(int x,int n,int z) {
  84.         switch (x) {
  85.         case 1:
  86.             bench[0]--;
  87.             break;
  88.         case 2:
  89.             bench[1]--;
  90.             break;
  91.         case 3:
  92.             bench[2]--;
  93.             break;
  94.         }
  95.         switch (z) {
  96.         case 1:
  97.             myHanoi_pan[n-1].x=80-myHanoi_pan[n-1].width/2;
  98.             myHanoi_pan[n-1].y=140-bench[0]*10;
  99.             bench[0]++;
  100.             break;
  101.         case 2:
  102.             myHanoi_pan[n-1].x=200-myHanoi_pan[n-1].width/2;
  103.             myHanoi_pan[n-1].y=140-bench[1]*10;
  104.             bench[1]++;
  105.             break;
  106.         case 3:
  107.             myHanoi_pan[n-1].x=320-myHanoi_pan[n-1].width/2;
  108.             myHanoi_pan[n-1].y=140-bench[2]*10;
  109.             bench[2]++;
  110.             break;
  111.         }
  112.     }
  113.     private void Init() {
  114.         for (int i=1;i<=level;i++) {
  115.             myHanoi_pan[i-1]= new Hanoi_pan(80-5*i,150-10*(level-i+1),10*i,10);
  116.         }
  117.     }
  118.     private void jbInit() throws Exception {
  119.         Init();
  120.         setSize(new Dimension(400, 300));
  121.         this.setTitle("Hanoi");
  122.         getContentPane().setLayout(null);
  123.         jButton1.setBounds(new Rectangle(118, 219, 60, 27));
  124.         jButton1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
  125.         jButton1.setText("开始");
  126.         jButton1.addActionListener(new DrawingFrame_jButton1_actionAdapter(this));
  127.         jButton2.setBounds(new Rectangle(191, 219, 60, 27));
  128.         jButton2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
  129.         jButton2.setText("退出");
  130.         jButton2.addActionListener(new DrawingFrame_jButton2_actionAdapter(this));
  131.         this.getContentPane().add(myDrawPanel, null);
  132.         this.getContentPane().add(jButton1);
  133.         this.getContentPane().add(jButton2);
  134.         this.setResizable(false);
  135.         myDrawPanel.setBounds(new Rectangle(0, 0, 400, 200));
  136.         myDrawPanel.setLayout(null);
  137.         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  138.         Dimension frameSize = this.getSize();
  139.         if (frameSize.height > screenSize.height) {
  140.             frameSize.height = screenSize.height;
  141.         }
  142.         if (frameSize.width > screenSize.width) {
  143.             frameSize.width = screenSize.width;
  144.         }
  145.         this.setLocation((screenSize.width - frameSize.width) / 2,
  146.                           (screenSize.height - frameSize.height) / 2);
  147.         this.setVisible(true);
  148.     }
  149.     public void jButton1_actionPerformed(ActionEvent e) {
  150.         if (PaintThread==null) {
  151.             PaintThread=new Thread(this);
  152.             PaintThread.start();
  153.             this.jButton1.setText("停止");
  154.             this.jButton2.setEnabled(false);
  155.         }
  156.         else if (!PaintThread.isInterrupted()) {
  157.             PaintThread.interrupt();
  158.             this.jButton2.setText("开始");
  159.         }
  160.     }
  161.     public void jButton2_actionPerformed(ActionEvent e) {
  162.         System.exit(0);
  163.     }
  164. }
  165. class DrawingFrame_jButton2_actionAdapter implements ActionListener {
  166.     private DrawingFrame adaptee;
  167.     DrawingFrame_jButton2_actionAdapter(DrawingFrame adaptee) {
  168.         this.adaptee = adaptee;
  169.     }
  170.     public void actionPerformed(ActionEvent e) {
  171.         adaptee.jButton2_actionPerformed(e);
  172.     }
  173. }
  174. class DrawingFrame_jButton1_actionAdapter implements ActionListener {
  175.     private DrawingFrame adaptee;
  176.     DrawingFrame_jButton1_actionAdapter(DrawingFrame adaptee) {
  177.         this.adaptee = adaptee;
  178.     }
  179.     public void actionPerformed(ActionEvent e) {
  180.         adaptee.jButton1_actionPerformed(e);
  181.     }
  182. }