www.pudn.com > JavaBomb.rar > BombPanel.java


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];//当前用户的状态 
  private int bombCount[][]=new int[20][20];//初始化,如果为-1,说明是雷,否则为雷的个数 
  private int bombNumber=80;//定义雷的个数 
  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("Bomb Remains:"+String.valueOf(bombNumber-checkedBomb),20,423); 
 
    for (int i = 0; i < 20; i++) { 
      for (int j = 0; j < 20; j++) { 
        //已经点击,但不是雷 
        if (result[i][j] > 0) { 
          g.setColor(Color.black); 
          g.drawString(String.valueOf(result[i][j]), 18 + 20 * i, 25 + 20 * j); 
        } 
        //已经点击,但周围没有雷 
        if (result[i][j]==0){ 
          g.setColor(Color.white); 
          g.fill3DRect(10+20*i,10+20*j,20,20,false); 
        } 
 
        //点击了雷 
        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(count0)?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){ 
        repaint(); 
        JOptionPane.showMessageDialog(this,"You Lose!!"); 
      }else{ 
      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); 
            } 
      } 
    } 
    else{ 
      if(result[clickedLocation.width][clickedLocation.height] ==-3) 
        result[clickedLocation.width][clickedLocation.height] = -2; 
     else 
      result[clickedLocation.width][clickedLocation.height] = -3; 
    } 
 
    repaint(); 
 
    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) 
      JOptionPane.showMessageDialog(this, "You Win!!!"); 
  } 
 
  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); 
  } 
}