




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、import java.io.*;import java.awt.*;public class Disk extends Button int number; boolean 上方有盘=false; public Disk(int numble,HannoiTower con) this.number=number; setBackground(Color.blue); addMouseMotionListener(con); addMouseListener(con); public boolean get上方有盘() return 上方有盘; public void set上方有盘(boo
2、lean b) 上方有盘=b; public int getNumber() return number; import javax.swing.*;import java.awt.*;import java.awt.event.*;public class HannoiTower extends JPanel implements MouseListener,MouseMotionListenerTowerPoint point;int x,y;boolean move=false;Disk 盘子;int startX,startY;int startI;int 盘子数目=0;int wid
3、th,height;char towerName='A','B','C'TextArea 信息条=null;public HannoiTower(int number,int w,int h,charname,TextArea text)towerName=name;盘子数目=number;width=w;height=h;信息条=text;setLayout(null);addMouseListener(this);盘子=new Disk盘子数目;point=new TowerPoint3*盘子数目;int space=20;for(int i
4、=0;i<盘子数目;i+)pointi=new TowerPoint(40+width,100+space,false);space=space+height;space=20;for(int i=盘子数目;i<2*盘子数目;i+)pointi=new TowerPoint(160+width,100+space,false);space=space+height;space=20;for(int i=2*盘子数目;i<3*盘子数目;i+)pointi=new TowerPoint(280+width,100+space,false);space=space+height;i
5、nt tempWidth=width;int sub=(int)(tempWidth*0.2);for(int i=盘子数目-1;i>=0;i-)盘子i=new Disk(i,this);盘子i.setSize(tempWidth,height);tempWidth=tempWidth-sub;for(int i=0;i<盘子数目;i+)pointi.放置盘子(盘子i,this);if(i>=1)盘子i.set上方有盘(true);public void paintComponent(Graphics g)super.paintComponent(g);g.drawLine(
6、point0.getX(),point0.getY(),point盘子数目-1.getX(),point盘子数目-1.getY();g.drawLine(point盘子数目.getX(),point盘子数目.getY(),point2*盘子数目-1.getX(),point2*盘子数目-1.getY();g.drawLine(point2*盘子数目.getX(),point2*盘子数目.getY(),point3*盘子数目-1.getX(),point3*盘子数目-1.getY();g.drawLine(point盘子数目-1.getX()-width,point盘子数目-1.getY(),p
7、oint3*盘子数目-1.getX()+width,point3*盘子数目-1.getY();int leftx=point盘子数目-1.getX()-width;int lefty=point盘子数目-1.getY();int w=(point3*盘子数目-1.getX()+width)-(point盘子数目-1.getX()-width);int h=height/2;g.setColor(Color.orange);g.fillRect(leftx,lefty,w,h);g.setColor(Color.red);int size=4;for(int i=0;i<3*盘子数目;i+
8、)g.fillOval(pointi.getX()-size/2,pointi.getY()-size/2,size,size);g.drawString(" "+towerName0+"塔",point盘子数目-1.getX(),point盘子数目-1.getY()+30);g.drawString(" "+towerName1+"塔",point2*盘子数目-1.getX(),point盘子数目-1.getY()+30);g.drawString(" "+towerName2+"塔
9、",point3*盘子数目-1.getX(),point盘子数目-1.getY()+30);g.drawString("将全部盘子从"+towerName0+"塔搬运到"+towerName1+"塔或"+towerName2+"塔",point盘子数目-1.getX(),point盘子数目-1.getY()+80);public void mousePressed(MouseEvent e)Disk 盘子=null;Rectangle rect=null;if(e.getSource()=this)mov
10、e=false;if(move=false)if(e.getSource() instanceof Disk)盘子=(Disk)e.getSource();startX=盘子.getBounds().x;startY=盘子.getBounds().y;rect=盘子.getBounds();for(int i=0;i<3*盘子数目;i+)int x=pointi.getX();int y=pointi.getY();if(rect.contains(x,y)startI=i;break;public void mouseMoved(MouseEvent e)public void mou
11、seDragged(MouseEvent e)Disk disk=null;if(e.getSource() instanceof Disk)disk=(Disk)e.getSource();move=true;e=SwingUtilities.convertMouseEvent(disk,e,this);if(e.getSource()=this)if(move&&disk!=null)x=e.getX();y=e.getY();if(disk.get上方有盘()=false)disk.setLocation(x-disk.getWidth()/2,y-disk.getHei
12、ght()/2);public void mouseReleased(MouseEvent e)Disk disk=null;move=false;Rectangle rect=null;if(e.getSource() instanceof Disk)disk=(Disk)e.getSource();rect=disk.getBounds();e=SwingUtilities.convertMouseEvent(disk,e,this);if(e.getSource()=this)boolean containTowerPoint=false;int x=0,y=0;int endI=0;i
13、f(disk!=null)for(int i=0;i<3*盘子数目;i+)x=pointi.getX();y=pointi.getY();if(rect.contains(x,y)containTowerPoint=true;endI=i;break;if(disk!=null&&containTowerPoint)if(pointendI.是否有盘子()=true)disk.setLocation(startX,startY);elseif(endI=盘子数目-1|endI=2*盘子数目-1|endI=3*盘子数目-1)pointendI.放置盘子(disk,this)
14、;if(startI!=盘子数目-1&&startI!=2*盘子数目-1&&startI!=3*盘子数目-1)(pointstartI+1.获取盘子().set上方有盘(false);pointstartI.set有盘子(false);elsepointstartI.set有盘子(false);elseif(pointendI+1.是否有盘子()=true)Disk tempDisk=pointendI+1.获取盘子();if(tempDisk.getNumber()-disk.getNumber()>=1)pointendI.放置盘子(disk,this
15、);if(startI!=盘子数目-1&&startI!=2*盘子数目-1&&startI!=3*盘子数目-1)(pointstartI+1.获取盘子().set上方有盘(false);pointstartI.set有盘子(false);tempDisk.set上方有盘(true);elsepointstartI.set有盘子(false);tempDisk.set上方有盘(true);elsedisk.setLocation(startX,startY);elsedisk.setLocation(startX,startY);if(disk!=null&
16、;&!containTowerPoint)disk.setLocation(startX,startY);public void mouseEntered(MouseEvent e)public void mouseExited(MouseEvent e)public void mouseClicked(MouseEvent e)public void 自动演示搬运盘子(int 盘子数,char one,char two,char three)if(盘子数=1)信息条.append(" "+one+"到:"+three+"塔n"
17、;);Disk disk=在塔中获取最上面的盘子(one);int startI=在塔中获取最上面盘子的位置(one);int endI=在塔中获取最上面盘子的上方位置(three);if(disk!=null)pointendI.放置盘子(disk,this);pointstartI.set有盘子(false);tryThread.sleep(1000);catch(Exception ee)else自动演示搬运盘子(盘子数-1,one,three,two);信息条.append(" "+one+"到:"+three+"塔n");D
18、isk disk=在塔中获取最上面的盘子(one);int startI=在塔中获取最上面盘子的位置(one);int endI=在塔中获取最上面盘子的上方位置(three);if(disk!=null)pointendI.放置盘子(disk,this);pointstartI.set有盘子(false);tryThread.sleep(1000);catch(Exception ee)自动演示搬运盘子(盘子数-1,two,one,three);public Disk 在塔中获取最上面的盘子(char 塔名)Disk disk=null;if(塔名=towerName0)for(int i=0
19、;i<盘子数目;i+)if(pointi.是否有盘子()=true)disk=pointi.获取盘子();break;if(塔名=towerName1)for(int i=盘子数目;i<2*盘子数目;i+)if(pointi.是否有盘子()=true)disk=pointi.获取盘子();break;if(塔名=towerName2)for(int i=2*盘子数目;i<3*盘子数目;i+)if(pointi.是否有盘子()=true)disk=pointi.获取盘子();break;return disk;public int 在塔中获取最上面盘子的上方位置(char 塔名
20、)int position=0;if(塔名=towerName0)int i=0;for(i=0;i<盘子数目;i+)if(pointi.是否有盘子()=true)position=Math.max(i-1,0);break;if(i=盘子数目)position=盘子数目-1;if(塔名=towerName1)int i=0;for(i=盘子数目;i<2*盘子数目;i+)if(pointi.是否有盘子()=true)position=Math.max(i-1,0);break;if(i=2*盘子数目)position=2*盘子数目-1;if(塔名=towerName2)int i=
21、0;for(i=2*盘子数目;i<3*盘子数目;i+)if(pointi.是否有盘子()=true)position=Math.max(i-1,0);break;if(i=3*盘子数目)position=3*盘子数目-1;return position;public int 在塔中获取最上面盘子的位置(char 塔名)int position=0;if(塔名=towerName0)int i=0;for(i=0;i<盘子数目;i+)if(pointi.是否有盘子()=true)position=i;break;if(i=盘子数目)position=盘子数目-1;if(塔名=towe
22、rName1)int i=0;for(i=盘子数目;i<2*盘子数目;i+)if(pointi.是否有盘子()=true)position=i;break;if(i=2*盘子数目)position=2*盘子数目-1;if(塔名=towerName2)int i=0;for(i=2*盘子数目;i<3*盘子数目;i+)if(pointi.是否有盘子()=true)position=i;break;if(i=3*盘子数目)position=3*盘子数目-1;return position;import javax.swing.*;import java.awt.*;import java
23、.awt.event.*;public class Tower extends Frame implements ActionListener,RunnableHannoiTower tower=null;Button renew,auto=null;char towerName='A','B','c'int 盘子数目,盘宽,盘高;Thread thread;TextArea 信息条=null;public Tower()thread=new Thread(this);盘子数目=5;盘宽=80;盘高=18;信息条=new TextArea(12,
24、12);信息条.setText(null);tower=new HannoiTower(盘子数目,盘宽,盘高,towerName,信息条);renew=new Button("重新开始");auto=new Button("自动演示搬盘子");renew.addActionListener(this);auto.addActionListener(this);add(tower,BorderLayout.CENTER);add(renew,BorderLayout.SOUTH);add(auto,BorderLayout.NORTH);add(信息条,B
25、orderLayout.EAST);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0););setVisible(true);setBounds(60,20,670,540);validate();public void actionPerformed(ActionEvent e)if(e.getSource()=renew)if(!(thread.isAlive()this.remove(tower);信息条.setText(null); tower=new HannoiTower(盘子数目,盘宽,盘高,towerName,信息条); add(tower,BorderLayout.CENTER); validate(); elseif(e.getSource()=auto)if(!
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 刀剪产品的品牌推广策略与执行计划考核试卷
- 猪的饲养智能化管理考核试卷
- 竹材加工过程中的质量控制考核试卷
- 皮革制品的消费心理与购买决策考核试卷
- 盾构机施工中的隧道工程地质环境保护策略考核试卷
- 厦门医学院《心理测量学》2023-2024学年第一学期期末试卷
- 无锡科技职业学院《人体工程学》2023-2024学年第二学期期末试卷
- 西昌民族幼儿师范高等专科学校《脑瘫儿童功能评估》2023-2024学年第一学期期末试卷
- 沈阳化工大学《语文》2023-2024学年第一学期期末试卷
- 沈阳职业技术学院《外国文学作品欣赏》2023-2024学年第二学期期末试卷
- 工程项目成本管理的案例分析
- 3.2工业区位因素及其变化以大疆无人机为例课件高一地理人教版
- 2024年陕西省中考数学试卷(A卷)附答案
- DL-T5190.1-2022电力建设施工技术规范第1部分:土建结构工程
- 财务预算分析表模板
- 中国高清荧光腹腔镜行业市场现状分析及竞争格局与投资发展研究报告2024-2034版
- 2024年高考体育单招考试政治重点知识点归纳总结(复习必背)
- MOOC 大数据技术原理与应用-厦门大学 中国大学慕课答案
- 国企管理人员招聘考试题库
- 高血压与青光眼的关系
- (2024版)小学二年级孩子如何高效复习语文知识点
评论
0/150
提交评论