package com.pcy;
import java.awt.*;
import javax.swing.*;
public class HanoPanel extends JPanel {
@SuppressWarnings("unused")
private CardLayout cardLayout;
@SuppressWarnings("unused")
private JPanel fatherJPanel;
JPanel centerPanel;
JPanel northPanel;
JPanel southPanel;
HanoActionEvent hme;
int level = 3;
int N=45;
JButton exit;
JButton reset;
JButton config;
JLabel prompt;
JLabel[] dish = new JLabel[N/3-1];
JPanel[] hanoPane = new JPanel[N];
Grame_Size sd;
HanoMouseEvent html;
CountTime time;
JLabel timeJLabel;
public HanoPanel(JPanel fatherJPanel, CardLayout cardLayout) {
this.fatherJPanel = fatherJPanel;
this.cardLayout = cardLayout;
createInterPanel();
}
// 构建游戏界面,使用边界布局管理器
public void createInterPanel() {
southPanel = new JPanel();
northPanel = new JPanel();
centerPanel = new JPanel();
exit = new JButton("退出");
reset = new JButton("重设");
config = new JButton("设置等级");
prompt = new JLabel("开始游戏!! ");
timeJLabel = new JLabel("您所用的时间 0:0:0");
sd = new Grame_Size();
hme = new HanoActionEvent(this, fatherJPanel, cardLayout);
time = new CountTime(this);
createNorthPanel();
createHanoPane();
createDishes();
createCenterPanel();
createSouthPanel();
addToContainer(); // 添加北,中,南面板到边界布局的容器面板中
}
@SuppressWarnings("unused")
// 创建北边面板:状态栏面板
private void createNorthPanel() {
northPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
northPanel.setBackground(Color.white);
prompt.setSize(sd.Get_width(), 30);
prompt.setPreferredSize(new Dimension(300, 30));
timeJLabel.setPreferredSize(new Dimension(200, 30));
northPanel.add(prompt);
northPanel.add(timeJLabel);
}
@SuppressWarnings("unused")
// 创建中间面板:塔柱面板
private void createCenterPanel() {
addDishes(level);
addHanoPane();
addMouseE();
int index = 0;
for (int i = 0; i < N; i++) {
if (i % (N/3) == 0) {
index = i;
hanoPane[i].setLayout(new GridLayout(N/3-1, 1, 0, 0));
hanoPane[i].setBorder(BorderFactory
.createLineBorder(Color.blue));
} else {
hanoPane[i].setPreferredSize(new Dimension(150, 20));
hanoPane[index].add(hanoPane[i]);
}
}
}
@SuppressWarnings("unused")
// 创建南边面板:按钮面板
public void createSouthPanel() {
southPanel.setLayout(new GridLayout(0, 3, 5, 5));
reset.addActionListener(hme);
exit.addActionListener(hme);
config.addActionListener(hme);
southPanel.add(reset);
southPanel.add(config);
southPanel.add(exit);
}
@SuppressWarnings("unused")
// 创建中间面板包含的30个面板
private void createHanoPane() {
for (int i = 0; i < N; i++) {
hanoPane[i] = new JPanel();
hanoPane[i].setBorder(BorderFactory.createLineBorder(Color.green));
}
}
@SuppressWarnings("unused")
// 添加面板到中间面板中
private void addHanoPane() {
// centerPanel.setLayout(new GridLayout(0, 3, 5, 2));
centerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));
for (int i = 0; i < N; i++) {
if (i %( N/3) == 0) {
centerPanel.add(hanoPane[i]);
}
}
}
@SuppressWarnings("unused")
// 添加北,中,南面板到边界布局的容器面板中
private void addToContainer() {
this.setLayout(new BorderLayout());
this.add(northPanel, BorderLayout.NORTH);
this.add(southPanel, BorderLayout.SOUTH);
this.add(centerPanel, BorderLayout.CENTER);
}
@SuppressWarnings("unused")
// 创建N/3-1个盘子
private void createDishes() {
for (int i = 0; i < N/3-1; i++) {
dish[i] = new JLabel();
dish[i].setSize(i * 10 + 10, 10);
dish[i].setPreferredSize(new Dimension(i * 10 + 10, 10));
dish[i].setBackground(Color.blue);
dish[i].setOpaque(true);
}
}
@SuppressWarnings("unused")
// 添加盘子到塔柱面板中,n代表塔的序号
public void addDishes(int n) {
int count = n - 1;
for (int i = N/3-1; i > N/3-1 - n; i--) {
hanoPane[i].add(dish[count]);
count--;
}
}
// 从中间面板添加鼠标事件监听器
protected void addMouseE() {
html = new HanoMouseEvent(this);
hanoPane[0].addMouseListener(html);
hanoPane[N/3].addMouseListener(html);
hanoPane[N/3*2].addMouseListener(html);
}
// 从中间面板移走鼠标事件监听器
protected void removeMouseE() {
hanoPane[0].addMouseListener(html);
hanoPane[N/3].addMouseListener(html);
hanoPane[N/3*2].addMouseListener(html);
}
@SuppressWarnings("deprecation")
public void rest() {
int count = 0;
count = level - 1;
if (html.selectedJlabel != null) {
html.selectedJlabel = null;
}
for (int i = 0; i <N ; i++) {
if (i % (N/3)!= 0) {
hanoPane[i].removeAll();
hanoPane[i].updateUI();
}
}
for (int j = N/3-1; j > N/3-1 - level; j--) {
hanoPane[j].add(dish[count]);
dish[count].setBackground(Color.black);
dish[count].setOpaque(true);
count--;
}
html.mark = 0;
prompt.setText("移动步数 " + String.valueOf(html.mark));
prompt.updateUI();
if (time.isAlive()) {
try {
time.number = 0;
time.stop();
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
time = null;
time = new CountTime(this);
timeJLabel.setText("您所用的时间 0:0:0");
timeJLabel.updateUI();
}
}
评论0