java 汉诺塔的演示程序

原创 2007年09月12日 08:37:00
import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;


public class Hanoi extends JFrame
{
        static int n=Integer.parseInt(JOptionPane.showInputDialog("Enter number of disks:"));
        static int time=Integer.parseInt(JOptionPane.showInputDialog("Enter the move frequency of disks:"));
        static int num=0;
        static char[] array=new char[n];
        private static Thread animate;
       
        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("Hanoi 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)
        {
               
                       
               
                if (a == 1) // Stopping condition
                {
                        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)
                {
                }
        }
       
}

相关文章推荐

(Hanoi)汉诺塔java实现程序

汉诺塔:汉诺塔(又称河内塔)问题是源于印度一个古老传说的益智玩具。大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘。大梵天命令婆罗门把圆盘从下面开始按大小顺序...

[js - 算法可视化] 汉诺塔(Hanoi)演示程序

前段时间偶然看到有个日本人很早之前写了js的多种排序程序,使用js+html实现的排序动画,效果非常好。 受此启发,我决定写几个js的算法动画,第一个就用汉诺塔。 另外在这个程序里面,实现了一点,就是...
  • isea533
  • isea533
  • 2013年08月30日 14:46
  • 6526

汉诺塔java程序源代码

  • 2012年05月12日 13:18
  • 20KB
  • 下载

汉诺塔 java程序

  • 2011年01月25日 17:11
  • 1KB
  • 下载

汉诺塔演示程序QBasic版,开博第一帖

最近手痒,写了个字符环境下的
  • tony958
  • tony958
  • 2014年11月10日 20:35
  • 415

java汉诺塔游戏小程序

  • 2011年01月09日 01:38
  • 3KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:java 汉诺塔的演示程序
举报原因:
原因补充:

(最多只允许输入30个字)