「java扫雷如何载入图片」java扫雷如何载入图片里

博主:adminadmin 2022-12-10 01:57:10 66

本篇文章给大家谈谈java扫雷如何载入图片,以及java扫雷如何载入图片里对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java 怎么实现程序加载

分析文本内容然后重新加载.

我觉得是不是你保存的内容不够详细,所以分析起来就特别耗时,

比如地图上的有几个地雷的数字, 就可以先存起来

假设

已经打开的安全区域, 并且该按钮不用显示数字, 那么设置成 0

已经打开的安全区域,但按钮上需要显示数字,那么数字是几,那么设置成几

--1

--2

......

[没有标记]

没有打开的区域, 且上面没有点上地雷标记,且不是地雷的,那么数字可以设置成A

没有打开的区域, 且上面没有点上地雷标记,但是是地雷的,那么数字可以设置成B

[有标记]

没有打开的区域,有地雷标记, 且真的下面有地雷的,那么数字设置成C

没有打开的区域, 有地雷标记,但下面没有地雷的,那么数字设置成D

那么地图文件,可能存储的信息如下. (假设的,没有去推理过的)

02DCBA4AA1100

00123DBCDAB10

.....

这思路和RPG的游戏地图保存是这样的,

RPG地图划分出一个一个的格子来显示,所以保存的时候,保存每一个格子的数据就可以了,

没有设置ABC... 而全部使用数字, 0~N

比如

保存是数据如下:   行标,和每一行的数据

[0]=[12,89,101]

[1]=[13,72,12]

[2]=[3,10,56]

怎么用Java做一个扫雷程序,要原创。。。 做好了给加100

第一个JAVA文件

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

/**

* 显示所有按钮的面板

* @author Administrator

*

*/

public class AllButtonPanel extends JPanel implements ActionListener{

private int row;//行数

private int col;//列数

private int mineCount;//地雷数

private MineButton[][] allButtons;//所有按钮

public AllButtonPanel(int row,int col,int mineCount){

this.row=row;

this.col=col;

this.mineCount=mineCount;

allButtons=new MineButton[row][col];

createButtons();

createMine();

init();

}

private void init(){

this.setLayout(new GridLayout(row,col));

for(int i=0;iallButtons.length;i++){

for(int j=0;jallButtons[i].length;j++){

this.add(allButtons[i][j]);

}

}

}

/**

* 随机布雷的方法

*

*/

private void createMine(){

int n=0;

while(nmineCount){//随机生成mineCount个地雷

int i=(int)(Math.random()*row);

int j=(int)(Math.random()*col);

if(allButtons[i][j].getCountOfSurroundMines()!=-1){

allButtons[i][j].setCountOfSurroundMines(-1);

n++;

}

}

for(int i=0;iallButtons.length;i++){//计算每个位置的周围地雷数

for(int j=0;jallButtons[i].length;j++){

if(allButtons[i][j].getCountOfSurroundMines()!=-1){

allButtons[i][j].setCountOfSurroundMines(getSurroundMineCount(i,j));

}

}

}

}

/**

* 统计(i,j)坐标周围8个位置的地雷数

* @param data

* @param i

* @param j

* @return

*/

private int getSurroundMineCount(int i,int j){

int num=0;//统计周围的雷数

if(i-1=0j-1=0){

num+=(allButtons[i-1][j-1].getCountOfSurroundMines()==-1?1:0);

}

if(i-1=0){

num+=(allButtons[i-1][j].getCountOfSurroundMines()==-1?1:0);

}

if(i-1=0j+1allButtons[0].length){

num+=(allButtons[i-1][j+1].getCountOfSurroundMines()==-1?1:0);

}

if(j-1=0){

num+=(allButtons[i][j-1].getCountOfSurroundMines()==-1?1:0);

}

if(j+1allButtons[0].length){

num+=(allButtons[i][j+1].getCountOfSurroundMines()==-1?1:0);

}

if(i+1allButtons.lengthj-1=0){

num+=(allButtons[i+1][j-1].getCountOfSurroundMines()==-1?1:0);

}

if(i+1allButtons.length){

num+=(allButtons[i+1][j].getCountOfSurroundMines()==-1?1:0);

}

if(i+1allButtons.lengthj+1allButtons[0].length){

num+=(allButtons[i+1][j+1].getCountOfSurroundMines()==-1?1:0);

}

return num;

}

/**

* 生成按钮

*

*/

private void createButtons(){

for(int i=0;iallButtons.length;i++){

for(int j=0;jallButtons[i].length;j++){

allButtons[i][j]=new MineButton(i,j);

allButtons[i][j].setSize(6,6);

allButtons[i][j].addActionListener(this);//添加点击事件监听

allButtons[i][j].addMouseListener(new MouseAdapter(){//添加鼠标右键事件监听

public void mouseClicked(MouseEvent e) {

if(e.getButton()==MouseEvent.BUTTON3){

int remain=Integer.parseInt(CleanMine.remainMine.getText());

JButton b=(JButton)e.getSource();

if(b.getText().equals("")){

remain--;

CleanMine.remainMine.setText(remain+"");

b.setText("");

}else if(b.getText().equals("")){

remain++;

CleanMine.remainMine.setText(remain+"");

b.setText("");

}

}

}

});

}

}

}

public void actionPerformed(ActionEvent e) {//点击事件监听的方法

MineButton b=(MineButton)e.getSource();

int r=b.getRow();

int c=b.getCol();

if(allButtons[r][c].getCountOfSurroundMines()==-1){//如果是地雷

for(int i=0;iallButtons.length;i++){//把所有按钮都显示出来

for(int j=0;jallButtons[i].length;j++){

if(allButtons[i][j].getCountOfSurroundMines()==-1){//如果该位置是地雷

allButtons[i][j].setText("$");

}else if(allButtons[i][j].getCountOfSurroundMines()==0){//如果该位置为空(该位置不是地雷,周围8个位置也没有地雷)

allButtons[i][j].setText("");

allButtons[i][j].setBackground(Color.CYAN);

}else{//如果该位置不是地雷,但周围8个位置中有地雷

allButtons[i][j].setText(allButtons[i][j].getCountOfSurroundMines()+"");

allButtons[i][j].setBackground(Color.CYAN);

}

}

}

}else{//如果不是地雷

showEmpty(r,c);//执行排空操作

}

}

/**

* 排空方法,若(i,j)位置为空,则显示空白。然后依次递归找它周围的8个位置。

* @param data

* @param i

* @param j

*/

private void showEmpty(int i,int j){

MineButton b=allButtons[i][j];

if(b.isCleared()){

return;

}

if(allButtons[i][j].getCountOfSurroundMines()==0){

b.setBackground(Color.CYAN);

b.setCleared(true);

if(i-1=0j-1=0){

showEmpty(i-1,j-1);

}

if(i-1=0){

showEmpty(i-1,j);

}

if(i-1=0j+1allButtons[0].length){

showEmpty(i-1,j+1);

}

if(j-1=0){

showEmpty(i,j-1);

}

if(j+1allButtons[0].length){

showEmpty(i,j+1);

}

if(i+1allButtons.lengthj-1=0){

showEmpty(i+1,j-1);

}

if(i+1allButtons.length){

showEmpty(i+1,j);

}

if(i+1allButtons.lengthj+1allButtons[0].length){

showEmpty(i+1,j+1);

}

}else if(allButtons[i][j].getCountOfSurroundMines()0){

b.setText(allButtons[i][j].getCountOfSurroundMines()+"");

b.setBackground(Color.CYAN);

b.setCleared(true);

}

}

}

第二个JAVA文件

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

/**

* 扫雷游戏主界面

* @author tony.tang

*

*/

public class CleanMine extends JFrame implements ActionListener{

private JLabel text1,text2;

public static JLabel remainMine;//剩余地雷数

private JLabel time;//消耗时间

private JButton reset;//重新开始

private JPanel center;

private int row,col,mine;

public CleanMine(){

text1=new JLabel("剩余地雷:");

text2=new JLabel("消耗时间:");

remainMine=new JLabel("10");

time=new JLabel("0");

reset=new JButton("重新开始");

reset.addActionListener(this);

JMenuBar bar=new JMenuBar();

JMenu game=new JMenu("游戏");

JMenu help=new JMenu("帮助");

JMenuItem item;

game.add(item=new JMenuItem("开局"));item.addActionListener(this);

game.addSeparator();

ButtonGroup bg=new ButtonGroup();

game.add(item=new JCheckBoxMenuItem("初级",true));bg.add(item);item.addActionListener(this);

game.add(item=new JCheckBoxMenuItem("中级"));bg.add(item);item.addActionListener(this);

game.add(item=new JCheckBoxMenuItem("高级"));bg.add(item);item.addActionListener(this);

game.add(item=new JCheckBoxMenuItem("自定义..."));bg.add(item);item.addActionListener(this);

game.addSeparator();

game.add(item=new JMenuItem("退出"));item.addActionListener(this);

help.add(item=new JMenuItem("查看帮助"));item.addActionListener(this);

help.add(item=new JMenuItem("关于扫雷..."));item.addActionListener(this);

bar.add(game);

bar.add(help);

this.setJMenuBar(bar);

init();

}

private void init(){

JPanel north=new JPanel();

north.add(text1);

north.add(remainMine);

north.add(reset);

north.add(text2);

north.add(time);

this.add(north,BorderLayout.NORTH);

this.row=9;

this.col=9;

this.mine=10;

restart();

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

new Thread(){

public void run(){

while(Integer.parseInt(remainMine.getText())0){

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

time.setText((Integer.parseInt(time.getText())+1)+"");

}

}

}.start();

}

public void actionPerformed(ActionEvent e) {

if(e.getActionCommand().equals("初级")){

this.row=9;

this.col=9;

this.mine=10;

restart();

return;

}

if(e.getActionCommand().equals("中级")){

this.row=16;

this.col=16;

this.mine=40;

restart();

return;

}

if(e.getActionCommand().equals("高级")){

this.row=16;

this.col=30;

this.mine=99;

restart();

return;

}

if(e.getActionCommand().equals("重新开始")){

restart();

return;

}

}

private void restart(){

if(center!=null){

this.remove(center);

}

center=new AllButtonPanel(row,col,mine);

this.add(center,BorderLayout.CENTER);

this.remainMine.setText(mine+"");

this.time.setText("0");

this.setSize(col*30,row*30+10);

this.setResizable(false);

this.setVisible(true);

}

/**

* @param args

*/

public static void main(String[] args) {

new CleanMine();

}

}

第三个JAVA文件.

import javax.swing.JButton;

import java.awt.*;

public class MineButton extends JButton {

private int row;

private int col;

private boolean cleared=false;

private int countOfSurroundMines;//周围地雷数,如果本按钮是雷,则为-1;

public MineButton(int row,int col){

this.row=row;

this.col=col;

this.setMargin(new Insets(0,0,0,0));

}

public int getCol() {

return col;

}

public int getRow() {

return row;

}

public boolean isCleared() {

return cleared;

}

public void setCleared(boolean cleared) {

this.cleared = cleared;

}

public int getCountOfSurroundMines() {

return countOfSurroundMines;

}

public void setCountOfSurroundMines(int countOfSurroundMines) {

this.countOfSurroundMines = countOfSurroundMines;

}

}

全部编译以后就可以执行了

怎样用JAVA实现扫雷游戏

要详细代码?还是只要启动?

java编写实现,代码如下:import Java.awt.*;

import java.awt.event.*;

import javax.Swing.*;

/*按扭类*/

class Bomb extends JButton

{

public int num_x,num_y; //第几号方块

public int BombRoundCount; //周围雷数

public boolean isBomb; //是否为雷

public boolean isClicked; //是否被点击

public int BombFlag; //探雷标记

public boolean isRight; //是否点击右键

public Bomb(int x,int y)

{

BombFlag = 0;

num_x = x;

num_y = y;

BombRoundCount = 0;

isBomb = false;

isClicked = false;

isRight = false;

}

}

/*窗口及算法实现类*/

class MainBomb extends JFrame implements ActionListener,MouseListener

{

public JTextField text;

public Label nowBomb,setBomb;

public int BlockNum,BombNum; //当前方块数当前雷数

public Icon icon_bomb = new ImageIcon("Bomb.gif"); //踩雷

public Icon icon_bomb_big = new ImageIcon("bomb_big.gif"); //踩雷标记

public Icon icon_flag = new ImageIcon("flag.gif"); //雷标记

public Icon icon_question = new ImageIcon("question.gif"); //疑惑是否有雷

public JButton start = new JButton(" 开始 ");

public Panel MenuPamel = new Panel();

public Panel mainPanel = new Panel();

public Bomb[][] bombButton;

/*界面设计*/

public MainBomb()

{

super("扫雷 Aaron2004制作 2004.8 ");

BlockNum = 64;

BombNum = 10;

Container c=getContentPane();

c.setBackground(Color.gray);

c.setLayout(new BorderLayout());

text=new JTextField("10 ",3);

nowBomb = new Label("当前雷数"+" "+BombNum+"");

setBomb= new Label("设置地雷数");

start.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

{

BombNum = Integer.parseInt(text.getText().trim());

if(BombNum = 10 BombNum 50 )

replay();

else

{

JOptionPane msg = new JOptionPane();

JOptionPane.showMessageDialog(null,"您设置的地雷数太多了,请重设!","错误",2);

}

}

} );

MenuPamel.add(setBomb);

MenuPamel.add(text);

MenuPamel.add(start);

MenuPamel.add(nowBomb);

c.add(MenuPamel,"North");

mainPanel.setLayout(new GridLayout( (int)Math.sqrt(BlockNum) , (int)Math.sqrt(BlockNum)) );

bombButton=new Bomb[ (int)Math.sqrt(BlockNum) ][];

for(int i = 0 ; i (int)Math.sqrt(BlockNum) ; i++)

{

bombButton[ i ]=new Bomb[ (int)Math.sqrt(BlockNum) ];

}

for(int i = 0 ; i (int)Math.sqrt(BlockNum) ; i++ )

for(int j = 0 ; j (int)Math.sqrt(BlockNum) ; j++ )

{

bombButton[ i ][ j ]=new Bomb(i,j);

bombButton[ i ][ j ].setForeground( Color.gray);

bombButton[ i ][ j ].addActionListener(this);

bombButton[ i ][ j ].addMouseListener(this);

}

for(int i = 0 ; i (int)Math.sqrt(BlockNum) ; i++ )

for(int j = 0 ; j (int)Math.sqrt(BlockNum) ; j++ )

mainPanel.add(bombButton[ i ][ j ]);

c.add(mainPanel,"Center");

startBomb();

setSize(400,400);

setLocation(350,200);

setResizable(false);

}

/*布雷*/

public void startBomb()

{

for(int i=0;iBombNum;i++)

{

int x =(int)(Math.random()*(int)(Math.sqrt(BlockNum)-1));

int y =(int)(Math.random()*(int)(Math.sqrt(BlockNum)-1));

if(bombButton[ x ][ y ].isBomb==true)

i--;

else

bombButton[ x ][ y ].isBomb=true ;

}

}

/*重新开始*/

public void replay()

{

nowBomb.setText("当前雷数"+" "+BombNum+"");

for(int i = 0 ; i (int)Math.sqrt(BlockNum) ; i++)

for(int j = 0 ; j (int)Math.sqrt(BlockNum) ; j++)

{

bombButton[ i ][ j ].isBomb=false;

bombButton[ i ][ j ].isClicked=false;

bombButton[ i ][ j ].setEnabled(true);

bombButton[ i ][ j ].setText("");

bombButton[ i ][ j ].setIcon(null);

}

startBomb();

}

/*是否挖完了所有的雷*/

public void isWin()

{

int findBomb=0; //找到的地雷数

for(int i = 0;i (int)Math.sqrt(BlockNum) ; i++)

for(int j = 0;j (int)Math.sqrt(BlockNum ); j++)

{

if(bombButton[ i ][ j ].isBomb == true bombButton[ i ][ j ].isRight == true)

findBomb++;

}

if( findBomb == Integer.parseInt(text.getText().trim()) )

{

JOptionPane msg = new JOptionPane();

JOptionPane.showMessageDialog(this,"您挖完了所有的雷,您胜利了!","您胜利了",2);

}

}

/*计算方块周围雷数 */

public void CountRoundBomb()

{

for (int i = 0; i (int)Math.sqrt(BlockNum); i++) {

for (int j = 0; j (int)Math.sqrt(BlockNum); j++) {

int count = 0;

//当需要检测的单元格本身无地雷的情况下,统计周围的地雷个数

if (bombButton[ i ][ j ].isBomb != true) {

if ( (i - 1 = 0) (j - 1 = 0)) {

if (bombButton[i - 1][j - 1].isBomb == true) {

count += 1; //检测左上方空格是否是地雷

}

}

if ( (i - 1 = 0)) {

if (bombButton[i - 1][ j ].isBomb == true) {

count += 1; //检测上方空格是否为地雷

}

}

if ( (i - 1 = 0) (j + 1 = (int)Math.sqrt(BlockNum)-1)) {

if (bombButton[i - 1][j + 1] .isBomb == true) {

count += 1; //检测右上方是否为地雷

}

}

if ( (j - 1 = 0)) {

if (bombButton[ i ][j - 1] .isBomb == true) {

count += 1; //检测左边是否为地雷

}

}

if ( (i = 0) (j + 1 = (int)Math.sqrt(BlockNum)-1)) {

if (bombButton[ i ][j + 1].isBomb == true) {

count += 1; //右边

}

}

if ( (j - 1 = 0) (i + 1 = (int)Math.sqrt(BlockNum)-1)) {

if (bombButton[i + 1][j - 1].isBomb == true) {

count += 1; //左下

}

}

if ( (i + 1 = (int)Math.sqrt(BlockNum)-1)) {

if (bombButton[i + 1][ j ].isBomb == true) {

count += 1; //下

}

}

if ( (j + 1 = (int)Math.sqrt(BlockNum)-1) (i + 1 = Math.sqrt(BlockNum)-1)) {

if (bombButton[i + 1][j + 1].isBomb == true) {

count += 1; //右下

}

}

bombButton[ i ][ j ].BombRoundCount = count;

}

}

}

}

/**当选中的位置为空,则翻开周围的地图**/

public void isNull(Bomb[][] bombButton,Bomb ClickecButton)

{

int i,j;

i=ClickecButton.num_x;

j=ClickecButton.num_y;

if (ClickecButton.isBomb==true) {

}

else {

if ( (i - 1 = 0) (j - 1 = 0)) { //检测左上方空格是否是空

if (bombButton[i - 1][j - 1].isBomb == false bombButton[i - 1][j - 1].isClicked == false bombButton[i - 1][j - 1].isRight == false) {

bombButton[i - 1][j - 1].setText((bombButton[i - 1][j - 1].BombRoundCount)+"");

bombButton[i - 1][j - 1].setEnabled(false);

bombButton[i - 1][j - 1].isClicked=true;

}

}

if ( (i - 1 = 0)) { //检测上方空格是否为空

if (bombButton[i - 1][ j ] .isBomb == false bombButton[i - 1][ j ].isClicked == false bombButton[i - 1][ j ].isRight == false) {

bombButton[i - 1][ j ].setText((bombButton[i - 1][ j ].BombRoundCount)+"");

bombButton[i - 1][ j ].setEnabled(false);

bombButton[i - 1][ j ].isClicked=true;

}

}

if ( (i - 1 = 0) (j + 1 = ((int)Math.sqrt(BlockNum)-1)) ) { //检测右上方是否为空

if (bombButton[i - 1][j + 1] .isBomb == false bombButton[i - 1][j + 1].isClicked == false bombButton[i - 1][j + 1].isRight == false) {

bombButton[i - 1][j + 1].setText((bombButton[i - 1][j + 1].BombRoundCount)+"");

bombButton[i - 1][j + 1].setEnabled(false);

bombButton[i - 1][j + 1].isClicked=true;

}

}

if ( (j - 1 = 0)) { //检测左边是否为空

if (bombButton[ i ][j - 1].isBomb == false bombButton[ i ][j - 1].isClicked == false bombButton[ i ][j - 1].isRight == false) {

bombButton[ i ][j - 1].setText((bombButton[ i ][j - 1].BombRoundCount)+"");

bombButton[ i ][j - 1].setEnabled(false);

bombButton[ i ][j - 1].isClicked=true;

}

}

if ( (i = 0) (j + 1 = ((int)Math.sqrt(BlockNum)-1)) ) { //检测右边空格是否是空

if (bombButton[ i ][j + 1].isBomb == false bombButton[ i ][j + 1].isClicked == false bombButton[ i ][j + 1].isRight == false) {

bombButton[ i ][j + 1].setText((bombButton[ i ][j + 1].BombRoundCount)+"");

bombButton[ i ][j + 1].setEnabled(false);

bombButton[ i ][j + 1].isClicked=true;

}

}

if ( (j - 1 = 0) (i + 1 = ((int)Math.sqrt(BlockNum)-1)) ) { //检测左下空格是否是空

if (bombButton[i + 1][j - 1].isBomb == false bombButton[i + 1][j - 1].isClicked == false bombButton[i + 1][j - 1].isRight == false) {

bombButton[i + 1][j - 1].setText((bombButton[i + 1][j - 1].BombRoundCount)+"");

bombButton[i + 1][j - 1].setEnabled(false);

bombButton[i + 1][j - 1].isClicked=true;

}

}

if ( (i + 1 = ((int)Math.sqrt(BlockNum)-1)) ) { //检测下边空格是否是空

if (bombButton[i + 1][ j ].isBomb == false bombButton[i + 1][ j ].isClicked == false bombButton[i + 1][ j ].isRight == false) {

bombButton[i + 1][ j ].setText((bombButton[i + 1][ j ].BombRoundCount)+"");

bombButton[i + 1][ j ].setEnabled(false);

bombButton[i + 1][ j ].isClicked=true;

}

}

if ( (j + 1 = ((int)Math.sqrt(BlockNum)-1) ) (i + 1 = ((int)Math.sqrt(BlockNum)-1)) ) { //检测右下边空格是否是空

if (bombButton[i + 1][j + 1].isBomb == false bombButton[i + 1][j + 1].isClicked == false bombButton[i + 1][j + 1].isRight == false) {

bombButton[i + 1][j + 1].setText((bombButton[i + 1][j + 1].BombRoundCount)+"");

bombButton[i + 1][j + 1].setEnabled(false);

bombButton[i + 1][j + 1].isClicked=true;

}

}

if ( (i - 1 = 0) (j - 1 = 0))//检测左上

isNull(bombButton,bombButton[i - 1][j - 1]);

if ( (i - 1 = 0))

isNull( bombButton,bombButton[i - 1][ j ]);//检测上方

if ( (i - 1 = 0) (j + 1 = (int)Math.sqrt(BlockNum)-1))

isNull( bombButton,bombButton[i - 1][j + 1]);//检测右上

if ( (j - 1 = 0))

isNull(bombButton,bombButton[i][j - 1]);//检测左边

if ( (i = 0) (j + 1 = ((int)Math.sqrt(BlockNum)-1)) )

isNull(bombButton,bombButton[i][j + 1]);//检测右边

if ( (j - 1 = 0) (i + 1 = ((int)Math.sqrt(BlockNum)-1)) )

isNull(bombButton,bombButton[i + 1][j - 1]); //检测左下

if ( (i + 1 = ((int)Math.sqrt(BlockNum)-1)) ) //检测下

isNull(bombButton,bombButton[i + 1][ j ]);

if ( (j + 1 = ((int)Math.sqrt(BlockNum)-1)) (i + 1 = ((int)Math.sqrt(BlockNum)-1)) ) //检测右下

isNull(bombButton,bombButton[i + 1][j + 1]);

}

}

public void actionPerformed(ActionEvent e)

{

CountRoundBomb();

if(((Bomb)e.getSource()).isBomb==false ((Bomb)e.getSource()).isClicked == false)

{

((Bomb)e.getSource()).setText(( ((Bomb)e.getSource()).BombRoundCount )+"");

((Bomb)e.getSource()).isClicked=true;

((Bomb)e.getSource()).setIcon(null);

((Bomb)e.getSource()).setEnabled(false);

if((((Bomb)e.getSource()).BombRoundCount) == 0)

isNull(bombButton,(Bomb)e.getSource());

isWin();

}

else if(((Bomb)e.getSource()).isBomb == true)

{

for(int i=0;i(int)Math.sqrt(BlockNum);i++)

for(int j=0;j(int)Math.sqrt(BlockNum);j++)

{

if(bombButton[ i ][ j ].isBomb == true)

bombButton[ i ][ j ].setIcon(icon_bomb);

}

((Bomb)e.getSource()).setIcon(icon_bomb_big);

JOptionPane msg = new JOptionPane();

JOptionPane.showMessageDialog(this,"你踩到地雷了,按确定重来","你踩到地雷了",2);

replay();

}

}

public void mouseClicked(MouseEvent e)

{

Bomb bombSource = (Bomb)e.getSource();

boolean right = SwingUtilities.isRightMouseButton(e);

if((right == true) (bombSource.isClicked == false))

{

bombSource.BombFlag = (bombSource.BombFlag + 1)%3;

if(bombSource.BombFlag == 1)

{

if(BombNum 0 bombSource.isRight == false ){

bombSource.setIcon(icon_flag);

bombSource.isRight = true;

BombNum--;

}

isWin();

nowBomb.setText("当前雷数"+" "+BombNum+"");

}

else if(bombSource.BombFlag == 2)

{

if( (BombNum !=0 ) ||(BombNum ==0 (bombSource.getIcon()==icon_flag)) )

BombNum++;

bombSource.setIcon(icon_question);

nowBomb.setText("当前雷数"+" "+BombNum+"");

}

else if(bombSource.BombFlag == 0)

{

bombSource.setIcon(null);

bombSource.isRight = false;

}

}

}

public void mouseEntered(MouseEvent e)

{}

public void mouseReleased(MouseEvent e)

{}

public void mouseExited(MouseEvent e)

{}

public void mousePressed(MouseEvent e)

{}

}

/*主类*/

public class Main

{

public static void main(String args[])

{

(new MainBomb()).show();

}

}

用java做扫雷界面怎么做?

jframe 背景图片可以自己花 也可以找现成的 然后在图片上用画笔G 在上面画格子什么的 都行

java 如何载入本机图片并实现点击获得任意点坐标

//提示:坐标依次打印在命令符窗口

//提示:坐标依次打印在命令符窗口

//提示:坐标依次打印在命令符窗口

//不就是监听鼠标事件吗?

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.border.Border;

/**

*我想建立个界面,可以加载本机中图片。

*加载后可以通过鼠标点击获得图片上任意点坐标。

*提问者: sunny929929 - 试用期 一级

*/

public class MyPicture extends JFrame implements MouseListener{

private JLabel tipLabel;

/**

*main()

*/

public static void main(String[] args){

MyPicture frame = new MyPicture();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

/**

*constructor

*/

public MyPicture(){

setSize(800, 600);//根据要求调整大小

setLocation(100,100);

setTitle("获得图片上任意点坐标");

setResizable(false);

Container con=getContentPane();

ImageIcon bgIcon=new ImageIcon("bgpic.jpg");//注意图片的路径

ImagePanel backpicPanel=new ImagePanel(bgIcon);

backpicPanel.addMouseListener(this);

con.add(backpicPanel,BorderLayout.CENTER);

tipLabel=new JLabel("--------------------提示:坐标依次打印在屏幕上!--------------------");

con.add(tipLabel,BorderLayout.SOUTH);

}

/**

*

*/

public void mousePressed(MouseEvent e){

int x=e.getX();

int y=e.getY();

String message="("+x+","+y+")";

tipLabel.setText(message);

System.out.println(message);

}

public void mouseReleased(MouseEvent e){

}

public void mouseEntered(MouseEvent e){

}

public void mouseExited(MouseEvent e){

}

public void mouseClicked(MouseEvent e){

}

}

/**

*类ImagePanel,用于添加背景图片

*/

class ImagePanel extends JPanel{

private Image img;

public ImagePanel (ImageIcon imageIcon){

img=imageIcon.getImage();

}

public void paintComponent(Graphics g){

super.paintComponent(g);

g.drawImage(img,0,0,this);

}

}

java载入图片只载入图片的一部分,求代码

import java.awt.Graphics;

import java.awt.Image;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class TestFrame extends JFrame {

public TestFrame() {

MyJPanel mp = new MyJPanel();

add(mp);

setTitle("画图");

setDefaultCloseOperation(EXIT_ON_CLOSE);

setBounds(300, 200, 300, 300);

setVisible(true);

}

public static void main(String[] args) {

new TestFrame();

}

class MyJPanel extends JPanel {

public void paint(Graphics g) {

super.paint(g);

Image img = new ImageIcon("C:\\Users\\gyona\\Desktop\\12.jpg").getImage();

g.drawImage(img, 0, 0, 100, 100, 100, 100, 200, 200, this);

//下面2组参数,确定画在JPanel的什么位置上

//0, 0//左上顶点

//100,100//确定画在JPanel时候的长和宽

//下面2组参数,用于决定选取图片的什么区域

//100,100 //选取图片的左顶点

//200,200 右下顶点

}

}

}

效果

关于java扫雷如何载入图片和java扫雷如何载入图片里的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-10,除非注明,否则均为首码项目网原创文章,转载请注明出处。