📄 coreaction.java
字号:
/*
* Created on Mar 27, 2005
*
* TODO this class is control part of MVC structure. use to manage the main game
* action
*/
package control;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JCheckBoxMenuItem;
import view.Images;
//import view.MyButton;
import view.UI;
import model.Location;
import model.Model;
/**
* @author mqqqvpppm
*
* TODO this class is control part of MVC structure. use to manage the main game
* action
*/
public class CoreAction implements MouseListener, ActionListener {
public CoreAction(Model model, UI ui) {
super();
MOUSE_RIGHT_PRESSED = false;
MOUSE_LEFT_PRESSED = false;
this.model = model;
this.ui = ui;
this.buttonSize = ui.getButtonSize();
this.imageX = ui.getImageX();
this.imageY = ui.getImageY();
}
/*
* (non-Javadoc)
*
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
* just for startButton;
*/
public void actionPerformed(ActionEvent e) {
model.restart(model.getWidth(), model.getHeight(), model
.getMineNumber(), model.getType());
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
if(!isInImageArea(e)) return;
if (model.isFreezed())
return;
if (e.getButton() == MouseEvent.BUTTON2) {
runOnBothPressed(e);
} else if (e.getButton() == MouseEvent.BUTTON3) {//mouse right press
MOUSE_RIGHT_PRESSED = true;
if (MOUSE_LEFT_PRESSED) {//mouse left and right button are pressed
// at the same time
runOnBothPressed(e);
return;
}
runOnRightPressed(e);
} else if (e.getButton() == MouseEvent.BUTTON1) {//mouse left press
MOUSE_LEFT_PRESSED = true;
if (MOUSE_RIGHT_PRESSED) {//mouse left and right button are pressed
// at the same time
runOnBothPressed(e);
return;
}
runOnLeftPressed(e);
}
}
public void mouseReleased(MouseEvent e) {
if (model.isFreezed())
return;
// if(!isInImageArea(e)) return;
if (e.getButton() == MouseEvent.BUTTON2) {
this.runOnBothReleased(e);
return;
}
if (MOUSE_LEFT_PRESSED && MOUSE_RIGHT_PRESSED) {
runOnBothReleased(e);
MOUSE_LEFT_PRESSED = false;
MOUSE_RIGHT_PRESSED = false;
return;
}
if (!MOUSE_LEFT_PRESSED && !MOUSE_RIGHT_PRESSED)
return;
if (e.getButton() == MouseEvent.BUTTON1) {
MOUSE_LEFT_PRESSED = false;
runOnLeftReleased(e);
} else if (e.getButton() == MouseEvent.BUTTON3) {
MOUSE_RIGHT_PRESSED = false;
}
}
private void runOnBothPressed(MouseEvent e) {
Location location = new Location((e.getX() - imageX) / buttonSize, (e
.getY() - imageY)
/ buttonSize);
this.location = location;
int x, y, m, n;
if (model.isClickedButton(location) && model.isNumber(location)) {
for (m = 0, x = location.x - 1; m != 3; x++, m++) {
for (n = 0, y = location.y - 1; n != 3; y++, n++) {
if (x < 0 || y < 0 || x > model.getWidth() - 1
|| y > model.getHeight() - 1)
continue;
Location temp = new Location(x, y);
if (model.isClickedButton(temp) || model.isMarkedMine(temp)
|| model.isMark(temp))
continue;
ui.drawImage(Images.CLICKED, x, y);
}
}
ui.updateImage();
}
}
private void runOnLeftPressed(MouseEvent e) {
Location location = new Location((e.getX() - imageX) / buttonSize, (e
.getY() - imageY)
/ buttonSize);
this.location = location;
if (model.isClickedButton(location) || model.isMark(location)
|| model.isMarkedMine(location))
return;
ui.drawImage(Images.CLICKED, location);
ui.updateImage();
}
private void runOnRightPressed(MouseEvent e) {
Location location = new Location((e.getX() - imageX) / buttonSize, (e
.getY() - imageY)
/ buttonSize);
if (model.isClickedButton(location)) {
return;
}
if(((JCheckBoxMenuItem)ui.getMenuItem("mark")).isSelected() ){
if (model.isMarkedMine(location)) {
model.setMark(true, location);
model.setMarkedMine(false, location);
} else if (model.isMark(location)) {
model.setMark(false, location);
} else {
model.setMarkedMine(true, location);
model.setMark(false, location);
}
}else{
if(model.isMarkedMine(location)){
model.setMarkedMine(false,location);
}else if(model.isMark(location)){
model.setMark(false,location);
}else{
model.setMarkedMine(true,location);
}
}
ui.updateImage();
}
private void runOnLeftReleased(MouseEvent e) {
// Location location = new Location((e.getX() - imageX) / buttonSize, (e
// .getY() - imageY)
// / buttonSize);
if (model.isClickedButton(location) || model.isMarkedMine(location)) {
return;
}
if (model.isNumber(location) || model.isMine(location)) {//show number
model.setClickedButton(location);
} else {
model.unlockAll(location);
}
ui.updateImage();
}
private void runOnBothReleased(MouseEvent e) {
// Location location = new Location((e.getX() - imageX) / buttonSize, (e
// .getY() - imageY)
// / buttonSize);
if (!model.isClickedButton(location)) {
ui.drawImage(Images.UNCLICKED, location);
}
int x, y, m, n;
if (model.isClickedButton(location) && model.isNumber(location)) {
for (m = 0, x = location.x - 1; m != 3; x++, m++) {
for (n = 0, y = location.y - 1; n != 3; y++, n++) {
if (x < 0 || y < 0 || x > model.getWidth() - 1
|| y > model.getHeight() - 1)
continue;
Location temp = new Location(x, y);
if (model.isClickedButton(temp) || model.isMarkedMine(temp)
|| model.isMark(temp))
continue;
ui.drawImage(Images.UNCLICKED, x, y);
}
}
model.unlockOne(location);
}
ui.updateImage();
}
// private void runOnRightReleased(MouseEvent e) {
//
// }
private boolean isInImageArea(MouseEvent e) {
return e.getX() >= imageX
&& e.getX() <= imageX + buttonSize * model.getWidth()
&& e.getY() >= imageY
&& e.getY() <= imageY + buttonSize * model.getHeight();
}
private boolean MOUSE_RIGHT_PRESSED;
private boolean MOUSE_LEFT_PRESSED;
private Model model;
private int buttonSize;
private int imageX;
private int imageY;
private UI ui;
private Location location;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -