JAVA图形界面程序汉诺塔演示程序代码.docx

上传人:b****5 文档编号:7244866 上传时间:2023-01-22 格式:DOCX 页数:15 大小:18.66KB
下载 相关 举报
JAVA图形界面程序汉诺塔演示程序代码.docx_第1页
第1页 / 共15页
JAVA图形界面程序汉诺塔演示程序代码.docx_第2页
第2页 / 共15页
JAVA图形界面程序汉诺塔演示程序代码.docx_第3页
第3页 / 共15页
JAVA图形界面程序汉诺塔演示程序代码.docx_第4页
第4页 / 共15页
JAVA图形界面程序汉诺塔演示程序代码.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

JAVA图形界面程序汉诺塔演示程序代码.docx

《JAVA图形界面程序汉诺塔演示程序代码.docx》由会员分享,可在线阅读,更多相关《JAVA图形界面程序汉诺塔演示程序代码.docx(15页珍藏版)》请在冰豆网上搜索。

JAVA图形界面程序汉诺塔演示程序代码.docx

JAVA图形界面程序汉诺塔演示程序代码

汉诺塔问题——JAVA实现

importjava.awt.*;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.awt.event.WindowAdapter;

importjava.awt.event.WindowEvent;

importjavax.swing.JDialog;

importjavax.swing.JLabel;

importjavax.swing.JPanel;

classDeskextendsPanel

{

LabeltopPanel,leftLeg,rightLeg,name;

intnum;//桌子上现有盘子的数量

publicinttopy=340,topx=80;//第一个个盘子在哪里放下

intmaxsize=12;

intinitialx=0,initialy=340;

intrecord[]=newint[12];

Desk(Strings)//构造函数

{

name=newLabel();

name.setText(s);//桌子名称

topPanel=newLabel();

topPanel.setBackground(Color.red);

leftLeg=newLabel();

rightLeg=newLabel();

this.setSize(180,160);//桌子面板大小

this.setLayout(null);

this.add(topPanel);

this.add(leftLeg);

this.add(rightLeg);

this.add(name);

this.setFont(newFont("宋体",Font.CENTER_BASELINE,16));

this.setForeground(Color.blue);

topPanel.setBounds(10,0,160,30);

leftLeg.setBackground(Color.red);

leftLeg.setBounds(35,30,30,50);

rightLeg.setBackground(Color.red);

rightLeg.setBounds(115,30,30,50);

name.setBounds(70,100,60,30);

for(inti=0;i

{

record[i]=-1;

}

}

publicvoidSetrecord(inty)//修改记录数组

{

record[num-1]=y;//记录最上面的盘子的序号

}

publicvoidDeleoneRecord()//当移走某个盘子时,置相应的数组值为-1

{

record[num-1]=-1;

}

publicvoidSetinitialxy(intx)//修改下一个盘子的盘放位置

{

initialx=x;

}

publicvoidSettopxy()

{

topx=initialx+num*5;

topy=initialy-num*10;

}

publicvoidSettopx(intx)

{

topx=initialx+x*5;

}

publicvoidclear()

{

num=0;

}

publicvoidaddone()

{

this.num++;

}

publicvoidsubone()

{

this.num--;

}

}

classMainPanelextendsJPanel

{

//

Deska,b,c;

intspeed;//移动速度

intdefaultnum=3;//初始盘子数

booleanchangnum;

intmaxsize=12;

Labelplate[]=newLabel[maxsize];

intbigestsize=140;

intplateHeight=10;

intinitialx=80;

intinitialy=340;

intmoveSpeed=0;//默认为快速完成

intsteps=0;//完成步数

publicTextFieldshowAnswer=newTextField("结果显示区");

MainPanel()

{

for(intk=0;k

{

plate[k]=newLabel();

}

a=newDesk("A座");

b=newDesk("B座");

c=newDesk("C座");

this.setLayout(null);

this.setSize(700,600);

this.setBackground(Color.orange);

this.add(a);

this.add(b);

this.add(c);

this.add(showAnswer);

a.setLocation(60,350);

b.setLocation(320,350);

c.setLocation(580,350);

showAnswer.setBounds(300,50,200,25);

showAnswer.setEditable(false);

this.proplate(defaultnum);//初始化时产生的默认数量的盘子

this.setSize(850,650);

this.setLayout(null);

a.Setinitialxy(80);

b.Setinitialxy(340);

c.Setinitialxy(600);

a.Settopxy();

b.Settopxy();

c.Settopxy();

changnum=false;

validate();

}

publicvoidChangeDefault(intx)

{

this.defaultnum=x;

changnum=true;

}

publicvoidRemoveAllPlate()

{

for(inti=0;i

{

this.remove(plate[i]);

}

}

publicvoidproplate(intsum)

{

showAnswer.setText("结果显示区");

steps=0;

if(changnum==true)

{

this.RemoveAllPlate();

a.clear();

b.clear();

c.clear();

a.Settopxy();

b.Settopxy();

c.Settopxy();

}

for(intsu=0;su

{

plate[su].setText(""+su);

plate[su].setBackground(Color.black);

plate[su].setForeground(Color.red);

plate[su].setVisible(true);

this.add(plate[su]);

a.addone();//A座盘子数加1

a.Setrecord(su);//修改桌子A的记录数组

plate[su].setBounds(initialx+su*5,initialy-su*plateHeight,bigestsize-10*su,plateHeight);

}

}

publicvoidhanoi(intn,Deskone,Desktwo,Deskthree)

{

if(n==1)

move(one,three);

else

{

hanoi(n-1,one,three,two);

move(one,three);

hanoi(n-1,two,one,three);

}

}

publicvoidmove(Deskone,Desktwo)

{

//首先实现移动最顶端的盘子

//要做的事情包括修改相应的桌子上的盘子数,及下个盘子要放的位置,还有更新记录数组。

intno;//正在搬运的盘子的序号

doublework;//取得盘子上的序号

//下面的工作是找到要移动的盘子的序号

intmovewhich;

movewhich=one.record[one.num-1];

//定制移动路线

//先向上运动到制高点

intx;

inty;

x=plate[movewhich].getX();

y=plate[movewhich].getY();

two.Settopx(movewhich);

intstepleftright=(two.topx-x)/Math.abs(two.topx-x);//

while(y!

=200)

{

y=y-1;

plate[movewhich].setLocation(x,y);

try{

Thread.sleep(moveSpeed);

}catch(InterruptedExceptione){

//TODO自动生成catch块

e.printStackTrace();

}

}

//水平方向运动

while(x!

=two.topx)

{

x=x+stepleftright;

plate[movewhich].setLocation(x,y);

//this.repaint();

try{

Thread.sleep(moveSpeed);

}catch(InterruptedExceptione){

//TODO自动生成catch块

e.printStackTrace();

}

}

while(y!

=two.topy)

{

y=y+1;

plate[movewhich].setLocation(x,y);

try{

Thread.sleep(moveSpeed);

}catch(InterruptedExceptione){

//TODO自动生成catch块

e.printStackTrace();

}

}

one.DeleoneRecord();//表示该盘子移走了

work=Double.parseDouble(plate[movewhich].getText());

no=(int)work;

System.out.print(no);

one.subone();//盘子数减少1

two.addone();//盘子数加1

two.Setrecord(no);//桌子2上的记录数组添加1个元素

one.Settopxy();//修改1座上下个盘子的位置

two.Settopxy();//修改2座......

//设计函数实现下面的语句

Stringstring="当前搬运动作为:

"+one.name.getText()+"--->"+two.name.getText();

showAnswer.setText(string);

steps++;

}

}

classMainFrameextendsFrame

{

MenuBarmenubar;

Menudisplay,help,speed,num;

MenuItemstart,quit,paush,declare,introdue,speed1,speed2,speed3,num1,num2,num3,num4,num5;

MenuActionRereplay=newMenuActionRe();

MainPanelmainpanel=newMainPanel();

MainFrame()

{

}

MainFrame(Strings)

{

super(s);

setTitle(s);

menubar=newMenuBar();

display=newMenu("演示");

help=newMenu("帮助");

start=newMenuItem("开始");

speed=newMenu("选择速度");

num=newMenu("盘子数量");

quit=newMenuItem("退出");

quit.addActionListener(replay);

paush=newMenuItem("暂停");

declare=newMenuItem("说明");

introdue=newMenuItem("介绍");

speed1=newMenuItem("快速完成");

speed2=newMenuItem("适中速度");

speed3=newMenuItem("较慢实现");

num1=newMenuItem("3");

num1.addActionListener(replay);

num2=newMenuItem("5");

num2.addActionListener(replay);

num3=newMenuItem("7");

num3.addActionListener(replay);

num4=newMenuItem("9");

num4.addActionListener(replay);

num5=newMenuItem("12");

num5.addActionListener(replay);

display.add(start);

start.addActionListener(replay);

display.add(speed);

display.add(num);

display.add(quit);

speed.add(speed1);

speed1.addActionListener(replay);

speed.add(speed2);

speed2.addActionListener(replay);

speed.add(speed3);

speed3.addActionListener(replay);

num.add(num1);

num.add(num2);

num.add(num3);

num.add(num4);

num.add(num5);

help.add(declare);

declare.addActionListener(replay);

help.add(introdue);

introdue.addActionListener(replay);

menubar.add(display);

menubar.add(help);

menubar.setFont(newFont("宋体",Font.BOLD,14));

setMenuBar(menubar);

this.add(mainpanel,BorderLayout.CENTER);

setVisible(true);

setBounds(200,50,850,650);

}

classMenuActionReimplementsActionListener

{

publicvoidactionPerformed(ActionEventevent){

//TODO自动生成方法存根

if(event.getSource()==quit)

{

System.exit(0);//退出

}elseif(event.getSource()==start)

{

if(mainpanel.a.num==0)

{

//说明已经搬完了,是要重新开始啦

mainpanel.proplate(mainpanel.defaultnum);

mainpanel.c.clear();

mainpanel.a.Settopxy();

mainpanel.b.Settopxy();

mainpanel.c.Settopxy();

}

mainpanel.hanoi(mainpanel.defaultnum,mainpanel.a,mainpanel.b,mainpanel.c);

mainpanel.showAnswer.setText("搬运结束,搬运次数为:

"+mainpanel.steps);

}elseif(event.getSource()==declare)

{

Help1dialog=newHelp1();

dialog.setVisible(true);

}elseif(event.getSource()==introdue)

{

Help2dialog=newHelp2();

dialog.setVisible(true);

}elseif(event.getSource()==num1)

{

mainpanel.ChangeDefault(3);

mainpanel.proplate(mainpanel.defaultnum);

}elseif(event.getSource()==num2)

{

mainpanel.ChangeDefault(5);

mainpanel.proplate(mainpanel.defaultnum);

}elseif(event.getSource()==num3)

{

mainpanel.ChangeDefault(7);

mainpanel.proplate(mainpanel.defaultnum);

}elseif(event.getSource()==num4)

{

mainpanel.ChangeDefault(9);

mainpanel.proplate(mainpanel.defaultnum);

}elseif(event.getSource()==num5)

{

mainpanel.ChangeDefault(12);

mainpanel.proplate(mainpanel.defaultnum);

}elseif(event.getSource()==speed1)

{

//快速完成

mainpanel.moveSpeed=0;

}elseif(event.getSource()==speed2)

{

//适中速度

mainpanel.moveSpeed=2;

}elseif(event.getSource()==speed3)

{

//较慢完成

mainpanel.moveSpeed=6;

}

}

}

}

classHelp1extendsJDialog{

JPanelpanel1=newJPanel();

JLabeljLabel1=newJLabel("

作者:

徐小龙(中国石油大学)
2010年11月20日

");

JLabeljLabel2=newJLabel();

GridLayoutgridLayout1=newGridLayout();

Help1(){

super(newMainFrame(),"程序说明",false);

setBounds(400,150,360,260);

setVisible(true);

jLabel2.setText("使用说明:


通过演示菜单来控制程序的运行,
可以选择不同的移动速度,
也可以定制盘子的数量。

");

panel1.add(jLabel1,BorderLayout.NORTH);

panel1.add(jLabel2,BorderLayout.CENTER);

setResizable(false);

getContentPane().add(panel1);

}

}

classHelp2extendsJDialog{

JPanelpanel1=newJPanel();

JLabeljLabel2=newJLabel();

GridLayoutgridLayout1=newGridLayout();

Help2(){

super(newMainFrame(),"汉诺塔问题介绍",false);

setBounds(400,150,360,260);

setVisible(true);

jLabel2.setText("汉诺塔问题:


有若干个大小各不相同的盘子,
有三个桌子A、B、C,开始时,
盘子全部位于A桌上,现要将
盘子从"+

"A桌上移动到C桌上,
过程中要保证大盘子不能放到
小盘子上面,且每次只能移动一个盘子。

");

panel1.add(jLabel2,BorderLayout.CENTER);

getContentPane().add(panel1);

}

}

publicclassHanoiDisply{

publicstat

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 研究生入学考试

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1