PlayBoard.java
上传用户:jennyfly
上传日期:2021-08-10
资源大小:735k
文件大小:18k
- package view;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.FlowLayout;
- import java.awt.Font;
- import java.awt.Point;
- import java.awt.Toolkit;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseListener;
- 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;
- /**
- * 程序的游戏面板
- *
- * @author 何晓飞
- * @version 2.0
- */
- public class PlayBoard extends JPanel implements Observer {
- private static final long serialVersionUID = 1L;
- private ImageIcon iInfo, iStart,iSave,iBack;
- private ImageIcon iDemo, iStop;
- private ImageIcon iPause, iBegin;
- private ImageIcon iSoundPlay, iSoundMuse;
- private ImageIcon iRemind0, iRemind1, iRemind2, iRemind3, iRemind4,
- iRemind5, iRemind6,iRemind7,iRemind8;
- /* 背景声音 */
- private Sound bgSound;
- /* 所使用的图片的文件名 */
- private String picFile;
- /* 游戏中播放的音乐 */
- private String gameMusic;
- /* 图片选项下拉菜单显示的内容 */
- private String[] picStrings = { "图片选项", "水果", "国旗" };
- /* 音乐下拉菜单里显示的内容 */
- private String[] mStrings = { "背景音乐", "泰坦尼克", "柠檬树", "奇幻旋律", "梦中婚礼" };
- /* 6个JButton 分别用来控制游戏的暂停,关于,提示,演示,开始,保存 */
- private JButton btnifo, btnPause;
- private JButton btnRemind, btnDemo, btnStart,btnSave;
- private JLabel lblRemind;
- /* 音乐下拉菜单和图片下拉菜单 */
- private JComboBox cbMusic, cbPic;
- /* 音乐开关 */
- private JButton btnSound;
- private JButton btnBack;
- /* 图片面板 */
- private PicBoard pb;
- /* 4个JLabel分别显示当前关历史纪录,剩余可消去图片对数,已用时和剩余时间 */
- private JLabel lblHistory;
- private JLabel lblPairs;
- private JLabel lblTimePass;
- private JLabel lblTimeLeft;
- /* 显示当前关数 */
- private JLabel lblLevel;
- private JLabel lblLevelName;
- /* 显示得分 */
- private JLabel lblScore;
- // 创建一个Icon用于覆盖游戏界面
- ImageIcon coverIcon;
- // 创建一个包含Icon的label
- JLabel cover = new JLabel(coverIcon);
- /* 游戏运行模式 */
- private Model model;
- /* 操作面板 */
- private JPanel setPanel;
- /* 状态面板 */
- private JPanel statPanel;
- /**
- * 初始化各组件和相关参数
- *
- * @param model
- * 游戏运行时的逻辑控制
- */
- public PlayBoard(Model model) {
- this.model = model;
- // 初始化
- gameMusic = "梦中婚礼.mid";
- // 游戏中的背景音乐
- bgSound = new Sound(gameMusic);
- initComponents();
- // 将按钮添加至设置面板
- setPanel.add(btnifo);
- setPanel.add(btnStart);
- setPanel.add(btnRemind);
- setPanel.add(lblRemind);
- setPanel.add(btnDemo);
- setPanel.add(btnPause);
- setPanel.add(btnSave);
- setPanel.add(cbPic);
- setPanel.add(cbMusic);
- setPanel.add(btnSound);
- setPanel.add(btnBack);
- // 设置设置面的板背景色
- setPanel.setBackground(Model.BG_COLOR);
- // 将标签添加至状态面板
- statPanel.add(lblLevel);
- statPanel.add(lblLevelName);
- statPanel.add(lblScore);
- statPanel.add(lblTimePass);
- statPanel.add(lblTimeLeft);
- statPanel.add(lblPairs);
- statPanel.add(lblHistory);
- // 设置状态面板的背景色
- statPanel.setBackground(Model.BG_COLOR);
- // 设置大小
- setSize(820, 553);
- // 设置布局管理器
- setLayout(new BorderLayout());
- // 设置各Panel方位
- add(setPanel, BorderLayout.NORTH);// setPanel占据北边
- add(pb, 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();
- }
- }
- // 给不同的对象添加不同的监听
- public void addController(Object obj) {
- // 只监听图片面板的鼠标事件
- if (obj instanceof MouseListener) {
- pb.addMouseListener((MouseListener) obj);
- }
- // 给各按钮添加监听
- if (obj instanceof ActionListener) {
- ActionListener al = (ActionListener) obj;
- btnifo.addActionListener(al);
- btnStart.addActionListener(al);
- btnRemind.addActionListener(al);
- btnDemo.addActionListener(al);
- btnPause.addActionListener(al);
- btnSound.addActionListener(al);
- btnBack.addActionListener(al);
- btnSave.addActionListener(al);
- cbPic.addActionListener(al);
- cbMusic.addActionListener(al);
- }
- }
- /**
- * 设置音乐
- *
- * @param music
- * 目标音乐文件名
- */
- private void changeMusic(String music) {
- bgSound.stop();
- gameMusic = music;
- /*
- * 新建背景音乐改变音乐开关状态并设置音乐
- */
- bgSound = new Sound(gameMusic);
- btnSound.setText("关");
- bgSound.play(true);// 开始游戏音乐
- btnSound.setIcon(iSoundMuse);
- model.setMusic(gameMusic);
- }
- /**
- * 改变图片
- *
- * @param pic
- * 目标文件夹名
- */
- private void changePic(String pic) {
- /*
- * 设置图片文件名
- */
- model.setPictureFile(pic);
- /*
- * 重新开始游戏
- */
- model.restartGame();
- }
- // 初始化各组件
- private void initComponents() {
- // 初始化各组件,设置字体,ActionCommand
- lblTimePass = new JLabel();
- lblTimePass.setForeground(new Color(34, 139, 34));
- lblTimeLeft = new JLabel();
- lblTimeLeft.setForeground(new Color(61, 50, 167));
- lblPairs = new JLabel();
- lblPairs.setForeground(new Color(34, 139, 34));
- lblHistory = new JLabel();
- lblHistory.setForeground(new Color(34, 139, 34));
- lblLevel = new JLabel();//
- lblLevel.setForeground(new Color(238, 25, 59));
- lblLevelName=new JLabel();
- lblLevelName.setForeground(new Color(238, 25, 59));
- lblScore = new JLabel();
- lblScore.setForeground(new Color(34, 139, 34));
- // 设置字体
- lblTimePass.setFont(new Font("华文楷体", Font.PLAIN,18));
- lblTimeLeft.setFont(new Font("华文楷体", Font.PLAIN,18));
- lblPairs.setFont(new Font("华文楷体", Font.PLAIN, 18));
- lblHistory.setFont(new Font("华文楷体", Font.PLAIN,18));
- lblLevel.setFont(new Font("华文行楷", Font.PLAIN,18));
- lblLevelName.setFont(new Font("华文行楷", Font.PLAIN,18));
- lblScore.setFont(new Font("华文楷体", Font.PLAIN, 18));
- iInfo = new ImageIcon(ImageLoader.getImage("about.gif"));
- iStart = new ImageIcon(ImageLoader.getImage("restart.gif"));
- iSave=new ImageIcon(ImageLoader.getImage("save.gif"));
- iBack=new ImageIcon(ImageLoader.getImage("back.gif"));
- iDemo = new ImageIcon(ImageLoader.getImage("demo.gif"));
- iStop = new ImageIcon(ImageLoader.getImage("Stop.gif"));
- iPause = new ImageIcon(ImageLoader.getImage("pause.gif"));
- iBegin = new ImageIcon(ImageLoader.getImage("begin.gif"));
- iSoundPlay = new ImageIcon(ImageLoader.getImage("play.jpg"));
- iSoundMuse = new ImageIcon(ImageLoader.getImage("muse.jpg"));
- iRemind0 = new ImageIcon(ImageLoader.getImage("0.gif"));
- iRemind1 = new ImageIcon(ImageLoader.getImage("1.gif"));
- iRemind2 = new ImageIcon(ImageLoader.getImage("2.gif"));
- iRemind3 = new ImageIcon(ImageLoader.getImage("3.gif"));
- iRemind4 = new ImageIcon(ImageLoader.getImage("4.gif"));
- iRemind5 = new ImageIcon(ImageLoader.getImage("5.gif"));
- iRemind6 = new ImageIcon(ImageLoader.getImage("6.gif"));
- iRemind7=new ImageIcon(ImageLoader.getImage("7.gif"));
- iRemind8=new ImageIcon(ImageLoader.getImage("8.gif"));
- btnifo = new JButton(iInfo);// 关于按钮
- btnifo.setActionCommand("about");// 设置actionCommand
- btnStart = new JButton(iStart);// 重新开始按钮
- btnStart.setActionCommand("restart");// 设置actionCommand
-
- btnRemind = new JButton(iRemind6);// 提示按钮
- btnRemind.setActionCommand("remind");// 设置actionCommand
-
- lblRemind=new JLabel("次提示");
- lblRemind.setFont(new Font("华文行楷",Font.PLAIN,20));
- btnDemo = new JButton(iDemo);// 演示按钮
- btnDemo.setActionCommand("demo");// 设置actionCommand
- btnPause = new JButton(iPause);// 暂停按钮
- btnPause.setActionCommand("pause");// 设置actionCommand
-
- btnSave=new JButton(iSave);
- btnSave.setActionCommand("save");
- cbPic = new JComboBox(picStrings);// 图片下拉菜单
- cbPic.setSelectedIndex(0);// 设置默认选项
- cbPic.setActionCommand("pic");// 设置actionCommand
- cbMusic = new JComboBox(mStrings);// 音乐下拉菜单
- cbMusic.setSelectedIndex(0);// 设置默认选项
- cbMusic.setActionCommand("music");// 设置actionCommand
- btnSound = new JButton("开", iSoundPlay);// 声音按钮
- btnSound.setActionCommand("sound");// 设置 actionCommand
-
- btnBack=new JButton(iBack);
- btnBack.setActionCommand("back");
- coverIcon = new ImageIcon(ImageLoader.getCover());//创建一个背景
- cover = new JLabel(coverIcon);//创建一个label
-
- pb = new PicBoard(model);// gPanel面板
- // 创建GamePanel实例
- setPanel = new JPanel();
- // 设置位置
- setPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
- statPanel = new JPanel();
- // 设置布局
- statPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
- }
- // 显示工作组信息
- private void showifo() {
- JOptionPane.showMessageDialog(this, "连连看2.0" + "n" + "本游戏非开源软件" + "n"
- + "仅限学习及参考所用" + "n" + "请勿用于商业运作" + "n" + "如有问题及建议请mail到"
- + "n" + "kevin1987725@163.com" + "n" + "修改日期:2008 12月" + "n"
- + "<html><font color=#228B22>作者:何晓飞</font></html>", "关于本程序",
- JOptionPane.INFORMATION_MESSAGE, new ImageIcon(ImageLoader
- .getImage("info.jpg")));
- }
- // observer 必须写的方法用于动态更新数据和界面
- public void update(Observable obs, Object obj) {
- /*
- * 根据不同的消息产生不同的响应
- */
- if (obj instanceof String) {
- if (obj.equals("link")) {
- /*
- * 画线
- */
- pb.drawLink();
- /*
- * 释放关键点
- */
- model.freeKeyPoints();
- }
- /*
- * 更新关数,包括该关的名字
- */
- else if (obj.equals("level")) {
- lblLevel.setText("第" + model.getLevel() + "关: ");
- lblLevelName.setText(model.getLevelName());
- }
- /*
- * 介于得分,剩余时间,可消除对数更新的比较快,故 采取与时间更新同步的策略。
- */
- else if (obj.equals("timer")) {
- // 如果是timer
- lblScore.setText("得分: " + model.getCurrentScore()+" ");// 设置得分
- lblTimePass.setText("已用时"+model.getPlayTime()+"秒 ");//设置流失时间
- lblTimeLeft.setText("剩余时间: " + model.getTimeLeft() + "秒 ");// 设置剩余时间
- lblPairs.setText(" 可消除: " + model.solutionCount() + "对 ");// 设置剩余对数
- }
- /*
- * 显示当前关的历史记录
- */
- else if (obj.equals("record")) {
- lblHistory.setText("本关纪录: " + model.getRecord(model.getLevel())
- + "秒 ");
- }
- /*
- * 超过游戏时间游戏结束
- */
- else if (obj.equals("over")) {
- int response = JOptionPane
- .showConfirmDialog(this,
- "你已经超过规定时间,是否重新开始游戏 ?n点否直接退出游戏!", " 友情提示",
- JOptionPane.YES_NO_OPTION,
- JOptionPane.QUESTION_MESSAGE);
- if (response == JOptionPane.YES_OPTION) {
- model.restartGame();
- } else if (response == JOptionPane.NO_OPTION) {
- System.exit(0);
- }
- }
- /*
- * 重新开始游戏
- */
- else if (obj.equals("restart")) {
- int response = JOptionPane.showConfirmDialog(this,
- "该操作会使游戏返回第一关,n" + "您是否决定继续这样的操作?",
- " 请 确 认", JOptionPane.YES_NO_OPTION,
- JOptionPane.QUESTION_MESSAGE);
- if (response == JOptionPane.YES_OPTION) {
- model.restartGame();
- }
- }
- /*
- * 打破历史记录
- */
- else if (obj.equals("breakrecord")) {
- // 如果是breakRecorder
- JOptionPane.showMessageDialog(this, "恭喜打破纪录!" + "n原来纪录:"
- + model.getRecord(model.getLevel()) + "秒" + "n你用时间:"
- + model.getPlayTime() + "秒");
- }
- /*
- * 提示
- */
- else if (obj.equals("remind")) {
- int reminds = model.getReminds();// 剩余提示次数
- // 点击次数变化,提示变化
- ImageIcon icon;
- switch(reminds){
- case 8:
- icon=iRemind8;
- break;
- case 7:
- icon=iRemind7;
- break;
- case 6:
- icon=iRemind6;
- break;
- case 5:
- icon=iRemind5;
- break;
- case 4:
- icon=iRemind4;
- break;
- case 3:
- icon=iRemind3;
- break;
- case 2:
- icon=iRemind2;
- break;
- case 1:
- icon=iRemind1;
- break;
- default:
- icon=iRemind0;
- break;
- }
- btnRemind.setIcon(icon);
- }
- // 如果是演示
- else if (obj.equals("demo")) {
- if (btnDemo.getIcon().equals(iDemo)) {// 如果是"演示"则弹出确认对话框
- int response = JOptionPane.showConfirmDialog(this,
- "是否进入演示模式?", " 请 确 认",
- JOptionPane.YES_NO_OPTION,
- JOptionPane.QUESTION_MESSAGE);
- if (response == JOptionPane.YES_OPTION) {// 经确认后
- btnDemo.setIcon(iStop);
- // 锁定其它按钮
- btnifo.setEnabled(false);
- btnPause.setEnabled(false);
- btnRemind.setEnabled(false);
- btnStart.setEnabled(false);
- btnSave.setEnabled(false);
- cbPic.setEnabled(false);
- cbMusic.setEnabled(false);
- btnSound.setEnabled(false);
- btnBack.setEnabled(false);
- // 开始演示
- model.startDemo();
- }
- } else if (btnDemo.getIcon().equals(iStop)) {// 如果是停止则弹出确认对话框
- int response = JOptionPane.showConfirmDialog(this,
- "是否退出演示模式?", " 请 确 认",
- JOptionPane.YES_NO_OPTION,
- JOptionPane.QUESTION_MESSAGE);
- if (response == JOptionPane.YES_OPTION) {// 经确认后
- model.stopDemo();// 停止演示
- btnDemo.setIcon(iDemo);
- // 其它按钮激活
- btnifo.setEnabled(true);
- btnPause.setEnabled(true);
- btnRemind.setEnabled(true);
- btnStart.setEnabled(true);
- btnSave.setEnabled(true);
- btnBack.setEnabled(true);
- cbPic.setEnabled(true);
- cbMusic.setEnabled(true);
- btnSound.setEnabled(true);
- }
- }
- }
- /*
- * 演示完成
- */
- else if (obj.equals("demofinished")) {
- // 如果是demoFinished
- JOptionPane.showMessageDialog(this, "演示完成!");
- btnDemo.setIcon(iDemo);
- // 其它按钮激活
- btnifo.setEnabled(true);
- btnPause.setEnabled(true);
- btnRemind.setEnabled(true);
- btnStart.setEnabled(true);
- btnSave.setEnabled(true);
- btnBack.setEnabled(true);
- cbPic.setEnabled(true);
- cbMusic.setEnabled(true);
- btnSound.setEnabled(true);
- model.IsDemo=false;
- model.startGame(true);
- }
- /*
- * 改变图片
- */
- else if (obj.equals("pic")) {
- int index = cbPic.getSelectedIndex();
- String picture = model.getPictureFile();
- switch (index) {
- case 1:
- picFile = "fruit";
- break;
- case 2:
- picFile = "flag";
- break;
- default:
- break;
- }
- if (!picture.equals(picFile)) {
- changePic(picFile);
- }
- }
- // 如果是音乐下拉菜单
- else if (obj.equals("music")) {
- int index = cbMusic.getSelectedIndex();
- String music = model.getMusic();
- switch (index) {
- case 1:
- gameMusic = "我心永恒.mid";
- break;
- case 2:
- gameMusic = "柠檬树.mid";
- break;
- case 3:
- gameMusic = "奇幻旋律.mid";
- break;
- case 4:
- gameMusic = "梦中婚礼.mid";
- break;
- default:
- break;
- }
- if (!music.equals(gameMusic)) {
- changeMusic(gameMusic);
- }
- }
- // 如果是音乐开关
- else if (obj.equals("sound")) {
- if (btnSound.getText().equals("开")) {
- btnSound.setText("关");
- bgSound.play(true);// 开始游戏音乐
- btnSound.setIcon(iSoundMuse);
- } else if (btnSound.getText().equals("关")) {
- btnSound.setText("开");
- bgSound.stop();
- btnSound.setIcon(iSoundPlay);
- }
- }
- // 如果是暂停
- else if (obj.equals("pause")) {
- if (btnPause.getIcon().equals(iPause)) {
- long currentTime = System.currentTimeMillis();
- model.setPauseTime(currentTime);
- btnPause.setIcon(iBegin);
- // 设置picBoard不可见
- pb.setVisible(false);
- // 加入cover
- add(cover);
- // 其它按钮锁定
- btnifo.setEnabled(false);
- btnDemo.setEnabled(false);
- btnRemind.setEnabled(false);
- btnStart.setEnabled(false);
- btnSave.setEnabled(false);
- btnBack.setEnabled(false);
- cbPic.setEnabled(false);
- cbMusic.setEnabled(false);
- btnSound.setEnabled(false);
- model.IsPlaying = false;
- } else if (btnPause.getIcon().equals(iBegin)) {
- model.pauseDuration();
- btnPause.setIcon(iPause);
- // 设置picBoard可见
- pb.setVisible(true);
- //移除cover
- remove(cover);
- // 其它按钮激活
- btnifo.setEnabled(true);
- btnDemo.setEnabled(true);
- btnRemind.setEnabled(true);
- btnStart.setEnabled(true);
- btnSave.setEnabled(true);
- btnBack.setEnabled(true);
- cbPic.setEnabled(true);
- cbMusic.setEnabled(true);
- btnSound.setEnabled(true);
- model.IsPlaying = true;
- }
- }
- // 如果是无解
- else if (obj.equals("nosolution")) {
- JOptionPane.showMessageDialog(this, "单击确定后自动刷新", "死锁",
- JOptionPane.ERROR_MESSAGE);
- } else if (obj.equals("about")) {
- showifo();
- } else if (obj.equals("save")) {
- int response = JOptionPane
- .showConfirmDialog(this, "保存将会覆盖上次保存的内容!n是否保存?",
- " 友情提示", JOptionPane.YES_NO_OPTION,
- JOptionPane.QUESTION_MESSAGE);
- if (response == JOptionPane.YES_OPTION) {
- model.saveGame();
- }
- } else if (obj.equals("accomplish")) {
- JOptionPane.showMessageDialog(this, "恭喜通关!", "通关",
- JOptionPane.INFORMATION_MESSAGE);
- }
- /*
- * 征求玩家意见重新开始游戏或者退出游戏
- */
- else if (obj.equals("option")) {
- int response = JOptionPane.showConfirmDialog(this, "是否重新开始游戏",
- "友情提示", JOptionPane.YES_NO_OPTION,
- JOptionPane.QUESTION_MESSAGE);
- if (response == JOptionPane.YES_OPTION) {
- model.restartGame();
- } else {
- model.cancel();
- }
- }
- }
- else {
- pb.repaint();
- }
- }
- }