📄 buttonmine.java
字号:
//This class is the basic one, it can computer the number of the button, also is the
// number of the mine number of a grid’s around. In general, its feature is to form the areas’
// content and some button events’ realized.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class ButtonMine extends JButton {
int x,y,mineCounter,inButton,numb;
boolean markedMine,selectShow;
ButtonMine[] button = new ButtonMine[1000];
private Random rand = new Random();
SweepMine sweeper = new SweepMine();
int mainSizeX, mainSizeY;
int mineNumber,realmineNumber;
Icon noneIcon = new ImageIcon();
int totalInButton = 0 ;
public void generalBuilt( JPanel container ){
totalInButton=0;
mineNumber = 10;
realmineNumber = 10;
mainSizeX = 10;
mainSizeY = 10;
for ( numb =0 ; numb < mainSizeX*mainSizeY; numb ++ )
{ button[ numb ]= new ButtonMine();
button[ numb ].setBackground(Color.lightGray);
button[ numb ].x = numb / mainSizeY;
button[ numb ].y = numb % mainSizeY;
button[ numb ].mineCounter = 0;
button[ numb ].inButton = 0;
button[ numb ].markedMine = false;
button[ numb ].selectShow = false;
button[ numb ].addMouseListener( new MyMouseListener() );
container.add( button[ numb ] );
}
for ( int j = 0 ; j < realmineNumber ; )
{ int num = rand.nextInt( numb-1 );
if ( button[num].inButton == 0 )
{ button[num].inButton = 1; j ++ ;}
}
CountMine number = new CountMine(button );
}
public class CountMine {
private int[][] move = new int[][]{
{-1,-1}, { 1, -1}, {0,-1}, {-1,0},
{0,1},{1,0},{1,1},{-1,1}};
public CountMine( ButtonMine[] button ){
int col , row ;
int seatX, seatY ;
int mineIndex;
for ( row = 0 ; row < 10 ; row ++ )
for ( col = 0 ; col < 10 ; col ++ )
{ mineIndex = row * 10 + col ;
if ( button [ mineIndex ].inButton==1 )
{ for ( int j = 0 ; j < 8 ; j ++ )
{ seatX = row + move [ j ][ 0 ];
seatY = col + move [ j ][ 1 ];
if ( seatX >=0 && seatX < 10 && seatY >=0 && seatY < 10)
{ int mineIndex2 = seatX * 10 + seatY;
if ( button[ mineIndex2 ].inButton == 0 )
button [ mineIndex2 ].mineCounter ++;
}
}
}
}
}
}
private class MyMouseListener extends MouseAdapter{
public void mouseClicked( MouseEvent event){
Icon mineIcon = new ImageIcon("mine.gif");
Icon markIcon = new ImageIcon("mark.gif");
if ( totalInButton ==0 )
if ( event.isMetaDown())
{ int i ;
for ( i = 0 ; i < button.length; i ++ )
if (event.getSource()==button [ i ] )
break;
if ( button [ i ].selectShow == false )
{ if ( button [ i ].markedMine ==false )
{ button [ i ].setIcon(markIcon);
button [ i ].markedMine = true;
mineNumber--;
MineSweepMain.downMine() ;
if ( button [ i ].inButton == 1 )
realmineNumber --;
}
else if ( button [ i ].markedMine ==true)
{ button [ i ].setIcon(noneIcon);
button [ i ].markedMine = false;
mineNumber ++;
MineSweepMain.addMine() ;
if ( button [ i ].inButton ==1 )
realmineNumber ++;
}
}
if ( mineNumber ==0 && realmineNumber ==0 )
JOptionPane.showMessageDialog(null, " Winner! Congratulations! ");
else if ( mineNumber ==0 && realmineNumber!=0 )
JOptionPane.showMessageDialog(null, " Be careful! Missed Some Mines !");
}
else{ int i ;
for ( i = 0 ; i < button.length ; i ++ )
if ( event.getSource() == button [ i ])
break;
int mineNum = button [ i ].mineCounter;
char mineCh = (char) ( mineNum + 48 ) ;
if ( button [i ].markedMine == true || button [i ].selectShow ==true ){}
else if ( button [ i ].inButton == 0 && button [ i ].mineCounter!= 0)
{ button [ i ].setText( mineCh+"" );
button [ i ].selectShow = true;
}
else if ( button [ i ].inButton ==1 )
{
JOptionPane.showMessageDialog(null, " Touched the mine,and be lost!");
for ( int j = 0 ; j < mainSizeX*mainSizeY ; j ++ )
if ( button [ j ].inButton== 1 )
button [ j ].setIcon(mineIcon);
totalInButton = 1 ;
}
else if ( button [ i ].mineCounter == 0 )
sweeper.bfsSweeper(button , i );
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -