- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- 如何使用java實(shí)現拼圖小游戲
這篇文章主要介紹了如何使用java實(shí)現拼圖小游戲,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著(zhù)大家一起了解一下。
具體內容如下
1.首先設計視圖面板。2.添加所需要的圖片按鈕。3.最主要的是設計監聽(tīng)事件,添加圖片的監聽(tīng)按鈕,設定移動(dòng)空白圖片周?chē)陌粹o。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; // 等級設置 private static int level = 3; // 圖片索引 private static int index = 0; // 圖片數量 private static int picCount = 2; // 開(kāi)始時(shí)間 private long startTime;// 初始化小方塊 private JButton[] buttons; // 初始化空方塊 private JPanel emptyPanel = new JPanel(); // 初始化監聽(tīng)類(lèi) private PintuListener listener = new PintuListener(); // 初始化Panel private JPanel panel = new JPanel(null); // 圖片預覽 private JLabel label; private String[] imgpath = new String[picCount]; // 選圖時(shí)的圖片路徑 String path; public PintuFrame() { for (int i = 0; i < picCount; i++) { imgpath[i] = i + ".jpg"; System.out.println(imgpath[i]); } path = imgpath[index]; } /** * 開(kāi)始窗體加載 */```public void StartFrame() { panel.removeAll(); JButton start = new JButton("開(kāi)始");// 開(kāi)始按鈕 JButton left = new JButton("<"); JButton right = new JButton(">"); JLabel selLevel = new JLabel("LV:"); label = new JLabel(getIcon());// 根據圖標設置標簽 final Choice choice = new Choice();// 創(chuàng )建選擇器 choice.add("--初級--");// 添加列表項 choice.add("--中級--"); choice.add("--高級--"); selLevel.setBounds(5, 0, 20, 20);// 設置坐標 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);// 設置標簽的方位 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); // 監聽(tīng)等級選擇 start.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { level = choice.getSelectedIndex() + 3; launchFrame(); } }); // 監聽(tīng)選圖按鈕 <- 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(); } }); // 監聽(tīng)選圖按鈕 -> 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]; // 設置圖標組 Icon[] icon = new PintuFrame().creatIcon(path); // 小方塊索引 int index = 0; // 小方塊坐標 int x = 0, y = 0; // 設置小方塊位置,圖標,監聽(tīng) for (int i = 0; i < level; i++) { for (int j = 0; j < level; j++) { // 添加圖標 buttons[index] = new JButton(icon[index]); // 添加監聽(tīng) buttons[index].addMouseListener(listener); // 設置位置 buttons[index].setBounds(x, y, 100, 100); // 添加到panel panel.add(buttons[index++]); x += 100; } y += 100; x = 0; } // 移除最后一個(gè)小方塊 panel.remove(buttons[(level * level) - 1]); // 設置空方塊位置 emptyPanel.setBounds((level - 1) * 100, (level - 1) * 100, 100, 100); // 添加空方塊 panel.add(emptyPanel); panel.repaint(); add(panel); setResizable(false); setTitle("拼圖游戲"); // 設置大小 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); // 獲得該方格按鈕的橫坐標 x = buttons[rid].getBounds().x; // 獲得該方格按鈕的縱坐標 y = buttons[rid].getBounds().y; // 得到空方格的橫坐標 emptyDir_X = emptyPanel.getBounds().x; // 得到空方格的縱坐標 emptyDir_Y = emptyPanel.getBounds().y; move(x, y, emptyDir_X, emptyDir_Y, buttons[rid]); } } // 移動(dòng)方格 public void move(int x, int y, int emptyDir_X, int emptyDir_Y, JButton button) { // 進(jìn)行比較果滿(mǎ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; // 重新設置空方格的位置 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; // 根據坐標位置判斷是否拼湊成功 0+0 0+1 .. if (y / 100 * level + x / 100 != i) return false; } return true; } // 事件監聽(tīng)類(lèi) public class PintuListener extends MouseAdapter { @Override public void mousePressed(MouseEvent e) { JButton button = (JButton) e.getSource();// 獲得鼠標按的方格按鈕 int x = button.getBounds().x;// 獲得該方格按鈕的橫坐標 int y = button.getBounds().y;// 獲得該方格按鈕的縱坐標 int nullDir_X = emptyPanel.getBounds().x;// 得到空方格的橫坐標 int nullDir_Y = emptyPanel.getBounds().y;// 得到空方格的縱坐標 move(x, y, nullDir_X, nullDir_Y, button); if (isFinish()) {// 進(jìn)行是否完成的判斷 panel.remove(emptyPanel);// 移除最后一個(gè)小方塊 panel.add(buttons[(level * level) - 1]);// 移除最后一個(gè)小方塊 JOptionPane.showMessageDialog(null, "恭喜你,完成拼圖\r\n用時(shí)為:" + (System.currentTimeMillis() - startTime) / 1000 + "S"); for (int i = 0; i < picCount; i++) {// 循環(huán)撤消鼠標事件 buttons[i].removeMouseListener(listener); } StartFrame(); } repaint(); } } // 創(chuàng )建方格圖標組 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];// 根據窗體大小創(chuàng )建圖標數量 for (int i = 0; i < level; i++) { for (int j = 0; j < level; j++) { // 從原圖像上獲取一個(gè)方形位置 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; }}
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站