java實(shí)現(xiàn)簡(jiǎn)單的拼圖游戲
用Java來實(shí)現(xiàn)簡(jiǎn)單的拼圖游戲,供大家參考,具體內(nèi)容如下
首先我們看一下這個(gè)拼圖小游戲的效果圖:

創(chuàng)建一個(gè)名稱為MedleyGame的類,該類繼承了JFrame類;然后在該類中分別聲明一個(gè)面板對(duì)象和一個(gè)按鈕對(duì)象,面板對(duì)象用來添加拼圖按鈕,按鈕對(duì)象為當(dāng)前顯示空白圖片的按鈕;最后為該類編寫一個(gè)main()方法和一個(gè)構(gòu)造方法MedleyGame(),并在構(gòu)造方法中設(shè)置窗體的相關(guān)屬性,如窗體的標(biāo)題、顯示位置、大小等。
java項(xiàng)目結(jié)構(gòu)如下:

具體java代碼如下:
package pac;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
public class MedleyGame extends JFrame{
private JPanel centerPanel;// 拼圖按鈕面板
private JButton emptyButton;// 空白按鈕對(duì)象
public static void main(String[] args) {
try {
MedleyGame frame = new MedleyGame();//創(chuàng)建本類的對(duì)象
frame.setVisible(true);//設(shè)置窗體為可見
}catch(Exception e) {
e.printStackTrace();
}
}
public MedleyGame() {
super();// 繼承JFrame類的構(gòu)造方法
setResizable(false);// 設(shè)置窗體大小不可改變
setTitle("拼圖游戲");// 設(shè)置窗體的標(biāo)題
setBounds(100, 100, 354, 435);// 設(shè)置窗體的顯示位置及大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 設(shè)置關(guān)閉窗體時(shí)退出程序
final JPanel topPanel = new JPanel();// 創(chuàng)建面板對(duì)象
topPanel.setBorder(new TitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, null, null));// 為面板添加邊框
topPanel.setLayout(new BorderLayout());// 設(shè)置面板采用邊界布局
getContentPane().add(topPanel,BorderLayout.NORTH);// 將面板添加到窗體頂部
final JLabel modelLabel = new JLabel();// 創(chuàng)建顯示參考圖片的標(biāo)簽對(duì)象
modelLabel.setIcon(new ImageIcon("img/model.jpg"));// 設(shè)置標(biāo)簽顯示的參考圖片
topPanel.add(modelLabel, BorderLayout.WEST);// 將標(biāo)簽添加到面板的左側(cè)
final JButton startButton = new JButton();// 創(chuàng)建“下一局”按鈕對(duì)象
startButton.setText("下一局");// 設(shè)置按鈕的標(biāo)簽文本
startButton.addActionListener(new StartButtonAction());// 為按鈕添加監(jiān)聽器
topPanel.add(startButton, BorderLayout.CENTER);// 將按鈕添加到面板的中間
centerPanel = new JPanel();// 創(chuàng)建拼圖按鈕面板對(duì)象
centerPanel.setBorder(new TitledBorder(null, "",
TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, null, null));// 為面板添加邊框
centerPanel.setLayout(new GridLayout(0, 3));// 設(shè)置拼圖按鈕面板采用3列的網(wǎng)格布局
getContentPane().add(centerPanel, BorderLayout.CENTER);// 將面板添加到窗體的中間
String[][] stochasticOrder = reorder();// 獲得網(wǎng)格圖片的隨機(jī)擺放順序
for (int row = 0; row < 3; row++) {// 遍例行
for (int col = 0; col < 3; col++) {// 遍例列
final JButton button = new JButton();// 創(chuàng)建拼圖按鈕對(duì)象
button.setName(row + "" + col);// 設(shè)置按鈕的名稱
button.setIcon(new ImageIcon(stochasticOrder[row][col])); // 為拼圖按鈕設(shè)置圖片
if (stochasticOrder[row][col].equals("img/22.jpg")) // 判斷是否為空白按鈕
emptyButton = button;
button.addActionListener(new ImgButtonAction()); // 為拼圖按鈕添加監(jiān)聽器
centerPanel.add(button);// 將按鈕添加到拼圖按鈕面板中
}
}
//
}
private String[][] reorder(){// 用來獲取網(wǎng)格圖片的隨機(jī)擺放順序
String[][] exactnessOrder = new String[3][3];// 網(wǎng)格圖片的正確擺放順序
for (int row = 0; row < 3; row++) {// 遍例行
for (int col = 0; col < 3; col++) {// 遍例列
exactnessOrder[row][col] = "img/" + row + col + ".jpg";
}
}
String[][] stochasticOrder = new String[3][3];// 網(wǎng)格圖片的隨機(jī)擺放順序
for (int row = 0; row < 3; row++) {// 遍例行
for (int col = 0; col < 3; col++) {// 遍例列
while (stochasticOrder[row][col] == null) { // 隨機(jī)擺放順序的指定網(wǎng)格為空
int r = (int) (Math.random() * 3);// 取隨機(jī)行
int c = (int) (Math.random() * 3);// 取隨機(jī)列
if (exactnessOrder[r][c] != null) { // 正確擺放順序的指定網(wǎng)格不為空
// 將位于正確擺放順序的指定網(wǎng)格的圖片擺放到位于隨機(jī)擺放順序的指定網(wǎng)格中
stochasticOrder[row][col] = exactnessOrder[r][c];
// 將位于正確順序的指定網(wǎng)格設(shè)置為空
exactnessOrder[r][c] = null;
}
}
}
}
return stochasticOrder;
}
class ImgButtonAction implements ActionListener {// 拼圖按鈕監(jiān)聽器
public void actionPerformed(ActionEvent e) {
String emptyName = emptyButton.getName();// 獲得空白按鈕的名稱
char emptyRow = emptyName.charAt(0);// 獲得空白按鈕所在的行
char emptyCol = emptyName.charAt(1);// 獲得空白按鈕所在的列
JButton clickButton = (JButton) e.getSource();// 獲得被點(diǎn)擊按鈕對(duì)象
String clickName = clickButton.getName();// 獲得被點(diǎn)擊按鈕的名稱
char clickRow = clickName.charAt(0);// 獲得被點(diǎn)擊按鈕所在的行
char clickCol = clickName.charAt(1);// 獲得被點(diǎn)擊按鈕所在的列
// 判斷被點(diǎn)擊按鈕與空白按鈕是否相臨
if (Math.abs(clickRow - emptyRow) + Math.abs(clickCol - emptyCol) == 1) {
// 將被點(diǎn)擊按鈕的圖片移動(dòng)到空白按鈕上
emptyButton.setIcon(clickButton.getIcon());
// 設(shè)置被點(diǎn)擊的按鈕顯示空白圖片
clickButton.setIcon(new ImageIcon("img/22.jpg"));
emptyButton = clickButton;// 將被點(diǎn)擊的按鈕設(shè)置為空白按鈕
}
}
}
class StartButtonAction implements ActionListener {// 下一局按鈕監(jiān)聽器
public void actionPerformed(ActionEvent e) {
String[][] stochasticOrder = reorder();// 獲得網(wǎng)格圖片的隨機(jī)擺放順序
int i = 0;// 拼圖按鈕在拼圖按鈕面板中的索引
for (int row = 0; row < 3; row++) {// 遍例行
for (int col = 0; col < 3; col++) {// 遍例列
JButton button = (JButton) centerPanel.getComponent(i++); // 獲得位于指定索引的拼圖按鈕
button.setIcon(new ImageIcon(stochasticOrder[row][col])); // 為拼圖按鈕設(shè)置圖片
if (stochasticOrder[row][col].equals("img/22.jpg")) // 判斷是否為空白按鈕
emptyButton = button;
}
}
}
}
}
到這里代碼程序就寫完了,我們來運(yùn)行一下:

按步驟保存然后運(yùn)行一下:

結(jié)果沒問題,單擊“下一局”會(huì)重新刷新游戲,圖片會(huì)進(jìn)行隨機(jī)排布:

到此,一個(gè)簡(jiǎn)簡(jiǎn)單單的拼圖小游戲就實(shí)現(xiàn)了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
合成聚合復(fù)用原則_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了合成聚合復(fù)用原則,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
Mybatis plus實(shí)現(xiàn)Distinct去重功能
這篇文章主要介紹了Mybatis plus實(shí)現(xiàn)Distinct去重功能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
org.apache.zookeeper.KeeperException.BadVersionException異常的解
在使用Apache ZooKeeper進(jìn)行分布式協(xié)調(diào)時(shí),你可能會(huì)遇到org.apache.zookeeper.KeeperException.BadVersionException異常,本文就來介紹一下解決方法,感興趣的可以了解一下2024-03-03
Java實(shí)現(xiàn)在正則表達(dá)式中控制大小寫的方法
這篇文章主要介紹了Java實(shí)現(xiàn)在正則表達(dá)式中控制大小寫的方法,結(jié)合實(shí)例形式分析了java正則表達(dá)式中傳遞控制參數(shù)的功能與相關(guān)操作技巧,需要的朋友可以參考下2017-04-04
Java面向?qū)ο缶幊讨衒inal關(guān)鍵字的使用方法詳解
這篇文章主要介紹了Java面向?qū)ο缶幊讨衒inal關(guān)鍵字的使用方法詳解,包括對(duì)內(nèi)部匿名類無法訪問外面的非 final 的變量問題的解讀,需要的朋友可以參考下2016-06-06
SpringBoot整合MybatisPlus實(shí)現(xiàn)增刪改查功能
MybatisPlus是國(guó)產(chǎn)的第三方插件,?它封裝了許多常用的CURDapi,免去了我們寫mapper.xml的重復(fù)勞動(dòng)。本文將整合MybatisPlus實(shí)現(xiàn)增刪改查功能,感興趣的可以了解一下2022-05-05
Spring 實(shí)現(xiàn)excel及pdf導(dǎo)出表格示例
本篇文章主要介紹了Spring 實(shí)現(xiàn)excel及pdf導(dǎo)出表格示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03

