欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實(shí)現(xiàn)簡(jiǎn)易拼圖游戲的方法詳解

 更新時(shí)間:2022年05月12日 09:21:41   作者:錯(cuò)過(guò)了時(shí)間  
這篇文章主要介紹了如何利用Java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易拼圖游戲,幫助大家更好的理解和使用Java開(kāi)發(fā)游戲,感興趣的朋友可以跟隨小編一起學(xué)習(xí)一下

效果展示

介紹:游戲共有五張圖片可以選擇,分成了4 X 4 十六個(gè)方格,點(diǎn)擊開(kāi)始就可以開(kāi)始游戲。游戲運(yùn)行的截圖如下

游戲結(jié)構(gòu)

實(shí)現(xiàn)代碼

代碼如下:MedleyGame.java類

package game.medleyPicture;
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 JLabel modelLabel;
	private JPanel centerPanel;
	private JButton emptyButton;
	int num = 0;
	public static void main(String[] args) {
		try {
			MedleyGame frame = new MedleyGame();
			frame.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//建立窗口構(gòu)造方法
	public MedleyGame() {
		super();
		setResizable(false);
		setTitle("拼圖游戲");
		setBounds(100, 100, 370, 525);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//創(chuàng)建面板對(duì)象,并增加邊框、布局
		final JPanel topPanel = new JPanel();
		topPanel.setBorder(new TitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION,
				TitledBorder.DEFAULT_POSITION, null, null));
		topPanel.setLayout(new BorderLayout());
		getContentPane().add(topPanel, BorderLayout.NORTH);//放于上方
		//創(chuàng)建標(biāo)簽放原圖
		modelLabel = new JLabel();
		modelLabel.setIcon(new ImageIcon("image/"+ num+ "model.jpg"));
		topPanel.add(modelLabel, BorderLayout.WEST);
		//在右側(cè)加個(gè)面板,添加兩個(gè)按鈕
		JPanel eastPanel = new JPanel();
		topPanel.add(eastPanel,BorderLayout.CENTER);
		eastPanel.setLayout(new BorderLayout());
		JButton nextButton = new JButton();
		nextButton.setText("下一張");
		nextButton.addActionListener(new NextButtonAction());
		eastPanel.add(nextButton,BorderLayout.NORTH);
		//創(chuàng)建按鈕開(kāi)局添加監(jiān)聽(tīng)
		final JButton startButton = new JButton();
		startButton.setText("開(kāi)局");
		startButton.addActionListener(new StartButtonAction());
		eastPanel.add(startButton, BorderLayout.CENTER);
		//初始化中心面板,設(shè)置邊框,添加按鈕
		centerPanel = new JPanel();
		centerPanel.setBorder(new TitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION,
				TitledBorder.DEFAULT_POSITION, null, null));
		centerPanel.setLayout(new GridLayout(4, 0));
		getContentPane().add(centerPanel, BorderLayout.CENTER);
		//初始化圖片
		String[][] exactnessOrder = order();
		//按排列添加按鈕,設(shè)置圖片
		for (int row=0; row<4; row++) {
			for (int col=0; col<4; col++) {
				final JButton button = new JButton();
				button.setName(row+""+col);
				button.setIcon(new ImageIcon(exactnessOrder[row][col]));
				if (exactnessOrder[row][col].equals("image/"+ num+"00.jpg"))
					emptyButton = button;
				button.addActionListener(new ImgButtonAction());
				centerPanel.add(button);
			}
		}
	}
	//初始化圖片
	private String[][] order() {
		String[][] exactnessOrder = new String[4][4];
		for (int row=0; row<4; row++) {
			for (int col=0; col<4; col++) {
				exactnessOrder[row][col] = "image/"+ num+ row+ col+ ".jpg"; 
			}
		}
		return exactnessOrder;
	}
	//隨機(jī)排列圖片
	private String[][] reorder() {
		String[][] exactnessOrder = new String[4][4];
		for (int row=0; row<4; row++) {
			for (int col=0; col<4; col++) {
				exactnessOrder[row][col] = "image/"+ num+ row+ col+ ".jpg"; 
			}
		}
		String[][] stochasticOrder = new String[4][4];
		for (int row=0; row<4; row++) {
			for (int col=0; col<4; col++) {
				while (stochasticOrder[row][col]==null) {
					int r = (int) (Math.random()*4);
					int c = (int) (Math.random()*4);
					if (exactnessOrder[r][c] != null) {
						stochasticOrder[row][col] = exactnessOrder[r][c];
						exactnessOrder[r][c] = null;
					}
				}
			}
		}
		return stochasticOrder;
	}
	//游戲時(shí)排列圖片
	class ImgButtonAction implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String emptyName= emptyButton.getName();
			char emptyRow = emptyName.charAt(0);
			char emptyCol = emptyName.charAt(1);
			JButton clickButton = (JButton) e.getSource();
			String clickName = clickButton.getName();
			char clickRow = clickName.charAt(0);
			char clickCol = clickName.charAt(1);
			if (Math.abs(clickRow - emptyRow) + Math.abs(clickCol - emptyCol) == 1) {
				emptyButton.setIcon(clickButton.getIcon());
				clickButton.setIcon(new ImageIcon("image/"+ num+ "00.jpg"));
				emptyButton = clickButton;
			}
		}
	}
	//換下一張圖片
	class NextButtonAction implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (num==5) {
				num=0;
			} else {
				++num;
			}
			modelLabel.setIcon(new ImageIcon("image/"+num+"model.jpg"));
			String[][] exactnessOrder = order();
			int i= 0;
			for (int row=0; row<4; row++) {
				for (int col=0; col<4; col++) {
					JButton button = (JButton) centerPanel.getComponent(i++);
					button.setIcon(new ImageIcon(exactnessOrder[row][col]));
					if(exactnessOrder[row][col].equals("image/"+ num+ "00.jpg"))
						emptyButton=button;
				}
			}
		}
	}
	//開(kāi)局排列圖片
	class StartButtonAction implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String[][] stochasticOrder = reorder();
			int i= 0;
			for (int row=0; row<4; row++) {
				for (int col=0; col<4; col++) {
					JButton button = (JButton) centerPanel.getComponent(i++);
					button.setIcon(new ImageIcon(stochasticOrder[row][col]));
					if(stochasticOrder[row][col].equals("image/"+ num+ "00.jpg"))
						emptyButton=button;
				}
			}
		}
	}
}

代碼鏈接地址

以上就是Java實(shí)現(xiàn)簡(jiǎn)易拼圖游戲的方法詳解的詳細(xì)內(nèi)容,更多關(guān)于Java拼圖游戲的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • jdk在centos中安裝配置圖文教程

    jdk在centos中安裝配置圖文教程

    這篇文章主要介紹了jdk在centos中安裝配置圖文教程,文中給出大家jdk下載地址,需要的朋友可以參考下
    2018-04-04
  • java反射使用示例分享

    java反射使用示例分享

    這篇文章主要介紹了java反射使用示例,代碼很簡(jiǎn)單,需要的朋友可以參考下
    2014-02-02
  • java實(shí)現(xiàn)飛機(jī)大戰(zhàn)案例詳解

    java實(shí)現(xiàn)飛機(jī)大戰(zhàn)案例詳解

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)飛機(jī)大戰(zhàn)案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • 解決spring集成redisson踩過(guò)的坑

    解決spring集成redisson踩過(guò)的坑

    這篇文章主要介紹了spring集成redisson踩過(guò)的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 微服務(wù)間調(diào)用Retrofit在Spring?Cloud?Alibaba中的使用

    微服務(wù)間調(diào)用Retrofit在Spring?Cloud?Alibaba中的使用

    這篇文章主要為大家介紹了微服務(wù)間調(diào)用Retrofit在Spring?Cloud?Alibaba中的使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • 深入理解Java責(zé)任鏈模式實(shí)現(xiàn)靈活的請(qǐng)求處理流程

    深入理解Java責(zé)任鏈模式實(shí)現(xiàn)靈活的請(qǐng)求處理流程

    本文詳細(xì)介紹了Java中的責(zé)任鏈模式,幫助您理解其工作原理,以及如何在代碼中實(shí)現(xiàn)。該模式可以將請(qǐng)求沿著處理鏈路傳遞,實(shí)現(xiàn)靈活的請(qǐng)求處理流程。通過(guò)本文的學(xué)習(xí),您將獲得在Java應(yīng)用程序中使用責(zé)任鏈模式的知識(shí)和技能
    2023-04-04
  • SpringBoot登錄用戶權(quán)限攔截器

    SpringBoot登錄用戶權(quán)限攔截器

    這篇文章主要介紹了SpringBoot登錄用戶權(quán)限攔截器,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • IDEA使用入門(mén)小白操作教程

    IDEA使用入門(mén)小白操作教程

    IntelliJ IDEA,就是Java語(yǔ)言開(kāi)發(fā)工具 (IDE) 功能與Eclipse同類!本文通過(guò)圖文并茂的形式給大家介紹IntelliJ IDEA使用入門(mén)教程,非常適合新手小白操作,感興趣的朋友一起看看吧
    2020-10-10
  • Java中打亂一個(gè)數(shù)組的2種公平算法分享

    Java中打亂一個(gè)數(shù)組的2種公平算法分享

    這篇文章主要介紹了Java中打亂一個(gè)數(shù)組的2種公平算法分享,本文講解了洗牌程序原理、生成隨機(jī)索引交換二種方法并給出示例代碼,需要的朋友可以參考下
    2015-03-03
  • 使用SpringCache加Redis做緩存

    使用SpringCache加Redis做緩存

    這篇文章主要介紹了使用SpringCache加Redis做緩存方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12

最新評(píng)論