汉诺塔 java 程序
更新时间:2024-04-15 19:31:01 阅读量: 综合文库 文档下载
- 汉诺塔怎么玩推荐度:
- 相关推荐
汉诺塔 java 程序 import java.awt.*;
import java.awt.event.*; import javax.swing.*;
public class AutoMoveDisc extends JDialog implements ActionListener{ int amountOfDisc=3;
TowerPoint [] pointA,pointB,pointC; char [] towerName; Container con;
StringBuffer moveStep; JTextArea showStep;
JButton bStart,bStop,bContinue,bClose; Timer time;
int i=0,number=0;
AutoMoveDisc(Container con){ setModal(true);
setTitle(\自动演示搬盘子过程\ this.con=con;
moveStep=new StringBuffer(); time=new Timer(1000,this); time.setInitialDelay(10);
showStep=new JTextArea(10,12); bStart=new JButton(\演示\ bStop=new JButton(\暂停\ bContinue=new JButton(\继续\ bClose=new JButton(\关闭\ bStart.addActionListener(this); bStop.addActionListener(this); bContinue.addActionListener(this); bClose.addActionListener(this); JPanel south=new JPanel();
south.setLayout(new FlowLayout()); south.add(bStart); south.add(bStop); south.add(bContinue); south.add(bClose);
add(new JScrollPane(showStep),BorderLayout.CENTER); add(south,BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); towerName=new char[3];
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){ time.stop();
setVisible(false);
} }); }
public void setPointA(TowerPoint [] pointA){ this.pointA=pointA; }
public void setPointB(TowerPoint [] pointB){ this.pointB=pointB; }
public void setPointC(TowerPoint [] pointC){ this.pointC=pointC; }
public void setTowerName(char name[]){
if(name[0]==name[1]||name[0]==name[2]||name[1]==name[2]){ towerName[0]='A'; towerName[1]='B'; towerName[2]='C'; }
else
towerName=name; }
public void setAmountOfDisc(int n){ amountOfDisc=n; }
public void actionPerformed(ActionEvent e) { if(e.getSource()==time){ number++;
char cStart,cEnd;
if(i<=moveStep.length()-2){ cStart=moveStep.charAt(i); cEnd=moveStep.charAt(i+1);
showStep.append(\从\座搬一个盘子到\座\\n\ autoMoveDisc(cStart,cEnd); } i=i+2;
if(i>=moveStep.length()-1){ time.stop(); } }
else if(e.getSource()==bStart){ if(moveStep.length()==0){ if(time.isRunning()==false){ i=0;
moveStep=new StringBuffer();
setMoveStep(amountOfDisc,towerName[0],towerName[1],towerName[2]); number=0;
time.start(); } } }
else if(e.getSource()==bStop){ if(time.isRunning()==true) time.stop(); }
else if(e.getSource()==bContinue){ if(time.isRunning()==false) time.restart(); }
else if(e.getSource()==bClose){ time.stop();
setVisible(false); } }
private void setMoveStep(int amountOfDisc,char one,char two,char three){ if(amountOfDisc==1){
moveStep.append(one);
moveStep.append(three); } else{
setMoveStep(amountOfDisc-1,one,three,two); moveStep.append(one); moveStep.append(three);
setMoveStep(amountOfDisc-1,two,one,three); } }
private void autoMoveDisc(char cStart,char cEnd){ Disc disc=null;
if(cStart==towerName[0]){
for(int i=0;i if(cStart==towerName[1]){ for(int i=0;i if(pointB[i].isHaveDisc()==true){ disc=pointB[i].getDiscOnPoint(); pointB[i].setHaveDisc(false); break; } } } if(cStart==towerName[2]){ for(int i=0;i TowerPoint endPoint=null; int i=0; if(cEnd==towerName[0]){ for(i=0;i if(pointA[i].isHaveDisc()==true){ if(i>0){ endPoint=pointA[i-1]; break; } else if(i==0) break; } } if(i==pointA.length) endPoint=pointA[pointA.length-1]; } if(cEnd==towerName[1]){ for(i=0;i if(pointB[i].isHaveDisc()==true){ if(i>0){ endPoint=pointB[i-1]; break; } else if(i==0) break; } } if(i==pointB.length) endPoint=pointB[pointB.length-1]; } if(cEnd==towerName[2]){ for(i=0;i if(pointC[i].isHaveDisc()==true){ if(i>0){ endPoint=pointC[i-1]; break; } else if(i==0) break; } } if(i==pointC.length) endPoint=pointC[pointC.length-1]; } if(endPoint!=null&&disc!=null){ endPoint.putDisc(disc,con); endPoint.setHaveDisc(true); } } } import javax.swing.*; import java.awt.*; public class Disc extends JButton{ int number; TowerPoint point; Disc(){ setBackground(Color.cyan); } public void setNumber(int n){ number=n; } public int getNumber(){ return number; } public void setPoint(TowerPoint p){ point=p; } public TowerPoint getPoint(){ return point; } } import java.awt.event.*; import java.awt.*; public class HandleMouse implements MouseListener,MouseMotionListener { TowerPoint [] pointA,pointB,pointC; TowerPoint startPoint=null,endPoint=null; int leftX,leftY,x0,y0; boolean move=false,countTime=false; Container con; HandleMouse(Container con){ this.con=con; } public void setPointA(TowerPoint [] pointA){ this.pointA=pointA; } public void setPointB(TowerPoint [] pointB){ this.pointB=pointB; } public void setPointC(TowerPoint [] pointC){ this.pointC=pointC; } public void mousePressed(MouseEvent e){ move=false; Disc disc=null; disc=(Disc)e.getSource(); startPoint=disc.getPoint(); x0=e.getX(); y0=e.getY(); int m=0; for(int i=0;i if(m>0&&(pointA[m-1].isHaveDisc()==false)){ move=true; break; } else if(m==0){ move=true; break; } } } for(int i=0;i if(m>0&&(pointB[m-1].isHaveDisc()==false)){ move=true; break; } else if(m==0){ move=true; break; } } } for(int i=0;i if(m>0&&(pointC[m-1].isHaveDisc()==false)){ move=true; break; } else if(m==0){ move=true; break; } } } } public void mouseMoved(MouseEvent e){} public void mouseDragged(MouseEvent e){ Disc disc=null; disc=(Disc)e.getSource(); leftX=disc.getBounds().x; leftY=disc.getBounds().y; int x=e.getX(); int y=e.getY(); leftX=leftX+x; leftY=leftY+y; if(move==true) disc.setLocation(leftX-x0,leftY-y0); } public void mouseReleased(MouseEvent e){ Disc disc=null; disc=(Disc)e.getSource(); Rectangle rect=disc.getBounds(); boolean location=false; int x=-1,y=-1; for(int i=0;i if(i==pointA.length-1&&endPoint.isHaveDisc()==false){ location=true; break; } else if(i &&pointA[i+1].getDiscOnPoint().getNumber() >disc.getNumber()){ location=true; break; } } } for(int i=0;i if(i==pointB.length-1&&endPoint.isHaveDisc()==false){ location=true; break; } else if(i &&pointB[i+1].getDiscOnPoint().getNumber() >disc.getNumber()){ location=true; break; } } } for(int i=0;i y=pointC[i].getY(); if(rect.contains(x,y)){ endPoint=pointC[i]; if(i==pointC.length-1&&endPoint.isHaveDisc()==false){ location=true; break; } else if(i &&pointC[i+1].getDiscOnPoint().getNumber() >disc.getNumber()){ location=true; break; } } } if(endPoint!=null&&location==true){ endPoint.putDisc(disc,con); startPoint.setHaveDisc(false); } else startPoint.putDisc(disc,con); } public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mouseClicked(MouseEvent e){} } import javax.swing.*; import java.awt.*; import java.awt.event.*; public class HannoiWindow extends JFrame implements ActionListener{ Tower tower=null; int amountOfDisc=3; char []towerName={'A','B','C'}; JMenuBar bar; JMenu menuGrade; JMenuItem oneGradeItem,twoGradeItem,threeGradeItem; JButton renew=null; JButton autoButton=null; JPanel center=new JPanel(); HannoiWindow(){ tower=new Tower(towerName); tower.setAmountOfDisc(amountOfDisc); tower.setMaxDiscWidth(120); tower.setMinDiscWidth(50); tower.setDiscHeight(16); tower.putDiscOnTower(); add(tower,BorderLayout.CENTER); bar=new JMenuBar(); menuGrade=new JMenu(\选择级别\ oneGradeItem=new JMenuItem(\初级\ twoGradeItem=new JMenuItem(\中级\ threeGradeItem=new JMenuItem(\高级\ menuGrade.add(oneGradeItem); menuGrade.add(twoGradeItem); menuGrade.add(threeGradeItem); bar.add(menuGrade); setJMenuBar(bar); oneGradeItem.addActionListener(this); twoGradeItem.addActionListener(this); threeGradeItem.addActionListener(this); renew=new JButton(\重新开始\ renew.addActionListener(this); autoButton=new JButton(\自动演示\ autoButton.addActionListener(this); JPanel north=new JPanel(); north.add(renew); north.add(autoButton); String mess=\将全部盘子从\座搬运到\ \座或\座\ JLabel hintMess=new JLabel(mess,JLabel.CENTER); north.add(hintMess); add(north,BorderLayout.NORTH); setResizable(false); setVisible(true); setBounds(60,60,460,410); validate(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e){ if(e.getSource()==oneGradeItem){ amountOfDisc=3; tower.setAmountOfDisc(amountOfDisc); tower.putDiscOnTower(); } else if(e.getSource()==twoGradeItem){ amountOfDisc=4; tower.setAmountOfDisc(amountOfDisc); tower.putDiscOnTower(); } else if(e.getSource()==threeGradeItem){ amountOfDisc=5; tower.setAmountOfDisc(amountOfDisc); tower.putDiscOnTower(); } else if(e.getSource()==renew){ tower.setAmountOfDisc(amountOfDisc); tower.putDiscOnTower(); } else if(e.getSource()==autoButton){ tower.setAmountOfDisc(amountOfDisc); tower.putDiscOnTower(); int x=this.getBounds().x+this.getBounds().width; int y=this.getBounds().y; tower.getAutoMoveDisc().setLocation(x,y); tower.getAutoMoveDisc().setSize(280,this.getBounds().height); tower.getAutoMoveDisc().setVisible(true); } validate(); } public static void main(String args[]){ new HannoiWindow(); } } import javax.swing.*; import java.awt.*; public class Tower extends JPanel{ int amountOfDisc=3; Disc [] disc; int maxDiscWidth,minDiscWidth,discHeight; char [] towerName; TowerPoint [] pointA,pointB,pointC; HandleMouse handleMouse; AutoMoveDisc autoMoveDisc; Tower(char [] towerName){ handleMouse=new HandleMouse(this); this.towerName=towerName; setLayout(null); setBackground(new Color(200,226,226)); } public void setAmountOfDisc(int number){ if(number<=1) amountOfDisc=1; else amountOfDisc=number; } public void setMaxDiscWidth(int m){ maxDiscWidth=m; } public void setMinDiscWidth(int m){ minDiscWidth=m; } public void setDiscHeight(int h){ discHeight=h; } public AutoMoveDisc getAutoMoveDisc(){ return autoMoveDisc; } public void putDiscOnTower(){ removeDisk(); int n=(maxDiscWidth-minDiscWidth)/amountOfDisc; disc=new Disc[amountOfDisc]; for(int i=0;i int diskwidth=minDiscWidth+i*n; disc[i].setSize(diskwidth,discHeight); disc[i].addMouseListener(handleMouse); disc[i].addMouseMotionListener(handleMouse); } pointA=new TowerPoint[amountOfDisc]; pointB=new TowerPoint[amountOfDisc]; pointC=new TowerPoint[amountOfDisc]; int vertialDistance=discHeight; for(ii pointA[i]=new TowerPoint(maxDiscWidth,100+vertialDistance); vertialDistance=vertialDistance+discHeight; } vertialDistance=discHeight; for(int i=0;i pointB[i]=new TowerPoint(2*maxDiscWidth,100+vertialDistance); vertialDistance=vertialDistance+discHeight; } vertialDistance=discHeight; for(int i=0;i pointC[i]=new TowerPoint(3*maxDiscWidth,100+vertialDistance); vertialDistance=vertialDistance+discHeight; } for(int i=0;i handleMouse.setPointA(pointA); handleMouse.setPointB(pointB); handleMouse.setPointC(pointC); autoMoveDisc=new AutoMoveDisc(this); autoMoveDisc.setTowerName(towerName); autoMoveDisc.setAmountOfDisc(amountOfDisc); autoMoveDisc.setPointA(pointA); autoMoveDisc.setPointB(pointB); autoMoveDisc.setPointC(pointC); validate(); repaint(); } public void removeDisk(){ if(pointA!=null){ for(int i=0;i pointA[i].removeDisc(pointA[i].getDiscOnPoint(),this); pointB[i].removeDisc(pointB[i].getDiscOnPoint(),this); pointC[i].removeDisc(pointC[i].getDiscOnPoint(),this); } } } public void paintComponent(Graphics g){ super.paintComponent(g); int x1,y1,x2,y2; x1=pointA[0].getX(); y1=pointA[0].getY()-discHeight/2; x2=pointA[amountOfDisc-1].getX(); y2=pointA[amountOfDisc-1].getY()+discHeight/2; g.drawLine(x1,y1,x2,y2); x1=pointB[0].getX(); y1=pointB[0].getY()-discHeight/2; x2=pointB[amountOfDisc-1].getX(); y2=pointB[amountOfDisc-1].getY()+discHeight/2; g.drawLine(x1,y1,x2,y2); x1=pointC[0].getX(); y1=pointC[0].getY()-discHeight/2; x2=pointC[amountOfDisc-1].getX(); y2=pointC[amountOfDisc-1].getY()+discHeight/2; g.drawLine(x1,y1,x2,y2); g.setColor(Color.blue); x1=pointA[amountOfDisc-1].getX()-maxDiscWidth/2; y1=pointA[amountOfDisc-1].getY()+discHeight/2; x2=pointC[amountOfDisc-1].getX()+maxDiscWidth/2; y2=pointC[amountOfDisc-1].getY()+discHeight/2; int length=x2-x1,height=6; g.fillRect(x1,y1,length,height); int size=5; for(int i=0;i g.drawString(towerName[0]+\座\ pointA[amountOfDisc-1].getX(),pointA[amountOfDisc-1].getY()+50); g.drawString(towerName[1]+\座\ pointB[amountOfDisc-1].getX(),pointB[amountOfDisc-1].getY()+50); g.drawString(towerName[2]+\座\ pointC[amountOfDisc-1].getX(),pointC[amountOfDisc-1].getY()+50); } } import java.awt.*; public class TowerPoint{ int x,y; boolean haveDisc; Disc disc=null; public TowerPoint(int x,int y){ this.x=x; this.y=y; } public boolean isHaveDisc(){ return haveDisc; } public void setHaveDisc(boolean boo){ haveDisc=boo; } public int getX(){ return x; } public int getY(){ return y; } public boolean equals(TowerPoint p){ if(p.getX()==this.getX()&&p.getY()==this.getY()) return true; else return false; } public void putDisc(Component com,Container con){ disc=(Disc)com; con.setLayout(null); con.add(disc); int w=disc.getBounds().width; int h=disc.getBounds().height; disc.setBounds(x-w/2,y-h/2,w,h); haveDisc=true; disc.setPoint(this); con.validate(); } public Disc getDiscOnPoint(){ return disc; } public void removeDisc(Component com,Container con){ if(com!=null) con.remove(com); con.validate(); } }
正在阅读:
汉诺塔 java 程序04-15
2017年士兵提干考试之常识植物:故宫里的古树名木03-18
密码虽密,颇有规律12-28
三行情书寄心语活动策划书12-19
乡镇街道矛盾纠纷调处中心流程图08-31
北京中医药大学远程教育学院中医基础理论Z1-11次作业10-08
计算PCB铜箔通流能力软件的介绍及其应用03-17
地藏禅寺因果教育实录(圣云法师讲因果)01-15
苯和甲苯气相色谱实验条件的考察05-03
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 汉诺
- 程序
- java
- 数字水印技术简介与应用
- 北语15春《计算机图形学》作业1满分答案
- 税收学复习资料
- CNG加气站施工组织设计方案 doc1
- 新概念一93-94课练习详解
- 制度文件汇编 - 图文
- 政府和社会资本合作模式(PPP)-输变电及变电站工程项目可行性研究
- 经济法案例整理 - 图文
- 关于加强保险业突发事件应急管理工作的通知
- 四川省自贡市富顺县2018-2019学年下学期八年级数学六校联考第一
- 2014步步高大一轮复习讲义第十单元 第39讲胚胎工程(免费下载)
- 最新人教版八年级物理下册单元测试题及答案全套B
- 2017年中国抗磨剂市场规模现状分析及十三五竞争战略研究报告(目
- 江苏省阜宁县实验初级中学 七年级语文上学期第三次阶段试题苏教
- 废旧钢材买卖协议
- 中国文化史标准答案答案
- 检验用仪器设备管理规程 - 图文
- 电大 政府经济学期末复习
- “十三五”重点项目-高纯乙炔生产建设项目申请报告
- 基于单片机的直流调速系统的设计毕业论文