- package control;
- import java.awt.Point;
- 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 model.Model;
- import view.PicBoard;
- /**
- *
- * @author 何晓飞 control 类对界面上的事件进行监听
- * @version 2.0
- */
- public class Control extends MouseAdapter implements ActionListener,
- WindowListener {
- /* 一个model */
- private Model model;
- public Control(Model model) {
- this.model = model;
- }
- /*
- * @see
- * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
- * 监听按钮事件
- */
- public void actionPerformed(ActionEvent e) {
- /*
- * 获取命令 并转化为小写形式
- */
- String cmd = e.getActionCommand().toLowerCase();
- /*
- * 根据不同的命令参数进行响应
- */
- /*
- * 重新开始游戏
- */
- if (cmd.equals("restart")) {
- model.reStart();
- }
- /*
- * 进入游戏面板
- */
- else if (cmd.equals("enter")) {
- model.enter();
- }
- /*
- * 在初始界面退出游戏
- */
- else if (cmd.equals("cancel")) {
- model.cancel();
- }
- /*
- * 装载游戏
- */
- else if (cmd.equals("load")) {
- model.load();
- }
- /*
- * 提示
- */
- else if (cmd.equals("remind")) {
- model.remind();
- }
- /*
- * 选择图片
- */
- else if (cmd.equals("pic")) {
- model.clickPic();
- }
- /*
- * 音乐开关
- */
- else if (cmd.equals("sound")) {
- model.clickSound();
- }
- else if(cmd.equals("back")){
- model.back();
- }
- /*
- * 选择音乐
- */
- else if (cmd.equals("music")) {
- model.clickMusic();
- }
- /*
- * 演示
- */
- else if (cmd.equals("demo")) {// 如果是demo
- model.demo();
- }
- /*
- * 暂停
- */
- else if (cmd.equals("pause")) {
- model.pause();
- }
- /*
- * 帮助
- */
- else if (cmd.equals("about")) {
- model.about();
- }
- /*
- * 游戏面板退出游戏
- */
- else if (cmd.equals("save")) {
- model.save();
- }
- }
- /**
- * 单击右键取消选择,单击左键则将系统坐标转化为行列坐标并执行相应方法
- */
- public void mousePressed(MouseEvent e) {
- Object src = e.getSource();// 得到事件对象
- if (src instanceof PicBoard) {
- if (e.getButton() == MouseEvent.BUTTON3)
- model.cancelSelect();// 右键则取消选择
- else
- model.clickCell(model.pointToMatrix(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) {
- Point p = e.getWindow().getLocationOnScreen();
- model.setFrameLocation(p);
- 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
- }
- }