📄 centralpanel.java
字号:
package view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import model.Model;
import login.Sound;
/**
*
* @author 何晓飞 李强 何明
* @version 2.0 本类采用了严格的监听模式以动态的跟踪程序内部产生的事件 同时本类整体是一个panel以方便得添加至initPanel
*/
public class CentralPanel extends JPanel implements Observer {
private static final long serialVersionUID = 1L;
/* 背景声音 */
private Sound bgSound;
/* 游戏中的音乐 */
private String gameMusic;
/*音乐下拉菜单里显示的内容*/
private String[] mStrings={"背景音乐","泰坦尼克","柠檬树","奇幻旋律","梦中婚礼"};
/* 6个JButton 分别用来控制游戏的暂停,关于,提示,演示,开始,退出 */
private JButton btnifo, btnPause;
private JButton btnRemind, btnDemo, btnStart, btnQuit;
/*音乐下拉菜单*/
private JComboBox cbMusic;
/*控制音乐的开关*/
private JButton btnSound;
/* GamePanel实例 */
private GamePanel gPanel;
/* 3个JLabel分别显示历史纪录,剩余可消去图片对数和剩余时间 */
private JLabel lblHistory;
private JLabel lblLeave;
private JLabel lblTimer;
/* 显示当前关数 */
private JLabel lblLevel;
/*显示得分*/
private JLabel lblScore;
/*游戏运行模式*/
private Model model;
/* 操作面板 */
private JPanel setPanel;
/* 状态面板 */
private JPanel statPanel;
/**
* CentralPanel构造函数初始化并添加各个组件,同时自定义鼠标
*/
public CentralPanel(Model model) {
this.model = model;
// 初始化
gameMusic="梦中婚礼.mid";
//游戏中的背景音乐
bgSound = new Sound(gameMusic);
//循环播放
bgSound.play(2);
initComponents();
// 添加各轻量级组件
setPanel.add(btnifo);// 添加btnifo按钮
setPanel.add(btnStart);// 田间btnStart按钮
setPanel.add(btnRemind);// 添加btnRemind按钮
setPanel.add(btnDemo);// 添加btnDemo按钮
setPanel.add(btnPause);// 添加btnPause按钮
setPanel.add(btnQuit);// 添加btnQuit按钮
setPanel.add(cbMusic);//添加cbMusic下拉菜单
setPanel.add(btnSound);//添加btnSound按钮
setPanel.setBackground(Color.DARK_GRAY);// 设置setPanel背景为深灰色
statPanel.add(lblLevel);//添加lblLevel 标签
statPanel.add(lblScore);//添加lblTimer 标签
statPanel.add(lblTimer);// 添加lblTimer标签
statPanel.add(lblLeave);// 添加lblLeave标签
statPanel.add(lblHistory);// 添加lblHistory标签
statPanel.setBackground(Color.DARK_GRAY);// 设置statPanel背景色为深灰色
// 设置大小
setSize(820, 553);
// 设置布局管理器
setLayout(new BorderLayout());
// 设置各Panel方位
add(setPanel, BorderLayout.NORTH);// setPanel占据北边
add(gPanel, BorderLayout.CENTER);// 个Panel占据中间
add(statPanel, BorderLayout.SOUTH);// statPanel占据南边
// 自定义鼠标
try {
setCursor(Toolkit.getDefaultToolkit().createCustomCursor(
ImageLoader.getCursorImage(), new Point(0, 0), "cursor"));
} catch (Exception e) {
e.printStackTrace();
}
// 显示关数
}
/**
*
* @return btnifo 按钮
*/
public JButton getBtnifo() {
return btnifo;
}
/**
*
* @return btnRemind 按钮
*/
public JButton getBtnRemind() {
return btnRemind;
}
/**
*
* @return btnDemo 按钮
*/
public JButton getBtnDemo() {
return btnDemo;
}
/**
*
* @return btnStart按钮
*/
public JButton getBtnStart() {
return btnStart;
}
/**
*
* @return btnPause 按钮
*/
public JButton getBtnPause() {
return btnPause;
}
/**
*
* @return btnQuit 按钮
*/
public JButton getBtnQuit() {
return btnQuit;
}
/**
*
* @return lblLevel 标签
*/
public JLabel getLblLevel() {
return lblLevel;
}
/**
*
* @return btnSound
*/
public JButton getBtnSound(){
return btnSound;
}
/**
*
* @return gPanel 面板
*/
public JPanel getGPanel() {
return gPanel;
}
// 给不同的对象添加不同的监听
public void addController(Object obj) {
// gPanel添加MouseListener监听类
if (obj instanceof MouseListener) {
gPanel.addMouseListener((MouseListener) obj);
}
// 给btnifo,btnStart,btnRemind,btnDemo,添加ActionListener监听类
if (obj instanceof ActionListener) {
ActionListener al = (ActionListener) obj;
btnifo.addActionListener(al);// 添加监听
btnStart.addActionListener(al);// 添加监听
btnRemind.addActionListener(al);// 添加监听
btnDemo.addActionListener(al);// 添加监听
btnPause.addActionListener(al);// 添加监听
btnQuit.addActionListener(al);// 添加监听
btnSound.addActionListener(al);// 添加监听
cbMusic.addActionListener(al);// 添加监听
}
}
protected ImageIcon createImageIcon(String path) {
// 获取URL
java.net.URL imgURL = CentralPanel.class.getResource(path);
if (imgURL != null) {
// 返回图标
return new ImageIcon(imgURL);
} else {
// 出错信息
System.err.println("fail to load file:" + path);
return null;
}
}
// 初始化各组件
private void initComponents() {
// 初始化各组件,设置字体,ActionCommand
lblTimer = new JLabel();
lblTimer.setForeground(new Color(34, 139, 34));
lblLeave = new JLabel();
lblLeave.setForeground(new Color(34, 139, 34));
lblHistory = new JLabel();
lblHistory.setForeground(new Color(34, 139, 34));
lblLevel = new JLabel();//
lblLevel.setSize(60,20);
lblLevel.setForeground(new Color(238, 59, 59));
lblScore=new JLabel();
lblScore.setForeground(new Color(238, 80, 34));
// 设置字体
lblTimer.setFont(new Font("serif", Font.BOLD, 16));
lblLeave.setFont(new Font("serif", Font.BOLD, 16));
lblHistory.setFont(new Font("serif", Font.BOLD, 16));
lblLevel.setFont(new Font("Serif", Font.BOLD, 16));
lblScore.setFont(new Font("Serif", Font.BOLD, 16));
btnifo = new JButton("关于",createImageIcon("/images/about.jpg"));// 关于按钮
btnifo.setActionCommand("SnailWorkgroup");// 设置actionCommand
btnStart = new JButton(" 重玩 ",createImageIcon("/images/restart.jpg"));// 重新开始按钮
btnStart.setActionCommand("start");// 设置actionCommand
btnRemind = new JButton("提示",createImageIcon("/images/6.gif"));// 提示按钮
btnRemind.setActionCommand("remind");// 设置actionCommand
btnDemo = new JButton("演示",createImageIcon("/images/demo.jpg"));// 演示按钮
btnDemo.setActionCommand("demo");// 设置actionCommand
btnPause = new JButton("暂停",createImageIcon("/images/pause.gif"));// 暂停按钮
btnPause.setActionCommand("break");// 设置actionCommand
btnQuit = new JButton("退出",createImageIcon("/images/exit.jpg"));// 退出按钮
btnQuit.setActionCommand("quit");// 设置actionCommand
cbMusic=new JComboBox(mStrings);//音乐下拉菜单
cbMusic.setSelectedIndex(0);//设置默认选项
cbMusic.setActionCommand("music");//设置actionCommand
btnSound=new JButton("关",createImageIcon("/images/muse.jpg"));//声音按钮
btnSound.setActionCommand("sound");//设置 actionCommand
gPanel = new GamePanel(model);// gPanel面板
gPanel.setBackground(Color.black);// 设置背景色为黑色
// 创建GamePanel实例
setPanel = new JPanel();
// 设置位置
setPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
statPanel = new JPanel();
// 设置布局
statPanel.setLayout(new GridLayout(1, 6));
}
// 显示工作组信息
private void showifo() {
JOptionPane.showMessageDialog(this, "", "蜗牛工作组",
JOptionPane.INFORMATION_MESSAGE, new ImageIcon(ImageLoader
.getIconImage("images/about.gif")));
}
// observer 必须写的方法用于动态更新数据和界面
public void update(Observable obs, final Object obj) {
// 如果对象是ArrayList那么就显示路径
if (obj instanceof ArrayList) {
gPanel.showPath(obj);
} else if (obj instanceof String) {
// 如果对象是String
if (obj.equals("timer")) {
// 如果是timer
lblScore.setText("得分:"+model.getScore());//设置得分
lblTimer.setText("剩余时间:" + (model.getTimeLeft() - model.getSeconds()) + "秒");//设置剩余时间
lblLeave.setText(" 可消除"+ model.solutionCount() + "对");//设置剩余对数
lblHistory.setText("本关纪录" + model.getHistoryRecord() + "秒");//显示本关记录
} else if (obj.equals("breakrecord")) {
// 如果是breakRecorder
JOptionPane.showMessageDialog(this, "恭喜打破纪录!" + "\n原来纪录:"
+ model.getHistoryRecord() + "秒" + "\n你用时间:"
+ model.getSeconds() + "秒");
} else if (obj.equals("remind")) {
int count = model.getCount();// 剩余提示次数
// 点击次数变化,提示变化
if (count == 0) {
btnRemind.setIcon(createImageIcon("/images/6.gif"));
} else if (count == 1) {
btnRemind.setIcon(createImageIcon("/images/5.gif"));
} else if (count == 2) {
btnRemind.setIcon(createImageIcon("/images/4.gif"));
} else if (count == 3) {
btnRemind.setIcon(createImageIcon("/images/3.gif"));
} else if (count == 4) {
btnRemind.setIcon(createImageIcon("/images/2.gif"));
} else if (count == 5) {
btnRemind.setIcon(createImageIcon("/images/1.gif"));
} else if (count == 6) {
btnRemind.setIcon(createImageIcon("/images/0.gif"));
}
}
//如果是演示
else if(obj.equals("dem")){
if (btnDemo.getText().equals("演示")){
btnDemo.setText("停止");
btnDemo.setIcon(createImageIcon("/images/stopDemo.jpg"));
}
else if(btnDemo.getText().equals("停止")){
btnDemo.setText("演示");
btnDemo.setIcon(createImageIcon("/images/demo.jpg"));
}
}
else if (obj.equals("demofinished")) {
// 如果是demoFinished
JOptionPane.showMessageDialog(this, "演示完成!");
btnDemo.setText("演示");// btnDemo内容变成"演示"
btnDemo.setIcon(createImageIcon("demo.jpg"));
// 其它按钮激活
btnifo.setEnabled(true);
btnPause.setEnabled(true);
btnRemind.setEnabled(true);
btnStart.setEnabled(true);
} else if (obj.equals("level")) {
int level = model.getLevel();
lblLevel.setText("第 " + level + " 关");
}
//如果是音乐下拉菜单
else if (obj.equals("music")){
int index=cbMusic.getSelectedIndex();
if(index==1){
gameMusic="我心永恒.mid";
bgSound.stop();
bgSound=new Sound(gameMusic);
btnSound.setText("关");
bgSound.play(2);// 开始游戏音乐
btnSound.setIcon(createImageIcon("/images/muse.jpg"));
}
else if(index==2){
gameMusic="柠檬树.mid";
bgSound.stop();
bgSound=new Sound(gameMusic);
btnSound.setText("关");
bgSound.play(2);// 开始游戏音乐
btnSound.setIcon(createImageIcon("/images/muse.jpg"));
}
else if(index==3){
gameMusic="奇幻旋律.mid";
bgSound.stop();
bgSound=new Sound(gameMusic);
btnSound.setText("关");
bgSound.play(2);// 开始游戏音乐
btnSound.setIcon(createImageIcon("/images/muse.jpg"));
}
else if(index==4){
gameMusic="梦中婚礼.mid";
bgSound.stop();
bgSound=new Sound(gameMusic);
btnSound.setText("关");
bgSound.play(2);// 开始游戏音乐
btnSound.setIcon(createImageIcon("/images/muse.jpg"));
}
}
//如果是音乐开关
else if(obj.equals("sound")){
if (btnSound.getText().equals("开")){
btnSound.setText("关");
bgSound.play(2);// 开始游戏音乐
btnSound.setIcon(createImageIcon("/images/muse.jpg"));
}
else if(btnSound.getText().equals("关")){
btnSound.setText("开");
bgSound.stop();
btnSound.setIcon(createImageIcon("/images/voice.jpg"));
}
}
//如果是暂停
else if(obj.equals("pause"))
{
if (btnPause.getText().equals("暂停")){
btnPause.setText("开始");
btnPause.setIcon(createImageIcon("/images/begin.gif"));
}
else if (btnPause.getText().equals("开始")) {
btnPause.setText("暂停");
btnPause.setIcon(createImageIcon("/images/pause.gif"));
lblTimer.setText("剩余时间:" + (model.getTimeLeft() - model.getSeconds()) + "秒");;
}
}
//如果是无解
else if (obj.equals("nosolution")) {
JOptionPane.showMessageDialog(this, "死锁!单击确定后自动刷新.");
} else if (obj.equals("SnailWorkgroup")) {// 显示制作人信息
showifo();
}
} else {
repaint();// repaint调用paint()
}
}
}
//?无法对model里的pictureFile操作。。
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -