package wxy;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Panel;
import java.sql.Time;
import java.util.Stack;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import com.sun.org.apache.bcel.internal.verifier.exc.StaticCodeConstraintException;
import java.util.Date;
public class HanoPanel extends JPanel{
// 定义南边,中间,北边的三个面板
JPanel northPanel;
JPanel centerPanel;
JPanel southPanel;
// 定义一个对卡片布局器的引用变量
CardLayout cardLayout;
//定义一个父面容器的引用变量
JPanel fatherPanel;
// level为初始化层数
int level=3;
// 步数
int step=0;
static int i=1;
// boolean型的变量,以判断是否退出时间线程
static boolean comp;
// 定义三个按钮重置,设置,撤消,退出
JButton exits;
JButton reset;
//JButton back;
JButton config;
// 用于存放计数器,时间
JLabel prompt,jlstep,jltime;
// 盘子数组,用jlabel来表示大小不同的盘子
JLabel[] dish;
// 存放盘子的子面板数组.hanoPanel[0]表示第一个面板数组,hanoPanel[10]表示第两个面板数组,hanoPanel[20]表示第三个面板数组
JPanel[] hanoPane;
// 定义处理鼠标事件的引用
HanoMouseEvent hme;
// 时间线程的引用
//HanoTimePanel htp;
// 带两个参数的构造方法,用于接收的父容器面板引用和卡片布局的引用
public HanoPanel(JPanel jp,CardLayout cl){
fatherPanel=jp;
cardLayout=cl;
createInterface();
}
// 构建游戏界面,使用边界布局器管理器
private void createInterface(){
this.setLayout(new BorderLayout());
createNorthPanel();
createCenterPanel();
createSouthPanel();
this.add(northPanel,BorderLayout.NORTH);
this.add(centerPanel,BorderLayout.CENTER);
this.add(southPanel,BorderLayout.SOUTH);
}
// 创建计时器
// protected void createHanotimepanel() {
// htp=new HanoTimePanel(this);
// }
//
// protected void hanoTimeclear(){
// htp.comp=true;
// }
//创建北部面板
private void createNorthPanel(){
northPanel=new JPanel();
northPanel.setLayout(new BorderLayout());
prompt=new JLabel("游戏开始,祝你成功!");
jlstep=new JLabel();
jltime=new JLabel();
jltime.setSize(120,20);
jltime.setPreferredSize(new Dimension(120,25));
jltime.setBackground(Color.white);
jltime.setOpaque(true);
jlstep.setSize(179,20);
jlstep.setPreferredSize(new Dimension(179,25));
jlstep.setBackground(Color.white);
jlstep.setOpaque(true);
prompt.setSize(450,20);
prompt.setPreferredSize(new Dimension(450,25));
prompt.setBackground(Color.white);
prompt.setOpaque(true);
northPanel.add(jlstep,BorderLayout.CENTER);
northPanel.add(prompt,BorderLayout.WEST);
northPanel.add(jltime,BorderLayout.EAST);
stepJian();
}
// 创建步数
protected void createStep() {
// TODO Auto-generated method stub
step++;
jlstep.setText("步数:"+step);
}
protected void stepJian(){
jlstep.setText("步数:"+step);
}
// 创建中间面板:塔柱面板
private void createCenterPanel(){
centerPanel=new JPanel();
centerPanel.setLayout(null);
centerPanel.setBorder(BorderFactory.createLineBorder(Color.blue));
centerPanel.setBackground(Color.white);
createHanoPane();
addHanoPane();
createDishes();
addDishes(level);
centerPanel.add(hanoPane[0]);
centerPanel.add(hanoPane[10]);
centerPanel.add(hanoPane[20]);
this.add(centerPanel);
//createHanotimepanel();
}
// 南部面板
private void createSouthPanel(){
southPanel=new JPanel();
southPanel.setLayout(new GridLayout(1,3));
exits=new JButton("返回");
reset=new JButton("重置");
config=new JButton("设置等级");
//back=new JButton("撤消");
HanoActionEvent haction=new HanoActionEvent(this, fatherPanel, cardLayout);
exits.addActionListener(haction);
reset.addActionListener(haction);
config.addActionListener(haction);
//back.addActionListener(haction);
southPanel.add(reset);
southPanel.add(config);
//southPanel.add(back);
southPanel.add(exits);
this.add(southPanel);
}
// 创建中间面板包含的30个面板
private void createHanoPane(){
int i;
hanoPane=new JPanel[30];
for(i=0;i<30;i++){
if(i%10==0){
hanoPane[i]=new JPanel();
hanoPane[i].setLayout(new GridLayout(9,1));
hanoPane[i].setBounds(i*20+60,40,120,300);
}
else{
hanoPane[i]=new JPanel();
hanoPane[i].setBorder(BorderFactory.createLineBorder(Color.blue,1));
hanoPane[i].setBackground(Color.white);
}
}
addMouseE();
}
// 添加北,中,南面板到布局的容器面板
private void addHanoPane(){
int i,index=0;
for(i=0;i<30;i++){
if(i%10==0){
index=i;
}
else{
hanoPane[index].add(hanoPane[i]);
}
}
}
// 创建9个盘子
private void createDishes(){
dish=new JLabel[9];
for(int i=0;i<9;i++){
dish[i]=new JLabel();
dish[i].setSize(i*10+10,20);
dish[i].setPreferredSize(new Dimension(i*10+10,20));
dish[i].setBackground(Color.blue);
dish[i].setOpaque(true);
}
}
// 添加盘子到塔柱面板中,N代表塔的序号
protected void addDishes(int level){
for(int i=9;i>9-level;i--){
hanoPane[i].add(dish[i-1]);
}
}
// 中间面板添加鼠标事件监听器
protected void addMouseE(){
hme = new HanoMouseEvent(this);
hanoPane[0].addMouseListener(hme);
hanoPane[10].addMouseListener(hme);
hanoPane[20].addMouseListener(hme);
}
}