?? bombpanel.java~173~
字號(hào):
package bomb;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.event.*;
import java.awt.Dimension;
import javax.swing.JOptionPane;
public class BombPanel extends JPanel {
private Location location = new Location();
private int result[][] = new int[20][20];//當(dāng)前用戶(hù)的狀態(tài)
private int bombCount[][]=new int[20][20];//初始化,如果為-1,說(shuō)明是雷,否則為雷的個(gè)數(shù)
private int bombNumber=80;//定義雷的個(gè)數(shù)
private int isWin;
private int checkedBomb=0;
public BombPanel(int number) {
bombNumber=number;
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
public BombPanel() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.yellow);
g.fillRect(10, 10, 400, 400);
g.setColor(Color.BLUE);
g.draw3DRect(10, 10, 400, 400, false);
g.setColor(Color.BLUE);
for (int i = 1; i < 20; i++) {
g.drawLine(10, 10 + i * 20, 410, 10 + i * 20);
g.drawLine(10 + i * 20, 10, 10 + i * 20, 410);
}
g.drawString("Remains:"+String.valueOf(bombNumber-checkedBomb),20,423);
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
//已經(jīng)點(diǎn)擊,但不是雷
if (result[i][j] > 0) {
g.setColor(Color.black);
g.drawString(String.valueOf(result[i][j]), 18 + 20 * i, 25 + 20 * j);
}
//已經(jīng)點(diǎn)擊,但周?chē)鷽](méi)有雷
if (result[i][j]==0){
g.setColor(Color.white);
g.fill3DRect(10+20*i,10+20*j,20,20,false);
}
//點(diǎn)擊了雷
if(result[i][j]==-1){
g.setColor(Color.red);
g.fillOval(10+20*i,10+20*j,20,20);
}
//挖雷
if(result[i][j]==-3){
g.setColor(Color.PINK);
g.fillRect(10+20*i,10+20*j,20,20);
g.setColor(Color.red);
g.drawString("B", 18 + 20 * i, 25 + 20 * j);
}
}
}
}
private void jbInit() throws Exception {
this.addMouseListener(new BombPanel_this_mouseAdapter(this));
for(int i=0;i<20;i++){
for(int j=0;j<20;j++){
result[i][j]=-2;
bombCount[i][j]=0;
}
}
int count=0;
int x=0,y=0;
while(count<bombNumber){
x=(int)(Math.random()*20);
y=(int)(Math.random()*20);
if(bombCount[x][y]!=-1){
bombCount[x][y] = -1;
count++;
//JOptionPane.showMessageDialog(this,String.valueOf(x)+" "+String.valueOf(y));
}
}
for(int i=0;i<20;i++){
for (int j = 0; j < 20; j++) {
if(bombCount[i][j] ==0){
for(int m=((i-1>0)?i-1:0);m<((i+2<20)?i+2:20);m++)
for(int n=((j-1>0)?j-1:0);n<((j+2<20)?j+2:20);n++)
if (bombCount[m][n] == -1)
bombCount[i][j]+=1;
}
}
}
}
void this_mouseClicked(MouseEvent e) {
location.setXY(e.getX(),e.getY());
Dimension clickedLocation=location.getClickedLocation();
if(e.getButton()==1){
result[clickedLocation.width][clickedLocation.height]=bombCount[clickedLocation.width][clickedLocation.height];
if(result[clickedLocation.width][clickedLocation.height]==-1){
JOptionPane.showMessageDialog(this,"You Lose!!");
}
//if(result[clickedLocation.width][clickedLocation.height]==0)
for(int m=((clickedLocation.width-1>0)?clickedLocation.width-1:0);m<((clickedLocation.width+2<20)?clickedLocation.width+2:20);m++)
for(int n=((clickedLocation.height-1>0)?clickedLocation.height-1:0);n<((clickedLocation.height+2<20)?clickedLocation.height+2:20);n++)
if (bombCount[m][n] == 0){
click(m, n);
}
isWin=1;
checkedBomb=0;
for(int i=0;i<20;i++)
for(int j=0;j<20;j++){
if (result[i][j] == -2) isWin = 0;
if(result[i][j]==-3)checkedBomb+=1;
}
if(isWin==1&&checkedBomb==bombNumber)
repaint();
JOptionPane.showMessageDialog(this,"You Win!!!");
}
else result[clickedLocation.width][clickedLocation.height]=-3;
repaint();
}
private void click(int m,int n){
result[m][n]=bombCount[m][n];
for(int i=((m-1>0)?m-1:0);i<((m+2<20)?m+2:20);i++)
for(int j=((n-1>0)?n-1:0);j<((n+2<20)?n+2:20);j++)
if (bombCount[i][j] == 0&&result[i][j]==-2)
click(i,j);
else{
if (bombCount[i][j] > 0&&result[i][j]==-2)
result[i][j]=bombCount[i][j];
}
}
}
class BombPanel_this_mouseAdapter extends java.awt.event.MouseAdapter {
BombPanel adaptee;
BombPanel_this_mouseAdapter(BombPanel adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.this_mouseClicked(e);
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -