import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.text.StyledEditorKit.ForegroundAction;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.RepaintManager;
public class DrawPanel extends JPanel
{
ArrayList<Plate> plates=new ArrayList<Plate>();
static Pillar[] pillars=new Pillar[3];
Hand hand=new Hand(1);
int chosen;
int n;
boolean init;
int step;
static{
for(int i=0;i<3;i++)
{
pillars[i]=new Pillar(i+1);
}
}
DrawPanel(int a )
{
init =true;
this.refresh(a);
}
//刷新
public void refresh(int a)
{
n=a;
int i;
cleanUp();
Plate[] temp=new Plate[n];
for(i=1;i<n+1;i++){
temp[i-1]=new Plate(i);
temp[i-1].pillarid=1;
temp[i-1].position=a-i+1;
pillars[0].count++;
pillars[0].plateid[temp[i-1].position]=temp[i-1].id;
plates.add(temp[i-1]);
}
if(!init)
{
repaint();
}else
{
init=false;
}
}
//清空
public void cleanUp()
{
int i,j;
step=0;
hand.pillarid=1;
hand.empty=true;
plates.clear();//清除arraylist
for(i=0;i<3;i++)
{
for(j=0;j<pillars[i].count;j++)
{
pillars[i].plateid[j+1]=0;
}
pillars[i].count=0;
}
}
//绘图
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int x=getWidth();
int y=getHeight();
int[] a = {hand.pillarid*x/4-10,hand.pillarid*x/4+10,hand.pillarid*x/4,hand.pillarid*x/4-10};
int[] b = {10,10,20,10};
String[] string = new String[]{"一","二","三","四","五","六","七"};
int i;
for(i=1;i<4;i++) {
g.drawRoundRect(i*x/4-3,y-240,6,250,2,2);
}
for(i=0;i<n;i++) {
g.fillRoundRect(plates.get(i).pillarid*x/4-plates.get(i).width/2,y-10*plates.get(i).position,plates.get(i).width,10,2,2);
}
g.drawPolyline(a,b,a.length);
g.drawString("步数:"+step,10,20);
g.drawString("第"+string[n-3]+"关",350,20);
}
//判断超过步数
public void judge()
{
int h = (int)(((Math.pow(2,n)-1)*2-1)*1.5);
if(step == h) {
JOptionPane.showMessageDialog(null,"请找东哥教你!");
refresh(3);
}
}
public void getPlate()
{
if(hand.empty)
{
int m;
if(pillars[hand.pillarid-1].count != 0) {
m = pillars[hand.pillarid-1].plateid[pillars[hand.pillarid-1].count];
plates.get(m-1).position = 25;
pillars[hand.pillarid-1].plateid[pillars[hand.pillarid-1].count] = 0;
pillars[hand.pillarid-1].count--;
chosen = m;
hand.empty = false;
repaint();
}
}
}
public void setPlate()
{
if(!hand.empty) {
int m;
m = pillars[hand.pillarid-1].plateid[pillars[hand.pillarid-1].count];
if(chosen < m||m == 0) {
plates.get(chosen-1).position = pillars[hand.pillarid-1].count+1;
pillars[hand.pillarid-1].plateid[pillars[hand.pillarid-1].count+1] = chosen;
pillars[hand.pillarid-1].count++;
chosen = 0;
hand.empty = true;
step++;
repaint();
if(pillars[2].count == n) {
if(n == 9) {
JOptionPane.showMessageDialog(null,"你完成了所有关卡!");
System.exit(0);
}
JOptionPane.showMessageDialog(null,"不错,东哥在第九关等着你");
n++;
refresh(n);
}
}
}
}
//向右移动
public void movePlateRright()
{
hand.pillarid = hand.pillarid%3+1;
if(chosen != 0) {
plates.get(chosen-1).pillarid = plates.get(chosen-1).pillarid %3+1;
}
repaint();
judge();
}
//向左移动
public void movePlateLeft()
{
hand.pillarid = (hand.pillarid+1)%3+1;
if(chosen != 0) {
plates.get(chosen-1).pillarid = (plates.get(chosen-1).pillarid+1)%3+1;
}
repaint();
judge();
}
}
汉诺塔界面实现
需积分: 10 143 浏览量
2016-08-15
09:19:28
上传
评论
收藏 15KB ZIP 举报
鱼香土豆丝
- 粉丝: 124
- 资源: 9
最新资源
- Integrated-Energy-Systems-with-CAES-(注释完全,可直接运行)
- PDF为英语文本绘制热区(DEMO)
- 4.22.cpp
- 基于Transformer和Bert的close domain抽取式问答系统构建的智能聊天机器人项目源代码
- 基于扩展(EKF)和无迹卡尔曼滤波(UKF)的电力系统动态状态估计(注释完全,可直接运行)(文档加Matlab源码)
- 2023各大软件技术峰会演进资料汇总(PPT),资料难得
- 基于混沌集成决策树的电能质量复合扰动识别(注释完全,可直接运行)(文档加Matlab源码)
- 航空公司如何成功实现数字化转型英文版.rar
- RTL8309N-8口交换机评估板Cadence设计硬件(原理图+PCB)及PDF原理图+RTL8309N技术规格书
- 基于JAVA的打飞机游戏设计(程序).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0