版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/itnerd/article/details/83034369
资源图片文件
就是随便搜了点图片,作为点击的按钮。最后效果如下:
闲话少说
package saolei;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Saolei
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
BombFrame frame = new BombFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//frame.setResizable(false);
}
});
}
}
class BombFrame extends JFrame
{
private BombMenuBar menubar;
private BombPanel panel;
private int NUMOFLINES;
private int NUMOFBOMBS;
public BombFrame()
{
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screen = kit.getScreenSize();
int w = (int)screen.getWidth();
int h = (int)screen.getHeight();
setBounds(w/4,h/4,w/2,h/2);
setTitle("wipe the Bombs");
menubar = new BombMenuBar();
setJMenuBar(menubar);
String numoflines = JOptionPane.showInputDialog("The bombfield is the square of :");
NUMOFLINES = Integer.parseInt(numoflines);
String numofbombs = JOptionPane.showInputDialog("The number of bombs is :");
NUMOFBOMBS = Integer.parseInt(numofbombs);
panel = new BombPanel(NUMOFLINES,NUMOFBOMBS);
add(panel);
pack();
}
class BombMenuBar extends JMenuBar
{
public BombMenuBar()
{
JMenu SetMenu = new JMenu("SET");
add(SetMenu);
JMenuItem ResetItem = SetMenu.add("RESET");
ResetItem.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
panel.reset();
repaint();pack();
}
}
);
}
}
}
class BombPanel extends JPanel
{
private ImageIcon img00 = new ImageIcon(getClass().getResource("00.jpg"));
private ImageIcon img0 = new ImageIcon(getClass().getResource("0.jpg"));
private ImageIcon img1 = new ImageIcon(getClass().getResource("1.jpg"));
private ImageIcon img2 = new ImageIcon(getClass().getResource("2.jpg"));
private ImageIcon img3 = new ImageIcon(getClass().getResource("3.jpg"));
private ImageIcon img4 = new ImageIcon(getClass().getResource("4.jpg"));
private ImageIcon img5 = new ImageIcon(getClass().getResource("5.jpg"));
private ImageIcon img6 = new ImageIcon(getClass().getResource("6.jpg"));
private ImageIcon img7 = new ImageIcon(getClass().getResource("7.jpg"));
private ImageIcon img8 = new ImageIcon(getClass().getResource("8.jpg"));
private ImageIcon img9 = new ImageIcon(getClass().getResource("9.jpg"));
private int NUMOFLINE ;
private int NUMOFBOMB ;
private int UNOPENFIELD ;
int[][] d = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
private BombButton[][] buttons;
public BombPanel(int numofline, int numofbomb)
{
NUMOFLINE = numofline;
NUMOFBOMB = numofbomb;
UNOPENFIELD = NUMOFLINE*NUMOFLINE;
buttons = new BombButton[NUMOFLINE][NUMOFLINE];
setLayout(new GridLayout(NUMOFLINE,NUMOFLINE));
int i,j;
for( i = 0; i < NUMOFLINE; ++i)
for(j = 0; j < NUMOFLINE; ++j )
{
buttons[i][j]= new BombButton(i,j);
buttons[i][j].addActionListener(new BombAction());
add(buttons[i][j]);
}
setBombPanel(NUMOFBOMB);
}
public void reset()
{
int i,j;
for( i = 0; i < NUMOFLINE; ++i)
for(j = 0; j < NUMOFLINE; ++j )
{
remove(buttons[i][j]);
buttons[i][j]= new BombButton(i,j);
buttons[i][j].addActionListener(new BombAction());
add(buttons[i][j]);
}
UNOPENFIELD = NUMOFLINE*NUMOFLINE;
setBombPanel(NUMOFBOMB);
}
public void setBombPanel(int n)
{
int x,y,i,j,k;
while(n > 0)
{
x = (int)(Math.random()*(NUMOFLINE*7))%(NUMOFLINE);
y = (int)(Math.random()*(NUMOFLINE*7))%(NUMOFLINE);
if(!buttons[x][y].isBomb())
{
buttons[x][y].setBomb();
for(k = 0; k < 8; ++k)
{
i = x + d[k][0];
j = y + d[k][1];
if(0 <= i && i < NUMOFLINE && 0 <= j && j < NUMOFLINE )
{
buttons[i][j].addNearBombs();
}
}
--n;
}
}
}
private class BombButton extends JButton
{
private int X,Y;
private boolean ISBOMB;
private boolean ISOPEN;
private int NEARBOMBS;
BombButton(int x, int y)
{
X = x;
Y = y;
ISBOMB = false;
ISOPEN = false;
NEARBOMBS = 0;
setIcon(img00);
setMargin(new Insets(0,0,0,0));//除去按钮上的缝隙
setOpaque(false);
}
public int getButtonX()
{
return X;
}
public int getButtonY()
{
return Y;
}
public void setBomb()
{
ISBOMB = true;
}
public boolean isBomb()
{
return ISBOMB;
}
public boolean isOpen()
{
return ISOPEN;
}
public void openButton()
{
ISOPEN = true;
}
public void addNearBombs()
{
++NEARBOMBS;
}
public int getNearBombs()
{
return NEARBOMBS;
}
}
private class BombAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
BombButton button = (BombButton)e.getSource();
if(button.isBomb())
{
for(int i = 0; i < NUMOFLINE; ++i)
for(int j = 0; j < NUMOFLINE; ++j)
{
if(buttons[i][j].isBomb() && !buttons[i][j].isOpen() )
{
buttons[i][j].setIcon(img9);
}
}
JOptionPane.showMessageDialog(null,"YOU FAILLED!!!");
//System.exit(0);
}
else
{
open(button);
}
//button.setEnabled(false);
}
public void open(BombButton button)
{
if(button.isOpen())return;
button.openButton();
UNOPENFIELD--;
if(UNOPENFIELD == NUMOFBOMB)
{
JOptionPane.showMessageDialog(null,"YOU WIN!!!O(>o<)O!!!");
}
int x,y,i,j,k;
x = button.getButtonX();
y = button.getButtonY();
switch (button.getNearBombs())
{
case 0:button.setIcon(img0);
{
for(k = 0; k < 8; ++k)
{
i = x + d[k][0];
j = y + d[k][1];
if(0 <= i && i < NUMOFLINE && 0 <= j && j < NUMOFLINE )
{
open(buttons[i][j]);
}
}
}break;
case 1:button.setIcon(img1);break;
case 2:button.setIcon(img2);break;
case 3:button.setIcon(img3);break;
case 4:button.setIcon(img4);break;
case 5:button.setIcon(img5);break;
case 6:button.setIcon(img6);break;
case 7:button.setIcon(img7);break;
case 8:button.setIcon(img8);break;
default:break;
}
}
}
}