package com.test;
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.Font;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class HanoPanel extends JPanel {
// 定义北边,中间,南边的三个面板
private JPanel nortPanel;
private JPanel centerPanel;
private JPanel southPanel;
// 定义卡片布局的引用变量
private CardLayout cardLayout;
// 定义一个对父面板容器的引用
private JPanel fatherPanel;
// 定义处理鼠标事件对象引用
HanoMouseEvent hme;
// 汉诺塔的层数,代表游戏的难度
int level = 3;
// 定义窗体上的按钮
private JButton exits;
private JButton reset;
private JButton config;
// 盘子数组,用jlabel标签演示盘子,jlabel标签的大小代表不同的盘子
JLabel[] dish = new JLabel[9];
JPanel[] hanoPane;
HanoPanel hanoPanel;
// 带两个参数的构造方法,用于接收传递的父容器面板引用和卡片布局的引用
public HanoPanel(JPanel jp, CardLayout cl) {
fatherPanel = jp;
cardLayout = cl;
createInterface();
}
// 构建游戏界面,使用边界布局管理器
private void createInterface() {
createNorthPanel();
createCenterPanel();
createSouthPanel();
this.setLayout(new BorderLayout());
this.setSize(649, 429);
// 添加北边,中间,南边的三个面板到主面板中
this.add(nortPanel, BorderLayout.NORTH);
this.add(centerPanel, BorderLayout.CENTER);
this.add(southPanel, BorderLayout.SOUTH);
this.setVisible(true);
}
private void createSouthPanel() {
// TODO Auto-generated method stub
southPanel = new JPanel();
southPanel.setLayout(new GridLayout(1, 3));
JButton exits = new JButton("退出");
JButton reset = new JButton("重置");
JButton config = new JButton("设置等级");
// shme=new HanoActionEvent(HanoPanel,fatherPanel,cardLayout);
HanoActionEvent ha = new HanoActionEvent(this, fatherPanel,
cardLayout);
exits.addActionListener(ha);
reset.addActionListener(ha);
config.addActionListener(ha);
southPanel.add(reset);
southPanel.add(config);
southPanel.add(exits);
}
private void createCenterPanel() {
// TODO Auto-generated method stub
centerPanel = new JPanel();
centerPanel.setBackground(Color.white);
centerPanel.setBorder(BorderFactory.createLineBorder(Color.black));
createHanoPane();
addHanoPane();
createDishes();
addDishes(level);
// this.setSize(649,329);
centerPanel.setLayout(null);
centerPanel.add(hanoPane[0]);
centerPanel.add(hanoPane[10]);
centerPanel.add(hanoPane[20]);
addMouseE();
}
private void addMouseE() {
// TODO Auto-generated method stub
hme = new HanoMouseEvent(this);
hanoPane[0].addMouseListener(hme);
hanoPane[10].addMouseListener(hme);
hanoPane[20].addMouseListener(hme);
}
public void addDishes(int n) {
// TODO Auto-generated method stub
for (int i = 9; i > 9 - n; i--) {
//hme = new HanoMouseEvent(this);
//hme.selectedJLabel=null;
dish[i-1].setBackground(Color.black);
hanoPane[i].add(dish[i - 1]);
}
}
public void createDishes() {
// TODO Auto-generated method stub
dish = new JLabel[9];
for (int i = 0; i < 9; i++) {
dish[i] = new JLabel();
dish[i].setSize(i * 15 + 10, 25);
dish[i].setPreferredSize(new Dimension(i * 15 + 10, 25));
dish[i].setBackground(Color.black);
dish[i].setOpaque(true);
}
}
private void createHanoPane() {
// TODO Auto-generated method stub
hanoPane = new JPanel[30];
for (int 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 + 30, 30, 150, 300);
hanoPane[i].setOpaque(true);
} else {
hanoPane[i] = new JPanel();
hanoPane[i].setBackground(Color.white);
hanoPane[i].setBorder(BorderFactory.createLineBorder(Color.black));
}
}
}
private void addHanoPane() {
// TODO Auto-generated method stub
int index = 0;
for (int i = 0; i < 30; i++) {
if (i % 10 == 0) {
index = i;
} else {
hanoPane[index].add(hanoPane[i]);
}
}
}
private void createNorthPanel() {
nortPanel = new JPanel();
nortPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel jlshow = new JLabel("开始游戏,祝你好运!!");
jlshow.setOpaque(true);
jlshow.setFont(new Font("黑体", 20, 20));
nortPanel.add(jlshow);
}
}