⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hannoitower.java

📁 用java写的一个动画显示汉诺塔移动的程序
💻 JAVA
字号:
/* * HannoiTower.java * * Created on 2007年10月4日, 下午6:07 * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */package 汉诺塔;/** * * @author baili */import javax.swing.*;import java.awt.*;import java.awt.event.*;public class HannoiTower extends JPanel{   TowerPoint point[];                                             int x,y;                                                        boolean move=false;                                            Disk   盘子[];                                                 int startX,startY;                                             int startI ;                                                   int 盘子数目=0;   int width,height;                                              char towerName[]={'A','B','C'};                               TextArea 信息条=null;        Image image;   Font font;   public  HannoiTower(int number,int w,int h,char[] name,TextArea text)   {        font = new Font("Helvetica", Font.BOLD, 25);        image = new javax.swing.ImageIcon("F:/My Pictures/205024.jpg").getImage();        towerName=name;        盘子数目=number;        width=w;        height=h;        信息条=text;        this.setBackground(Color.WHITE);        setLayout(null);        盘子= new Disk[盘子数目];        point=new TowerPoint[3*盘子数目];                                  int space=20;          for(int i=0;i<盘子数目;i++)                                         {            point[i]=new TowerPoint(40+width,100+space,false);             space=space+height;          }        space=20;        for(int i=盘子数目;i<2*盘子数目;i++)                                  {            point[i]=new TowerPoint(160+width,100+space,false);            space=space+height;          }        space=20;        for(int i=2*盘子数目;i<3*盘子数目;i++)                               {            point[i]=new TowerPoint(280+width,100+space,false);            space=space+height;          }       int tempWidth=width;                                             int sub=(int)(tempWidth*0.1);       for(int i=盘子数目-1;i>=0;i--)                                      {             盘子[i]=new Disk(i);             盘子[i].setSize(tempWidth,height);                                tempWidth=tempWidth-sub;          }        for(int i=0;i<盘子数目;i++)                                       {            point[i].放置盘子1(盘子[i],this);            if(i>=1)            盘子[i].set上方有盘(true);           }    }   public void update(Graphics g)   {       paint(g);   }  public void paint(Graphics g)                        {    // super.paintComponent(g);        g.drawImage(image, 0,0,getWidth(),getHeight(),this);     g.setColor(Color.green);     g.drawLine(point[0].getX(),point[0].getY(),                point[盘子数目-1].getX(),point[盘子数目-1].getY());          g.drawLine(point[盘子数目].getX(),point[盘子数目].getY(),                point[2*盘子数目-1].getX(),point[2*盘子数目-1].getY());      g.drawLine(point[2*盘子数目].getX(),point[2*盘子数目].getY(),                point[3*盘子数目-1].getX(),point[3*盘子数目-1].getY());      g.drawLine(point[盘子数目-1].getX()-width,point[盘子数目-1].getY(),               point[3*盘子数目-1].getX()+width,point[3*盘子数目-1].getY());      int leftx=point[盘子数目-1].getX()-width;     int lefty=point[盘子数目-1].getY();     int w=(point[3*盘子数目-1].getX()+width)-(point[盘子数目-1].getX()-width);     int h=height/2;     g.setColor(Color.cyan);     g.fillRect(leftx,lefty,w,2);     g.setFont(font);     g.setColor(Color.MAGENTA);     int size=2;    // for(int i=0;i<3*盘子数目;i++)                                          //   g.fillOval(point[i].getX()-size/2,point[i].getY()-size/2,size,size);     g.drawString(""+towerName[0]+"塔",point[盘子数目-1].getX()-10,point[盘子数目-1].getY()+40);     g.drawString(""+towerName[1]+"塔",point[2*盘子数目-1].getX()-10,point[盘子数目-1].getY()+40);     g.drawString(""+towerName[2]+"塔",point[3*盘子数目-1].getX()-10,point[盘子数目-1].getY()+40);    // g.drawString("将全部盘子从"+towerName[0]+"塔搬运到"+towerName[1]+"塔或"+towerName[2]+"塔",              //    point[盘子数目-1].getX(),point[盘子数目-1].getY()+80);           } //  public void paint(Graphics g) // {      //g.drawImage(image, 0,0,getWidth(),getHeight(),this); // } public void 自动演示搬运盘子(int 盘子数,char one,char two,char three)  {     if(盘子数==1)       {         Disk disk=在塔中获取最上面的盘子(one);         int startI=在塔中获取最上面盘子的位置(one);         int endI=在塔中获取最上面盘子的上方位置(three);                          if(disk!=null)           {                            //point[endI].放置盘子1(disk,this);           // disk.setBackground(Color.blue);             point[endI].放置盘子(disk,this,point[startI]);//             point[endI].放置盘子1(disk,this);             point[startI].set有盘子(false);             信息条.append(""+one+" 到: "+three+"塔\n");           }        }      else        {          自动演示搬运盘子(盘子数-1,one,three,two);          Disk disk=在塔中获取最上面的盘子(one);          int startI=在塔中获取最上面盘子的位置(one);          int endI=在塔中获取最上面盘子的上方位置(three);                           if(disk!=null)            {                           // point[endI].放置盘子1(disk,this);              //disk.setBackground(Color.blue);             point[endI].放置盘子(disk,this,point[startI]);      //       point[endI].放置盘子1(disk,this);             point[startI].set有盘子(false);           信息条.append(""+one+" 到: "+three+"塔\n");            } 	自动演示搬运盘子(盘子数-1,two,one,three);        }  } public Disk 在塔中获取最上面的盘子(char 塔名)  {    Disk disk=null;    if(塔名==towerName[0])      {         for(int i=0;i<盘子数目;i++)           {             if(point[i].是否有盘子()==true)               {                 disk=point[i].获取盘子();                 break;               }           }        }     if(塔名==towerName[1])      {         for(int i=盘子数目;i<2*盘子数目;i++)           {             if(point[i].是否有盘子()==true)               {                 disk=point[i].获取盘子();                 break;               }           }        }      if(塔名==towerName[2])      {         for(int i=2*盘子数目;i<3*盘子数目;i++)           {             if(point[i].是否有盘子()==true)               {                 disk=point[i].获取盘子();                 break;               }           }        }   return disk;  }  public int 在塔中获取最上面盘子的上方位置(char 塔名)  {    int position=0;    if(塔名==towerName[0])      {           int i=0;         for(i=0;i<盘子数目;i++)           {             if(point[i].是否有盘子()==true)               {                 position=Math.max(i-1,0);                 break;               }           }          if(i==盘子数目)           {             position=盘子数目-1;           }       }     if(塔名==towerName[1])      {            int i=0;         for(i=盘子数目;i<2*盘子数目;i++)           {             if(point[i].是否有盘子()==true)               {                 position=Math.max(i-1,0);                 break;               }           }           if(i==2*盘子数目)           {             position=2*盘子数目-1;           }        }      if(塔名==towerName[2])      {         int i=0;         for(i=2*盘子数目;i<3*盘子数目;i++)           {             if(point[i].是否有盘子()==true)               {                 position=Math.max(i-1,0);                 break;               }           }            if(i==3*盘子数目)           {             position=3*盘子数目-1;           }        }    return position;  }   public int 在塔中获取最上面盘子的位置(char 塔名)  {    int position=0;    if(塔名==towerName[0])      {           int i=0;         for(i=0;i<盘子数目;i++)           {             if(point[i].是否有盘子()==true)               {                 position=i;                 break;               }           }          //if(i==盘子数目)          // {          //   position=盘子数目-1;         //  }       }     if(塔名==towerName[1])      {            int i=0;         for(i=盘子数目;i<2*盘子数目;i++)           {             if(point[i].是否有盘子()==true)               {                 position=i;                 break;               }           }          // if(i==2*盘子数目)         //  {         //    position=2*盘子数目-1;         //  }        }      if(塔名==towerName[2])      {         int i=0;         for(i=2*盘子数目;i<3*盘子数目;i++)           {             if(point[i].是否有盘子()==true)               {                 position=i;                 break;               }           }           // if(i==3*盘子数目)         //  {         //    position=3*盘子数目-1;         //  }        }    return position;  } }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -