汉诺塔 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(); } }

本文来源:https://www.bwwdw.com/article/qaep.html

Top