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

java實(shí)現(xiàn)拼圖小游戲

 更新時間:2021年07月23日 10:33:58   作者:AI-w  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)拼圖小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

一個簡單的拼圖小游戲,供大家參考,具體內(nèi)容如下

1.首先設(shè)計視圖面板。
2.添加所需要的圖片按鈕。
3.最主要的是設(shè)計監(jiān)聽事件,添加圖片的監(jiān)聽按鈕,設(shè)定移動空白圖片周圍的按鈕。
4.判斷是否成功 。

package sxy;
import java.awt.Choice;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.util.Random;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class PintuGame {
  public static void main(String args[]) {
    new PintuFrame().StartFrame();
  }
}
class PintuFrame extends JFrame {
  private static final long serialVersionUID = 1L;
  // 等級設(shè)置
  private static int level = 3;
  // 圖片索引
  private static int index = 0;
  // 圖片數(shù)量
  private static int picCount = 2;
  // 開始時間
  private long startTime;
// 初始化小方塊
  private JButton[] buttons;
  // 初始化空方塊
  private JPanel emptyPanel = new JPanel();
  // 初始化監(jiān)聽類
  private PintuListener listener = new PintuListener();
  // 初始化Panel
  private JPanel panel = new JPanel(null);
  // 圖片預(yù)覽
  private JLabel label;
  private String[] imgpath = new String[picCount];
  // 選圖時的圖片路徑
  String path;
  public PintuFrame() {
    for (int i = 0; i < picCount; i++) {
      imgpath[i] = i + ".jpg";
      System.out.println(imgpath[i]);
    }
    path = imgpath[index];
  }
  /**
   * 開始窗體加載
   */```
public void StartFrame() {
    panel.removeAll();
    JButton start = new JButton("開始");// 開始按鈕
    JButton left = new JButton("<");
    JButton right = new JButton(">");
    JLabel selLevel = new JLabel("LV:");
    label = new JLabel(getIcon());// 根據(jù)圖標(biāo)設(shè)置標(biāo)簽
    final Choice choice = new Choice();// 創(chuàng)建選擇器
    choice.add("--初級--");// 添加列表項
    choice.add("--中級--");
    choice.add("--高級--");
    selLevel.setBounds(5, 0, 20, 20);// 設(shè)置坐標(biāo)
    choice.setBounds(28, 0, 65, 20);
    start.setBounds(93, 0, 85, 20);
    left.setBounds(178, 0, 61, 20);
    right.setBounds(239, 0, 61, 20);
    label.setBounds(0, 22, 300, 300);// 設(shè)置標(biāo)簽的方位
    panel.add(selLevel);
    panel.add(choice);
    panel.add(start);
    panel.add(left);
    panel.add(right);
    panel.add(label);
    panel.repaint();
    add(panel);
    setTitle("拼圖游戲");
    setBounds(450, 130, 300, 322);
    setResizable(false);
    // 添加關(guān)閉按鈕
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    // 監(jiān)聽等級選擇
    start.addMouseListener(new MouseAdapter() {
      @Override
      public void mousePressed(MouseEvent e) {
        level = choice.getSelectedIndex() + 3;
        launchFrame();
      }
    });
    // 監(jiān)聽選圖按鈕 <-
    left.addMouseListener(new MouseAdapter() {
      @Override
      public void mousePressed(MouseEvent e) {
        if (index == 0) {
          index = picCount - 1;
          path = imgpath[index];
        } else {
          path = imgpath[--index];
        }
        panel.remove(label);
        label = new JLabel(getIcon());
        label.setBounds(0, 22, 300, 300);
        panel.add(label);
        panel.repaint();
      }
    });
    // 監(jiān)聽選圖按鈕 ->
    right.addMouseListener(new MouseAdapter() {
      @Override
      public void mousePressed(MouseEvent e) {
        if (index == picCount - 1) {
          index = 0;
          path = imgpath[index];
        } else {
          path = imgpath[++index];
        }
        panel.remove(label);
        label = new JLabel(getIcon());
        label.setBounds(0, 22, 300, 300);
        panel.add(label);
        panel.repaint();
      }
    });
  }
  /**
   * 拼圖窗體加載
   */
  public void launchFrame() {
    startTime = System.currentTimeMillis();
    panel.removeAll();
    buttons = new JButton[level * level];
    // 設(shè)置圖標(biāo)組
    Icon[] icon = new PintuFrame().creatIcon(path);
    // 小方塊索引
    int index = 0;
    // 小方塊坐標(biāo)
    int x = 0, y = 0;
    // 設(shè)置小方塊位置,圖標(biāo),監(jiān)聽
    for (int i = 0; i < level; i++) {
      for (int j = 0; j < level; j++) {
        // 添加圖標(biāo)
        buttons[index] = new JButton(icon[index]);
        // 添加監(jiān)聽
        buttons[index].addMouseListener(listener);
        // 設(shè)置位置
        buttons[index].setBounds(x, y, 100, 100);
        // 添加到panel
        panel.add(buttons[index++]);
        x += 100;
      }
      y += 100;
      x = 0;
    }
    // 移除最后一個小方塊
    panel.remove(buttons[(level * level) - 1]);
    // 設(shè)置空方塊位置
    emptyPanel.setBounds((level - 1) * 100, (level - 1) * 100, 100, 100);
    // 添加空方塊
    panel.add(emptyPanel);
    panel.repaint();
    add(panel);
    setResizable(false);
    setTitle("拼圖游戲");
    // 設(shè)置大小
    setBounds(450, 130, level * 100, level * 100 + 30);
    // 打亂方格順序
    breakRank();
    // 添加關(guān)閉按鈕
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  // 選圖界面圖像
  public Icon getIcon() {
    ImageIcon bi = new ImageIcon(getClass().getClassLoader().getResource(path));
    // 縮放大小并顯示到窗體
    Image image = bi.getImage().getScaledInstance(300, 300, Image.SCALE_REPLICATE);
    return new ImageIcon(image);
  }
  // 打亂方格
  public void breakRank() {
    Random r = new Random();
    int x = 0, y = 0, emptyDir_X = 0, emptyDir_Y = 0;
    // 模擬隨即點(diǎn)擊1000次,打亂方格
    for (int i = 0; i < 1000; i++) {
      int rid = r.nextInt(level * level - 1);
      // 獲得該方格按鈕的橫坐標(biāo)
      x = buttons[rid].getBounds().x;
      // 獲得該方格按鈕的縱坐標(biāo)
      y = buttons[rid].getBounds().y;
      // 得到空方格的橫坐標(biāo)
      emptyDir_X = emptyPanel.getBounds().x;
      // 得到空方格的縱坐標(biāo)
      emptyDir_Y = emptyPanel.getBounds().y;
      move(x, y, emptyDir_X, emptyDir_Y, buttons[rid]);
    }
  }
  // 移動方格
  public void move(int x, int y, int emptyDir_X, int emptyDir_Y, JButton button) {
    // 進(jìn)行比較果滿足條件則交換
    if (x == emptyDir_X && y - emptyDir_Y == 100) {
      button.setLocation(button.getBounds().x, button.getBounds().y - 100);
    } else if (x == emptyDir_X && y - emptyDir_Y == -100) {
      button.setLocation(button.getBounds().x, button.getBounds().y + 100);
    } else if (x - emptyDir_X == 100 & y == emptyDir_Y) {
      button.setLocation(button.getBounds().x - 100, button.getBounds().y);
    } else if (x - emptyDir_X == -100 && y == emptyDir_Y) {
      button.setLocation(button.getBounds().x + 100, button.getBounds().y);
    } else
      return;
      // 重新設(shè)置空方格的位置
    emptyPanel.setLocation(x, y);
  }
  // 判斷是否拼湊成功
  public boolean isFinish() {
    for (int i = 0; i < (level * level) - 1; i++) {
      int x = buttons[i].getBounds().x;
      int y = buttons[i].getBounds().y;
      // 根據(jù)坐標(biāo)位置判斷是否拼湊成功 0+0 0+1 ..
      if (y / 100 * level + x / 100 != i)
        return false;
    }
    return true;
  }
  // 事件監(jiān)聽類
  public class PintuListener extends MouseAdapter {
    @Override
    public void mousePressed(MouseEvent e) {
      JButton button = (JButton) e.getSource();// 獲得鼠標(biāo)按的方格按鈕
      int x = button.getBounds().x;// 獲得該方格按鈕的橫坐標(biāo)
      int y = button.getBounds().y;// 獲得該方格按鈕的縱坐標(biāo)
      int nullDir_X = emptyPanel.getBounds().x;// 得到空方格的橫坐標(biāo)
      int nullDir_Y = emptyPanel.getBounds().y;// 得到空方格的縱坐標(biāo)
      move(x, y, nullDir_X, nullDir_Y, button);
      if (isFinish()) {// 進(jìn)行是否完成的判斷
        panel.remove(emptyPanel);// 移除最后一個小方塊
        panel.add(buttons[(level * level) - 1]);// 移除最后一個小方塊
        JOptionPane.showMessageDialog(null,
            "恭喜你,完成拼圖\r\n用時為:" + (System.currentTimeMillis() - startTime) / 1000 + "S");
        for (int i = 0; i < picCount; i++) {// 循環(huán)撤消鼠標(biāo)事件
          buttons[i].removeMouseListener(listener);
        }
        StartFrame();
      }
      repaint();
    }
  }
  // 創(chuàng)建方格圖標(biāo)組
  public Icon[] creatIcon(String srcImageFile) {
    ImageIcon bi = new ImageIcon(this.getClass().getClassLoader().getResource(srcImageFile));
    // 讀取源圖像
    Image image = bi.getImage();
    int index = 0;
    int x = 0, y = 0;
    Icon[] icon = new Icon[level * level];// 根據(jù)窗體大小創(chuàng)建圖標(biāo)數(shù)量
    for (int i = 0; i < level; i++) {
      for (int j = 0; j < level; j++) {
        // 從原圖像上獲取一個方形位置
        ImageFilter cropFilter = new CropImageFilter(x, y, 100, 100);
        // 截取方形圖像
        Image img = Toolkit.getDefaultToolkit()
            .createImage(new FilteredImageSource(image.getSource(), cropFilter));
        icon[index++] = new ImageIcon(img);
        x += 100;
      }
      y += 100;
      x = 0;
    }
    return icon;
  }
}

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

相關(guān)文章

  • Java實(shí)現(xiàn)讀取html文本內(nèi)容并按照格式導(dǎo)出到excel中

    Java實(shí)現(xiàn)讀取html文本內(nèi)容并按照格式導(dǎo)出到excel中

    這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)讀取html文本提取相應(yīng)內(nèi)容按照格式導(dǎo)出到excel中,文中的示例代碼講解詳細(xì),需要的可以參考下
    2024-02-02
  • 詳解Spring中bean生命周期回調(diào)方法

    詳解Spring中bean生命周期回調(diào)方法

    本篇文章主要介紹了詳解Spring中bean生命周期回調(diào)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • Java中List排序的3種常見方法總結(jié)

    Java中List排序的3種常見方法總結(jié)

    在Java編程中List對象的排序是一個常見的需求,List接口提供了多種排序方法,這篇文章主要給大家介紹了關(guān)于Java中List排序的3種常見方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-08-08
  • 使用mybatis的typeHandler對clob進(jìn)行流讀寫方式

    使用mybatis的typeHandler對clob進(jìn)行流讀寫方式

    這篇文章主要介紹了使用mybatis的typeHandler對clob進(jìn)行流讀寫方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 關(guān)于Tomcat出現(xiàn)The origin server did not find a current representation for the target resourc...的問題

    關(guān)于Tomcat出現(xiàn)The origin server did not find a current represent

    這篇文章主要介紹了關(guān)于Tomcat出現(xiàn)The origin server did not find a current representation for the target resourc...的問題,感興趣的小伙伴們可以參考一下
    2020-08-08
  • java并發(fā)編程專題(二)----如何創(chuàng)建并運(yùn)行java線程

    java并發(fā)編程專題(二)----如何創(chuàng)建并運(yùn)行java線程

    這篇文章主要介紹了java并發(fā)編程如何創(chuàng)建并運(yùn)行java線程,文中講解非常詳細(xì),示例代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • ElasticSearch如何設(shè)置某個字段不分詞淺析

    ElasticSearch如何設(shè)置某個字段不分詞淺析

    最近在學(xué)習(xí)ElasticSearch官方文檔過程中發(fā)現(xiàn)的某個問題,記錄一下 希望能幫助到后面的朋友,下面這篇文章主要給大家介紹了關(guān)于ElasticSearch如何設(shè)置某個字段不分詞的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • spring boot容器啟動流程

    spring boot容器啟動流程

    spring cloud是基于spring boot快速搭建的,今天咱們就看看spring boot容器啟動流程,需要的朋友跟隨腳本之家小編一起學(xué)習(xí)吧
    2018-01-01
  • Spring?Bean的8種加載方式總結(jié)

    Spring?Bean的8種加載方式總結(jié)

    以前學(xué)習(xí)Spring框架的時候,總結(jié)了幾種Bean的加載方式,不過老師說還有其它的加載方式,以下八種并不是全部,但也足以用來做很多事情了,希望對大家有所幫助
    2022-10-10
  • Spring學(xué)習(xí)筆記之RedisTemplate的配置與使用教程

    Spring學(xué)習(xí)筆記之RedisTemplate的配置與使用教程

    這篇文章主要給大家介紹了關(guān)于Spring學(xué)習(xí)筆記之RedisTemplate配置與使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用spring具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06

最新評論

高密市| 大兴区| 盐源县| 三穗县| 湛江市| 通州区| 岳阳市| 福鼎市| 射阳县| 合水县| 望都县| 嵊州市| 南澳县| 淮北市| 边坝县| 潞西市| 霸州市| 平乡县| 克什克腾旗| 盘山县| 长治市| 临西县| 新和县| 梨树县| 湾仔区| 镇宁| 太和县| 灵宝市| 泽普县| 洪江市| 美姑县| 沭阳县| 修文县| 苏尼特左旗| 大庆市| 海林市| 枣阳市| 平邑县| 南和县| 溧阳市| 贵州省|