?? miniminesweeper.java
字號(hào):
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
/**
* MiniMineSweeper: This is a game
* @author Ye Wang
*/
public class MiniMineSweeper implements MouseListener{
public static final int ROW, COLOMN, WIDTH, HEIGHT;
static {
ROW=3;
COLOMN=3;
WIDTH=400;
HEIGHT=300;
}
private int boom;//Define the location of mine
private MyPanel myPanel= new MyPanel();
private JFrame myFrame = new JFrame();
private ArrayList<JButton> list=new ArrayList<JButton>();
String str="BOOooM!!! Would you like to play again?";
public static void main(String[] args) {
MiniMineSweeper myGui=new MiniMineSweeper();
myGui.go();
}
/**
* Show the "welcome" dialog when the game begins
*/
public MiniMineSweeper(){
JOptionPane.showMessageDialog(null,"Welcome to MiniMineSweeper!\n"
+"The object of the game is to click on all \n"
+"the squares EXCEPT the one with the bomb.\n"
+"(There is only one bomb). To choose a square \n"
+" to display please simply click on the square.");
}
/**
* Set the game frame
*/
public void go(){
boom=(int)(Math.random()*8);
for(int i=0;i<ROW*COLOMN;i++)
list.add(new JButton());
myFrame.setTitle("Boom!");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.addMouseListener(this);
myFrame.setSize(WIDTH, HEIGHT);
//System.out.println(boom+"");
myPanel.setLayout(new GridLayout(ROW,COLOMN));
for (JButton button : list){
button.addMouseListener(this);
button.setBackground(Color.gray);
myPanel.add(button);
} //Set the initial interface of game
myFrame.getContentPane().add(myPanel);
myFrame.setVisible(true);
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
/**
* Decide what to do when user clicks on a button
*/
public void mouseReleased(MouseEvent e) {
if (list.indexOf(e.getComponent())==boom){
e.getComponent().setBackground(Color.black);
showOption(str);
}//Boom when the user click on the mine
else e.getComponent().setBackground(Color.white);
if (win()) {
str="Congratulations!!! Would you like to"
+"play again?";
showOption(str);
}
}
/**
* deal with user's option
*/
public void showOption(String str){
if(JOptionPane.showConfirmDialog(null, str, "",
JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
boom=(int)(Math.random()*8);
for (JButton button: list)
button.setBackground(Color.gray);
myPanel.setVisible(true);
}//restart the game
else System.exit(0);//exit the game
}
/**
* Compute the remain area to check whether the user won
*/
private boolean win() {
int count = 0;
boolean clear=false;
for(JButton panel:list) {
if(panel.getBackground() == Color.WHITE)
count++;
}
if(count == 8) clear=true;
return clear;
}
/**
* MyPanel:This is a inner class for setting panel
*/
public class MyPanel extends JPanel {
public void paintComponent(Graphics g){
g.drawLine(WIDTH/3,0,WIDTH/3,HEIGHT);
g.drawLine(WIDTH/3*2,0,WIDTH/3*2,HEIGHT);
g.drawLine(0,HEIGHT/3*1,WIDTH,HEIGHT/3*2);
}// draw the border of the button
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -