java版的连连看游戏
源代码在线查看: control.java
package control;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import model.Model;
import view.CentralPanel;
import view.GamePanel;
import view.ImageLoader;
/**
* @author 何晓飞, 李强, 何明
* @version 2.0 本类实现了对centralPanel所有控件的监控
*/
public class Control extends MouseAdapter implements ActionListener,
WindowListener {
/* 一个model */
private Model model;
/* CentralPanel实例 */
private CentralPanel cen;
/**
* Control (构造函数) 将正在操作的那个Panel传进来
*
* @param Model
* model ...
*/
public Control(Model model, CentralPanel cen) {
this.model = model;// 将形参付给model
this.cen = cen;// 将形参付给cen
}
public Control(Model model) {
this.model = model;// 将形参付给model
}
// 监听到的是游戏界面那四个Button的事件
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand().toLowerCase();// 得到actionCommand
if (cmd.equals("start")) {// 如果是start
int response =JOptionPane.showConfirmDialog(null, "您的操作将导致游戏从第一关重新开始,您是否决定继续这样的操作?",
" 请 确 认", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(response == JOptionPane.YES_OPTION){
model.restartGame();
}
} else if (cmd.equals("remind")) {// 如果是remind
model.remind();
}
else if(cmd.equals("sound")){//如果是sound
model.clickSound();
}
else if (cmd.equals("music")){//如果是music
model.clickMusic();
}
else if (cmd.equals("demo")) {// 如果是demo
JButton btn = (JButton) e.getSource();// 将所得对象强制转化为JButton
if (btn.getText().equals("演示")) {// 如果是"演示"则弹出确认对话框
int response = JOptionPane.showConfirmDialog(null, "是否进入演示模式?",
" 请 确 认", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.YES_OPTION) {// 经确认后
model.demo();
model.setLevel(model.getLevel());// 设置关数
// 锁定其它按钮
cen.getBtnifo().setEnabled(false);
cen.getBtnPause().setEnabled(false);
cen.getBtnRemind().setEnabled(false);
cen.getBtnStart().setEnabled(false);
// 开始演示
model.startDemo();
}
} else if (btn.getText().equals("停止")) {// 如果是停止则弹出确认对话框
int response = JOptionPane.showConfirmDialog(null, "是否退出演示模式?",
" 请 确 认", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.YES_OPTION) {// 经确认后
model.stopDemo();// 停止演示
model.demo();//刷新按钮
// 其它按钮激活
cen.getBtnifo().setEnabled(true);
cen.getBtnPause().setEnabled(true);
cen.getBtnRemind().setEnabled(true);
cen.getBtnStart().setEnabled(true);
int secResponse = JOptionPane.showConfirmDialog(null, "是否开始游戏?",
" 请 确 认", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);{
if (secResponse==JOptionPane.YES_OPTION){
model.setCount(model.getInitCount());
model.gameStart();// 开始游戏
}
else if(secResponse==JOptionPane.NO_OPTION){
model.quit();
}
}
}
}
} else if (cmd.equals("break")) {// 如果是暂停
JButton btn = (JButton) e.getSource();// 强制转化
if (btn.getText().equals("暂停")) {
model.pause();
// 创建一个Icon用于覆盖游戏界面
ImageIcon coverIcon = new ImageIcon(ImageLoader.getCover());
// 创建一个包含Icon的label
JLabel cover = new JLabel(coverIcon);
// 设置gPanel不可见
cen.getGPanel().setVisible(false);
// 加入cover label
cen.add(cover);
// 其它按钮锁定
cen.getBtnDemo().setEnabled(false);
cen.getBtnRemind().setEnabled(false);
cen.getBtnStart().setEnabled(false);
model.bPlaying = false;
} else if (btn.getText().equals("开始")){
model.pauseDuration();
//btn.setText("暂停");// 按钮内容变为"暂停"
// 其它按钮激活
cen.getGPanel().setVisible(true);
cen.getBtnDemo().setEnabled(true);
cen.getBtnRemind().setEnabled(true);
cen.getBtnStart().setEnabled(true);
model.bPlaying = true;
}
} else if (cmd.equals("snailworkgroup")) {// 如果是snailworkGroup
model.about();// 显示about
} else if (cmd.equals("quit")) {// 如果是退出
model.quit();// 退出游戏
}
}
// 监听到的是游戏界面GamePanel中的鼠标按下事件
public void mousePressed(MouseEvent e) {
Object src = e.getSource();// 得到事件对象
if (src instanceof GamePanel) {
GamePanel gp = (GamePanel) src;
if (e.getButton() == MouseEvent.BUTTON3)
model.cancelSelect();// 右键则取消选择
else
model.clickMatrix(gp.PointToMetrix(e.getPoint()));// 否则返回位置
}
}
// 监听到的是窗体事件,要实现其全部的接口中的方法
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent e) {
model.setFrameLocation(e.getWindow().getLocation());
model.saveSet();
}
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
}