import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class HannoiTower extends JPanel implements MouseListener,MouseMotionListener
{
TowerPoint point[];
int x,y;
boolean move=false;
Disk 盘子[];
int startX,startY;
int startI ;
int 盘子数目=0;
int width,height;
char towerName[]={'A','B','C'};
TextArea 信息条=null;
public HannoiTower(int number,int w,int h,char[] name,TextArea text)
{
towerName=name;
盘子数目=number;
width=w;
height=h;
信息条=text;
setLayout(null);
addMouseListener(this);
addMouseMotionListener(this);
盘子= new Disk[盘子数目];
point=new TowerPoint[3*盘子数目];
int space=20;
for(int i=0;i<盘子数目;i++)
{
point[i]=new TowerPoint(40+width,100+space,false);
space=space+height;
}
space=20;
for(int i=盘子数目;i<2*盘子数目;i++)
{
point[i]=new TowerPoint(160+width,100+space,false);
space=space+height;
}
space=20;
for(int i=2*盘子数目;i<3*盘子数目;i++)
{
point[i]=new TowerPoint(280+width,100+space,false);
space=space+height;
}
int 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++)
{
point[i].放置盘子(盘子[i],this);
if(i>=1)
盘子[i].set上方有盘(true);
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawLine(point[0].getX(),point[0].getY(),
point[盘子数目-1].getX(),point[盘子数目-1].getY());
g.drawLine(point[盘子数目].getX(),point[盘子数目].getY(),
point[2*盘子数目-1].getX(),point[2*盘子数目-1].getY());
g.drawLine(point[2*盘子数目].getX(),point[2*盘子数目].getY(),
point[3*盘子数目-1].getX(),point[3*盘子数目-1].getY());
g.drawLine(point[盘子数目-1].getX()-width,point[盘子数目-1].getY(),
point[3*盘子数目-1].getX()+width,point[3*盘子数目-1].getY());
int leftx=point[盘子数目-1].getX()-width;
int lefty=point[盘子数目-1].getY();
int w=(point[3*盘子数目-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++)
{
g.fillOval(point[i].getX()-size/2,point[i].getY()-size/2,size,size);
}
g.drawString(""+towerName[0]+"塔",point[盘子数目-1].getX(),point[盘子数目-1].getY()+30);
g.drawString(""+towerName[1]+"塔",point[2*盘子数目-1].getX(),point[盘子数目-1].getY()+30);
g.drawString(""+towerName[2]+"塔",point[3*盘子数目-1].getX(),point[盘子数目-1].getY()+30);
g.drawString("将全部盘子从"+towerName[0]+"塔搬运到"+towerName[1]+"塔或"+towerName[2]+"塔",
point[盘子数目-1].getX(),point[盘子数目-1].getY()+80);
}
public void mousePressed(MouseEvent e)
{
Disk 盘子=null;
Rectangle rect=null;
if(e.getSource()==this)
move=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=point[i].getX();
int y=point[i].getY();
if(rect.contains(x,y))
{
startI=i;
break;
}
}
}
}
public void mouseMoved(MouseEvent e)
{
}
public void mouseDragged(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.getHeight()/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;
if(disk!=null)
{
for(int i=0;i<3*盘子数目;i++)
{
x=point[i].getX();
y=point[i].getY();
if(rect.contains(x,y))
{
containTowerPoint=true;
endI=i;
break;
}
}
}
if(disk!=null&&containTowerPoint)
{
if(point[endI].是否有盘子()==true)
{
disk.setLocation(startX,startY);
}
else
{
if(endI==盘子数目-1||endI==2*盘子数目-1||endI==3*盘子数目-1)
{
point[endI].放置盘子(disk,this);
if(startI!=盘子数目-1&&startI!=2*盘子数目-1&&startI!=3*盘子数目-1)
{
(point[startI+1].获取盘子()).set上方有盘(false);
point[startI].set有盘子(false);
}
else
{
point[startI].set有盘子(false);
}
}
else
{
if(point[endI+1].是否有盘子()==true)
{
Disk tempDisk=point[endI+1].获取盘子();
if((tempDisk.getNumber()-disk.getNumber())>=1)
{
point[endI].放置盘子(disk,this);
if(startI!=盘子数目-1&&startI!=2*盘子数目-1&&startI!=3*盘子数目-1)
{
(point[startI+1].获取盘子()).set上方有盘(false);
point[startI].set有盘子(false);
tempDisk.set上方有盘(true);
}
else
{
point[startI].set有盘子(false);
没有合适的资源?快使用搜索试试~ 我知道了~
实训课题:计算器、HANNO塔、学籍管理系统、考试系统、日历记事本
共20个文件
java:20个
需积分: 0 10 下载量 161 浏览量
2009-01-03
14:57:32
上传
评论
收藏 24KB RAR 举报
温馨提示
计算器、HANNO塔、学籍管理系统、考试系统、日历记事本
资源推荐
资源详情
资源评论
收起资源包目录
.rar (20个子文件)
HANNOI-塔
chap3
Disk.java 497B
Tower.java 2KB
HannoiTower.java 13KB
TowerPoint.java 868B
学籍管理系统
chap9
ModifySituation.java 8KB
Inquest.java 4KB
StudentManager.java 3KB
Delete.java 5KB
StudentSituation.java 6KB
Student.java 1KB
计算器
chap2
ComputerPad.java 12KB
NumberButton.java 320B
OperationButton.java 317B
日历记事本
chap8
NotePad.java 6KB
Year.java 2KB
CalendarPad.java 6KB
Month.java 2KB
标准化考试系统 (单机版)
chap5
EnglishTest.java 695B
TestArea.java 8KB
ReadTestquestion.java 4KB
共 20 条
- 1
资源评论
a862728
- 粉丝: 0
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功