最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java?Swing實(shí)現(xiàn)掃雷源碼

 更新時(shí)間:2022年06月06日 17:26:00   作者:掉隊(duì)洋先生  
這篇文章主要為大家詳細(xì)介紹了Java?Swing實(shí)現(xiàn)掃雷源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Java Swing實(shí)現(xiàn)掃雷源碼的具體代碼,供大家參考,具體內(nèi)容如下

先來看下效果

運(yùn)行時(shí)只需要?jiǎng)?chuàng)建一個(gè)GameWindow的對(duì)象即可,可使用有參構(gòu)造函數(shù)自定義雷區(qū)行列數(shù)及炸彈個(gè)數(shù),也可使用無參構(gòu)造函數(shù)設(shè)置默認(rèn)值(小旗和炸彈的圖標(biāo)自己去找吧,我就不在這里放了)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;

public class GameWindow{

? ? private static Font labelFont = new Font("隸書",Font.BOLD,45);
? ? private static Font buttonFont = new Font("隸書",Font.CENTER_BASELINE,20);

? ? private static Icon flag = new ImageIcon("src/images/小旗.jpg");//小旗圖標(biāo)
? ? private static Icon bomb = new ImageIcon("src/images/炸彈.png");//炸彈圖標(biāo)

? ? private static int labelWeight = 30;//小方塊(雷區(qū))寬度
? ? private static int labelHeigth = 30;//小方塊(雷區(qū))高度
? ? private static int labelSpace = 1;//小方塊(雷區(qū))間距

? ? private static int scoreHeight = 50;//積分區(qū)域高度

? ? private static int[] x8 = {-1,-1,-1,0,0,1,1,1};//原坐標(biāo)周圍 8 個(gè)坐標(biāo)的 x 值變化
? ? private static int[] y8 = {-1,0,1,-1,1,-1,0,1};//原坐標(biāo)周圍 8 個(gè)坐標(biāo)的 y 值變化

? ? private static int[] x4 = {0,0,1,-1};//原坐標(biāo)周圍 4 個(gè)坐標(biāo)的 x 值變化
? ? private static int[] y4 = {1,-1,0,0};//原坐標(biāo)周圍 4 個(gè)坐標(biāo)的 x 值變化

? ? private JFrame frame;

? ? private int[][] data;//存儲(chǔ)和炸彈位置有關(guān)數(shù)據(jù),值為-1時(shí)表示炸彈
? ? private JLabel[][] labels;

? ? private JPanel mainPanel;//雷區(qū)主面板
? ? private JPanel scorePanel;//積分區(qū)域面板

? ? private JLabel areaLabel;//未掃雷區(qū)個(gè)數(shù)標(biāo)簽
? ? private JLabel bombLabel;//剩余地雷數(shù)標(biāo)簽

? ? private int width;//窗體寬度
? ? private int height;//窗體高度

? ? private int row;//行數(shù)
? ? private int column;//列數(shù)
? ? private int bombNum;//炸彈個(gè)數(shù)

? ? private int remainArea;//未掃雷區(qū)個(gè)數(shù)
? ? private int remainBomb;//剩余地雷數(shù)

? ? public GameWindow(int row,int column,int bombNum){
? ? ? ? frame = new JFrame("掃雷-洋仔小游戲");
? ? ? ? this.row = row;
? ? ? ? this.column = column;
? ? ? ? this.bombNum = bombNum;
? ? ? ? mainPanel = new JPanel();
? ? ? ? init();
? ? }

? ? public GameWindow(){
? ? ? ? this(10,10,20);
? ? }

? ? private void setWidthAndHeight(){
? ? ? ? width = labelWeight * column + (column + 1) * labelSpace;
? ? ? ? height = scoreHeight + labelHeigth * row + (row + 1) * labelSpace;
? ? }


? ? private void setBomb(){
? ? ? ? if(bombNum > row * column)
? ? ? ? ? ? throw new RuntimeException("炸彈數(shù)設(shè)置過多!");
? ? ? ? data = new int[row][column];
? ? ? ? int count = 0;
? ? ? ? while (count < bombNum){
? ? ? ? ? ? int r = (int)(Math.random() * row);
? ? ? ? ? ? int c = (int)(Math.random() * column);
? ? ? ? ? ? System.out.println("r = " + r + "\tc = " + c);

? ? ? ? ? ? if(data[r][c] == -1)
? ? ? ? ? ? ? ? continue;

? ? ? ? ? ? data[r][c] = -1;
? ? ? ? ? ? for(int i = 0; i < 8; i ++){
? ? ? ? ? ? ? ? int x = r + x8[i];
? ? ? ? ? ? ? ? int y = c + y8[i];
? ? ? ? ? ? ? ? if(x < 0 || x >= row || y < 0 || y >= column || data[x][y] == -1)
? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? data[r + x8[i]][c + y8[i]] ++;
? ? ? ? ? ? }
? ? ? ? ? ? count ++;
? ? ? ? ? ? System.out.println(count);
? ? ? ? }
? ? }

? ? private class ButtonListener extends MouseAdapter {

? ? ? ? private JButton button;
? ? ? ? private int r;
? ? ? ? private int w;
? ? ? ? private GameWindow window;
? ? ? ? private boolean disabled = true;

? ? ? ? public ButtonListener(JButton button,int r,int w,GameWindow window){
? ? ? ? ? ? this.button = button;
? ? ? ? ? ? this.r = r;
? ? ? ? ? ? this.w = w;
? ? ? ? ? ? this.window = window;
? ? ? ? }

? ? ? ? @Override
? ? ? ? public void mouseClicked(MouseEvent mouseEvent) {
? ? ? ? ? ? synchronized (window){
? ? ? ? ? ? ? ? if(!button.isEnabled())
? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? if(mouseEvent.getButton() == MouseEvent.BUTTON3){
? ? ? ? ? ? ? ? ? ? if(disabled) {
? ? ? ? ? ? ? ? ? ? ? ? button.setIcon(flag);
? ? ? ? ? ? ? ? ? ? ? ? data[r][w] -= 20;
? ? ? ? ? ? ? ? ? ? ? ? remainBomb --;
? ? ? ? ? ? ? ? ? ? ? ? remainArea --;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else {
? ? ? ? ? ? ? ? ? ? ? ? button.setIcon(null);
? ? ? ? ? ? ? ? ? ? ? ? data[r][w] += 20;
? ? ? ? ? ? ? ? ? ? ? ? remainBomb ++;
? ? ? ? ? ? ? ? ? ? ? ? remainArea ++;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? disabled = !disabled;
? ? ? ? ? ? ? ? ? ? bombLabel.setText("" + remainBomb);
? ? ? ? ? ? ? ? ? ? areaLabel.setText("" + remainArea);
? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if(!disabled)
? ? ? ? ? ? ? ? ? ? return;

? ? ? ? ? ? ? ? int cnt = 1;
? ? ? ? ? ? ? ? button.setVisible(false);
? ? ? ? ? ? ? ? if(data[r][w] == -1){
? ? ? ? ? ? ? ? ? ? gameOver();
// ? ? ? ? ? ? ? ? ? ?System.out.println("\n點(diǎn)到炸彈,全體按鈕已禁用");
? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? LinkedList<int[]> stack = new LinkedList<>();
? ? ? ? ? ? ? ? stack.push(new int[]{r,w});
? ? ? ? ? ? ? ? data[r][w] = -1;
? ? ? ? ? ? ? ? while (stack.size() > 0){
? ? ? ? ? ? ? ? ? ? int[] source = stack.pop();
? ? ? ? ? ? ? ? ? ? for(int i = 0; i < 4; i ++){
? ? ? ? ? ? ? ? ? ? ? ? int x = source[0] + x4[i];
? ? ? ? ? ? ? ? ? ? ? ? int y = source[1] + y4[i];
? ? ? ? ? ? ? ? ? ? ? ? if(x < 0 || x >= row || y < 0 || y >= column || data[x][y] < 0)
? ? ? ? ? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? ? ? ? ? if(data[x][y] == 0)
? ? ? ? ? ? ? ? ? ? ? ? ? ? stack.push(new int[]{x,y});
? ? ? ? ? ? ? ? ? ? ? ? labels[x][y].getLabelFor().setVisible(false);
? ? ? ? ? ? ? ? ? ? ? ? cnt ++;
? ? ? ? ? ? ? ? ? ? ? ? data[x][y] = -1;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? remainArea -= cnt;
? ? ? ? ? ? ? ? areaLabel.setText("" + remainArea);
? ? ? ? ? ? ? ? if(remainArea == remainBomb){
? ? ? ? ? ? ? ? ? ? gameOver();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }

? ? }

? ? private void gameOver(){
? ? ? ? for(JLabel[] ls : labels){
? ? ? ? ? ? for(JLabel l : ls){
? ? ? ? ? ? ? ? JButton button = (JButton) l.getLabelFor();
? ? ? ? ? ? ? ? button.setEnabled(false);
? ? ? ? ? ? }
? ? ? ? }
? ? }

? ? private void init(){
? ? ? ? setWidthAndHeight();

? ? ? ? frame.setBounds(380,100,width + 9,height + 32);
? ? ? ? frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
? ? ? ? frame.setResizable(false);
? ? ? ? frame.setLayout(null);

? ? ? ? mainPanel.setBackground(Color.GRAY);
? ? ? ? mainPanel.setBounds(1,scoreHeight + 1,width,height);
? ? ? ? mainPanel.setLayout(null);

? ? ? ? scorePanel = new JPanel();
? ? ? ? scorePanel.setBounds(0,0,width,scoreHeight);
? ? ? ? scorePanel.setLayout(new GridLayout(1,3,0,0));

? ? ? ? areaLabel = new JLabel();

? ? ? ? areaLabel.setFont(labelFont);
? ? ? ? areaLabel.setHorizontalAlignment(JLabel.CENTER);
? ? ? ? scorePanel.add(areaLabel);

? ? ? ? JLabel buttonLabel = new JLabel();
? ? ? ? scorePanel.add(buttonLabel);

? ? ? ? JButton resetButton = new JButton("重開");
? ? ? ? resetButton.setFont(buttonFont);
? ? ? ? resetButton.setFocusPainted(false);
? ? ? ? resetButton.setBounds(width/ 24,10,width / 4,30);
? ? ? ? resetButton.addActionListener((event)->{
? ? ? ? ? ? frame.setVisible(false);
? ? ? ? ? ? mainPanel.removeAll();
? ? ? ? ? ? resetGame();
? ? ? ? });
? ? ? ? buttonLabel.add(resetButton);

? ? ? ? bombLabel = new JLabel();
? ? ? ? bombLabel.setFont(labelFont);
? ? ? ? bombLabel.setHorizontalAlignment(JLabel.CENTER);
? ? ? ? scorePanel.add(bombLabel);

? ? ? ? frame.add(scorePanel);
? ? ? ? frame.add(mainPanel);


? ? ? ? resetGame();
? ? }

? ? public void resetGame(){
? ? ? ? setBomb();
? ? ? ? remainArea = row * column;
? ? ? ? remainBomb = bombNum;

? ? ? ? labels = new JLabel[row][column];

? ? ? ? System.gc();

? ? ? ? for(int i = 0; i < row; i ++){
? ? ? ? ? ? for(int j = 0; j < column; j ++){
? ? ? ? ? ? ? ? labels[i][j] = new JLabel();
? ? ? ? ? ? ? ? //設(shè)置元素居中
? ? ? ? ? ? ? ? labels[i][j].setHorizontalAlignment(JLabel.CENTER);
? ? ? ? ? ? ? ? if(data[i][j] == -1) {
? ? ? ? ? ? ? ? ? ? labels[i][j].setIcon(bomb);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else if(data[i][j] > 0)
? ? ? ? ? ? ? ? ? ? labels[i][j].setText("" + data[i][j]);

? ? ? ? ? ? ? ? int y = (i + 1) * labelSpace + i * labelWeight;
? ? ? ? ? ? ? ? int x = (j + 1) * labelSpace + j * labelHeigth;
? ? ? ? ? ? ? ? labels[i][j].setBounds(x,y,labelWeight,labelHeigth);

? ? ? ? ? ? ? ? JButton button = new JButton();

? ? ? ? ? ? ? ? button.addMouseListener(new ButtonListener(button,i,j,this));
? ? ? ? ? ? ? ? button.setBounds(0,0,labelWeight,labelHeigth);
? ? ? ? ? ? ? ? labels[i][j].setLayout(null);
? ? ? ? ? ? ? ? labels[i][j].add(button);
? ? ? ? ? ? ? ? labels[i][j].setLabelFor(button);
? ? ? ? ? ? ? ? mainPanel.add(labels[i][j]);
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? areaLabel.setText("" + remainArea);
? ? ? ? bombLabel.setText("" + remainBomb);

? ? ? ? frame.setVisible(true);
? ? }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring?Boot如何實(shí)現(xiàn)統(tǒng)一數(shù)據(jù)返回

    Spring?Boot如何實(shí)現(xiàn)統(tǒng)一數(shù)據(jù)返回

    這篇文章主要介紹了Spring?Boot如何實(shí)現(xiàn)統(tǒng)一數(shù)據(jù)返回,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-07-07
  • SpringBoot整合Shiro的環(huán)境搭建教程

    SpringBoot整合Shiro的環(huán)境搭建教程

    這篇文章主要為大家詳細(xì)介紹了SpringBoot整合Shiro的環(huán)境搭建教程,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以了解一下
    2022-12-12
  • 基于idea把springboot項(xiàng)目部署到docker

    基于idea把springboot項(xiàng)目部署到docker

    這篇文章主要介紹了基于idea把springboot項(xiàng)目部署到docker,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • Java使用PrepareStatement實(shí)現(xiàn)數(shù)據(jù)的插入與查詢操作

    Java使用PrepareStatement實(shí)現(xiàn)數(shù)據(jù)的插入與查詢操作

    這篇文章主要為大家詳細(xì)介紹了Java如何使用PrepareStatement實(shí)現(xiàn)數(shù)據(jù)的插入與查詢操作,文中的示例代碼講解詳細(xì),感興趣的可以了解一下
    2022-09-09
  • Springcloud GateWay網(wǎng)關(guān)配置過程圖解

    Springcloud GateWay網(wǎng)關(guān)配置過程圖解

    這篇文章主要介紹了Springcloud GateWay網(wǎng)關(guān)配置過程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Java之Pattern.compile函數(shù)用法詳解

    Java之Pattern.compile函數(shù)用法詳解

    這篇文章主要介紹了Java之Pattern.compile函數(shù)用法詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Java并發(fā)編程之原子操作類詳情

    Java并發(fā)編程之原子操作類詳情

    這篇文章主要介紹了Java并發(fā)編程之原子操作類詳情,文章基于Java并發(fā)編程展開相關(guān)內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-04-04
  • Java合并兩個(gè)及以上有序鏈表的示例詳解

    Java合并兩個(gè)及以上有序鏈表的示例詳解

    這篇文章主要通過兩個(gè)例題為大家介紹一下Java合并兩個(gè)及以上有序鏈表的實(shí)現(xiàn)方法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,需要的可以參考一下
    2022-11-11
  • Java中網(wǎng)絡(luò)IO的實(shí)現(xiàn)方式(BIO、NIO、AIO)介紹

    Java中網(wǎng)絡(luò)IO的實(shí)現(xiàn)方式(BIO、NIO、AIO)介紹

    這篇文章主要介紹了Java中網(wǎng)絡(luò)IO的實(shí)現(xiàn)方式(BIO、NIO、AIO)介紹的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Java接口冪等性設(shè)計(jì)原理解析

    Java接口冪等性設(shè)計(jì)原理解析

    這篇文章主要介紹了Java接口冪等性設(shè)計(jì)原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05

最新評(píng)論

包头市| 鄂州市| 乐陵市| 英超| 烟台市| 巴南区| 密山市| 石渠县| 县级市| 敦化市| 上林县| 宣化县| 沙湾县| 阜平县| 炎陵县| 故城县| 扶余县| 宜春市| 九江县| 衡阳市| 内黄县| 浦北县| 博湖县| 石屏县| 平乐县| 神农架林区| 静安区| 鄂尔多斯市| 泌阳县| 吉安市| 商都县| 肃北| 兴化市| 湾仔区| 元阳县| 铜陵市| 宝坻区| 清镇市| 三门县| 固镇县| 万宁市|