汉诺塔 java 程序
更新时间:2024-01-25 13:26: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 程序01-25
一年级思想品德课教学总结05-31
毕业设计四柱万能液压机系统设计说明书10-09
C语言第1次上机03-17
1999年湖南省高中理科实验班联合招生考试数学试卷03-17
2013年河北省小学品德与生活(社会)学科教学设计评选01-14
合同信用制度04-24
大众文化消费结构调查03-15
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- 汉诺
- 程序
- java
- 政府和社会资本合作模式(PPP)-输变电及变电站工程项目可行性研究报告 - 图文
- 十二五妇女儿童规划
- 北语15春《计算机图形学》作业1满分答案
- 税收学复习资料
- 2019-2020年高三英语考前得分训练试题五
- 2.4维护系统与使用常用工具软件教案
- 人教版小学语文三年级下全册看拼音写词语基础练习
- 本我自我超我
- CNG加气站施工组织设计方案 doc1
- 2017年中国抗磨剂市场规模现状分析及十三五竞争战略研究报告(目录) - 图文
- 五精管理理论
- 《信息技术与小学综合实践活动学科课程的有效整合研究》开题报告
- 经济法案例整理 - 图文
- PHC预应力管桩施工方案
- 管式加热
- 江苏省阜宁县实验初级中学 七年级语文上学期第三次阶段试题苏教版
- 自流平环氧胶泥地面
- 检验用仪器设备管理规程 - 图文
- 电大 政府经济学期末复习
- 2017大连高考语文试题及双基试题