?? jmine.java
字號:
/**
* This program is written by Jerry Shen(Shen Ji Feng) use the technology of
* SWING GUI and the OO design
*
* @author Jerry Shen all rights reserved.
* Email:jerry.shen@cognizant.com; jerry_shen_sjf@yahoo.com.cn
* Please report bug to these emails.
* Open source under GPLv3
*
* version 2.0
*/
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import java.util.Timer;
import java.util.TimerTask;
@SuppressWarnings("serial")
public class JMine extends JFrame implements MouseListener, ActionListener {
public static void main(String [] args) {
JMine jmine = new JMine();
jmine.setVisible(true);
}
private AboutFrame about;
ImageIcon blankIcon = new ImageIcon(ResourceLocator.getImgResource("blank1.gif"));
private JButton bTest;
private GridBagConstraints constraints;
private JPanel controlPane;
private CustomerFrame customerFrame;
private final int DEFAULT_COL_COUNT = 10;
private final int DEFAULT_ROW_COUNT = 10;
private ImageIcon[] faceIcon = { new ImageIcon(ResourceLocator.getImgResource("smile.gif")),
new ImageIcon(ResourceLocator.getImgResource("Ooo.gif")) };
private final int GAME_EASY = 12;
private final int GAME_HARD = 36;
private final int GAME_MIDDLE = 24;
private boolean gameStarted;
private GridBagLayout gridbag;
private int height;
private long lastClick = 0L;
private JMenuBar mb;
private JMenu mGame;
private JMenu mHelp;
private JMenuItem miAbout;
private JMenuItem miCustomer;
private JMenuItem miEasy;
private JMenuItem miExit;
private JMenuItem miHard;
private JMenuItem miMiddle;
private JMineArth mine;
private ImageIcon[] mineBombStatus = { new ImageIcon(ResourceLocator.getImgResource("0.gif")),
new ImageIcon(ResourceLocator.getImgResource("mine.gif")), new ImageIcon(ResourceLocator.getImgResource("wrongmine.gif")),
new ImageIcon(ResourceLocator.getImgResource("bomb.gif")) };
private JMineButton[][] mineButton;
private JCounter mineCounter;
private ImageIcon[] mineNumIcon = { new ImageIcon(ResourceLocator.getImgResource("blank1.gif")),
new ImageIcon(ResourceLocator.getImgResource("1.gif")), new ImageIcon(ResourceLocator.getImgResource("2.gif")),
new ImageIcon(ResourceLocator.getImgResource("3.gif")), new ImageIcon(ResourceLocator.getImgResource("4.gif")),
new ImageIcon(ResourceLocator.getImgResource("5.gif")), new ImageIcon(ResourceLocator.getImgResource("6.gif")),
new ImageIcon(ResourceLocator.getImgResource("7.gif")), new ImageIcon(ResourceLocator.getImgResource("8.gif")),
new ImageIcon(ResourceLocator.getImgResource("0.gif"))};
private ImageIcon[] mineStatus = { new ImageIcon(ResourceLocator.getImgResource("blank1.gif")),
new ImageIcon(ResourceLocator.getImgResource("flag.gif")), new ImageIcon(ResourceLocator.getImgResource("question.gif")) };
private long nowClick = 0L;
public int numFlaged;
public int numMine;
private JPanel pane;
Insets space = new Insets(0, 0, 0, 0);
private JCounter timeCounter;
private Timer timer = new Timer();
private int width;
private WinFrame winFrame;
private Timer winTimer = new Timer();
// Constructor of the game
public JMine() {
super("JMine Game");
setSize(250, 350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Game vars
gameStarted = false;
numMine = GAME_EASY;
numFlaged = 0;
gridbag = new GridBagLayout();
constraints = new GridBagConstraints();
pane = new JPanel();
pane.setLayout(gridbag);
constraints.fill = GridBagConstraints.BOTH;
constraints.anchor = GridBagConstraints.CENTER;
// Begin Menu Set
mb = new JMenuBar();
mGame = new JMenu("Game");
miEasy = new JMenuItem("Easy");
miEasy.addActionListener(this);
miMiddle = new JMenuItem("Middle");
miMiddle.addActionListener(this);
miHard = new JMenuItem("Hard");
miHard.addActionListener(this);
miCustomer = new JMenuItem("Customer");
miCustomer.addActionListener(this);
miExit = new JMenuItem("Exit");
miExit.addActionListener(this);
mGame.add(miEasy);
mGame.add(miMiddle);
mGame.add(miHard);
mGame.addSeparator();
mGame.add(miCustomer);
mGame.addSeparator();
mGame.add(miExit);
mb.add(mGame);
mHelp = new JMenu("Help");
miAbout = new JMenuItem("About...");
mHelp.add(miAbout);
miAbout.addActionListener(this);
mb.add(mHelp);
this.setJMenuBar(mb);
// end of Menu Set
// Control Panel
controlPane = new JPanel();
bTest = new JButton(faceIcon[0]);
bTest.setSize(26, 27);
bTest.setMargin(space);
bTest.addMouseListener(this);
bTest.setPressedIcon(faceIcon[1]);
mineCounter = new JCounter(numMine);
timeCounter = new JCounter();
controlPane.add(mineCounter);
controlPane.add(bTest);
controlPane.add(timeCounter);
buildConstraints(constraints, 0, 0, 10, 2, 100, 100);
gridbag.setConstraints(controlPane, constraints);
pane.add(controlPane);
width = DEFAULT_COL_COUNT;
height = DEFAULT_ROW_COUNT;
// Bottons
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
mineButton[j][i] = new JMineButton(j, i, blankIcon);
mineButton[j][i].addMouseListener(this);
mineButton[j][i].setMargin(space);
buildConstraints(constraints, j, i + 3, 1, 1, 80, 80);
gridbag.setConstraints(mineButton[j][i], constraints);
pane.add(mineButton[j][i]);
}
}
// Content Pane
setContentPane(pane);
setLocation(200, 200);
setVisible(true);
// About Frame
about = new AboutFrame("JMine About");
// Customer Frame
customerFrame = new CustomerFrame("Customer game");
// Win Frame
winFrame = new WinFrame("You win!", customerFrame);
}
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == miEasy) {
setNewGame(GAME_EASY,DEFAULT_COL_COUNT,DEFAULT_ROW_COUNT);
width = DEFAULT_COL_COUNT;
height = DEFAULT_ROW_COUNT;
return;
}
if (e.getSource() == miMiddle) {
setNewGame(GAME_MIDDLE,DEFAULT_COL_COUNT,DEFAULT_ROW_COUNT);
width = DEFAULT_COL_COUNT;
height = DEFAULT_ROW_COUNT;
return;
}
if (e.getSource() == miHard) {
setNewGame(GAME_HARD,DEFAULT_COL_COUNT,DEFAULT_ROW_COUNT);
width = DEFAULT_COL_COUNT;
height = DEFAULT_ROW_COUNT;
return;
}
if (e.getSource() == miCustomer) {
timer.cancel();
customerFrame.setOk(false);
customerFrame.setVisible(true);
winTimer.schedule(new TimerTask(){
public void run() {
while(!customerFrame.isOk()){
}
setNewGame(customerFrame.getMineNum(),customerFrame.getColCount(),customerFrame.getRowCount());
width = customerFrame.getColCount();
height = customerFrame.getRowCount();
this.cancel();
}
},0L);
return;
}
if (e.getSource() == miExit) {
System.exit(0);
}
if (e.getSource() == miAbout) {
about.setVisible(true);
}
} catch (Exception ie) {
}
}
// You lose
private void bomb(int col, int row, int colCount, int rowCount){
try{
//System.out.println("Bomb!");
for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < colCount; j++) {
mineButton[j][i].setIcon(mineBombStatus[0]);
int toShow;
toShow = mine.mine[j][i] != 9 ? 0 : 1;
mineButton[j][i].setClickFlag(true);
if (toShow == 1 && (i != row || j != col)) {
mineButton[j][i].setIcon(mineBombStatus[toShow]);
mineButton[j][i].setClickFlag(true);
} else if (toShow == 1 && (i == row && j == col)) {
mineButton[j][i].setIcon(mineBombStatus[3]);
mineButton[j][i].setClickFlag(true);
} else if (toShow == 0 && mineButton[i][j].getFlag() != 1) {
mineButton[j][i].setEnabled(false);
} else if (toShow == 0 && mineButton[i][j].getFlag() == 1) {
mineButton[j][i].setIcon(mineBombStatus[2]);
mineButton[j][i].setClickFlag(true);
}
}
}
timer.cancel();
}catch (Exception e){
}
}
// Set the GUI objects positions
private void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw,
int gh, int wx, int wy) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -