Java版拼图游戏(基于swing编程提升版)

来源:互联网 发布:android4高级编程 pdf 编辑:程序博客网 时间:2024/06/15 19:33

此拼图游戏是九宫格,图片是提前分割好的,未涉及自动分割图片算法。废话不说,代码如下:

package com.ctgu;import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class PaintGame extends JFrame implements ActionListener{private JLabel jLabel;private JButton jButton1,jButton2;JPanel topPanel,dommPanel,centerPanel;JButton[][] jButtons;JButton[][] checkButtons;JLabel labelTime,labelBu;int num,sum,timeCount;boolean boo;private int blackrow;private int blackcol;private JButton blackButton;String[][] strings = new String[4][4];static Timer timer;public static void main(String[] args) {// TODO Auto-generated method stubPaintGame pGame = new PaintGame();timer = new Timer(1000,pGame);timer.start();/*Thread thread = new Thread(pGame);thread.start();*/pGame.setVisible(true);}public PaintGame(){super();this.setBounds(200, 200, 360, 525);this.setTitle("拼图游戏");this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setResizable(false);initGame();}public void initGame(){num = 0;sum = 30;timeCount = 120000;jLabel = new JLabel();jButton1 = new JButton();jButton2 = new JButton();jButtons = new JButton[4][4];checkButtons = new JButton[4][4];labelTime = new JLabel();labelBu = new JLabel();blackrow = 0;blackcol = 0;blackButton = new JButton();jLabel.setIcon(new ImageIcon("image/"+num+"model.jpg"));jButton1.setText("换一张");jButton2.setText("开局");labelBu.setText("剩余"+sum+"步");labelTime.setText("倒计时:"+timeCount );jButton1.addActionListener(new ToNextPicture());jButton2.addActionListener(new DistructPicture());topPanel = new JPanel(new BorderLayout());topPanel.add(jLabel,BorderLayout.WEST);JPanel rightPanel = new JPanel(new BorderLayout());topPanel.add(rightPanel,BorderLayout.CENTER);rightPanel.add(jButton1,BorderLayout.NORTH);rightPanel.add(jButton2,BorderLayout.CENTER);centerPanel = new JPanel(new GridLayout(0,2));centerPanel.add(labelTime);centerPanel.add(labelBu);rightPanel.add(centerPanel,BorderLayout.SOUTH);dommPanel = new JPanel(new GridLayout(4,0));for(int i = 0;i < 4;i++){for(int j = 0;j < 4;j++){jButtons[i][j] = new JButton();jButtons[i][j].setIcon(new ImageIcon("image/"+num+i+j+".jpg"));checkButtons[i][j] = new JButton();checkButtons[i][j].setIcon(new ImageIcon("image/"+num+i+j+".jpg"));strings[i][j] = "image/"+num+i+j+".jpg";dommPanel.add(jButtons[i][j]);}}blackButton.setIcon(new ImageIcon("image/"+num+0+0+".jpg"));for(int i = 0;i < 4;i++){for(int j = 0;j < 4;j++){jButtons[i][j].addActionListener(new ClickMove(i,j));}}this.getContentPane().add(topPanel,BorderLayout.NORTH);this.getContentPane().add(dommPanel,BorderLayout.CENTER);}public String[][] rePaint(){String[][] s = new String[4][4];boolean[][] b = new boolean[4][4];int row = (int) (Math.random() * 4);int col = (int) (Math.random() * 4); for(int i = 0;i < 4;i++){for(int j = 0;j < 4;j++){while(b[row][col]){row = (int) (Math.random() * 4);col = (int) (Math.random() * 4);}s[i][j] = "image/"+num + row + col+".jpg";if(row == 0 && col == 0){blackrow = i;blackcol = j;blackButton.setIcon(new ImageIcon(s[i][j]));//System.out.println(s[i][j]+blackrow + blackcol );}b[row][col] = true;}}return s;}class DistructPicture implements ActionListener{public void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubstrings = rePaint(); for(int i = 0;i < 4;i++){for(int j = 0;j < 4;j++){jButtons[i][j].setIcon(new ImageIcon(strings[i][j]));dommPanel.add(jButtons[i][j]);}}}}class ToNextPicture implements ActionListener{public void actionPerformed(ActionEvent arg0) {// TODO Auto-generated method stubtimer.restart();sum = 30;labelBu.setText("剩余"+sum+"步");timeCount = 120000;labelTime.setText("倒计时:"+timeCount );blackrow = 0;blackcol = 0;if(num == 5)num = 0;elsenum++;blackButton.setIcon(new ImageIcon("image/"+num+0+0+".jpg"));jLabel.setIcon(new ImageIcon("image/"+num+"model.jpg"));for(int i = 0;i < 4;i++){for(int j = 0;j < 4;j++){strings[i][j] = "image/"+num+i+j+".jpg";jButtons[i][j].setIcon(new ImageIcon("image/"+num+i+j+".jpg"));checkButtons[i][j] = new JButton();checkButtons[i][j].setIcon(new ImageIcon("image/"+num+i+j+".jpg"));dommPanel.add(jButtons[i][j]);}}}}class ClickMove implements ActionListener{private int i;private int j;public ClickMove(int i,int j) {// TODO Auto-generated constructor stubthis.i = i;this.j = j;}@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif(i == blackrow+1&& j==blackcol || i==blackrow&&j ==blackcol+1 ||i==blackrow&&j==blackcol-1 || i==blackrow-1 &&j==blackcol){sum--;labelBu.setText("剩余"+sum+"步");if(sum == 0){if(JOptionPane.showConfirmDialog(null,"您的步数已满!结束游戏?" ,"提示",JOptionPane.WARNING_MESSAGE ) == JOptionPane.YES_OPTION){System.exit(0);}else {sum = 30;labelBu.setText("剩余"+sum+"步");strings = rePaint(); for(int m = 0;m < 4;m++){for(int n = 0;n < 4;n++){jButtons[m][n].setIcon(new ImageIcon(strings[m][n]));dommPanel.add(jButtons[m][n]);}}}}System.out.println(sum);jButtons[i][j].setIcon(blackButton.getIcon());System.out.println("=------"+"image/"+num+blackrow+blackcol+".jpg");jButtons[blackrow][blackcol].setIcon(new ImageIcon(strings[i][j]));System.out.println("image/"+num+i+j+".jpg");String temString = strings[i][j];strings[i][j] = blackButton.getIcon().toString();strings[blackrow][blackcol] = temString ;blackrow = i;blackcol = j;//System.out.println(blackrow + "" + blackcol);}boo = true;int m,n = 0;for(m = 0;m < 4;m++)for(n = 0;n < 4;n++){System.out.println(jButtons[m][n].getIcon() + "和" + checkButtons[m][n].getIcon());if(!jButtons[m][n].getIcon().toString().equals(checkButtons[m][n].getIcon().toString())){boo = false;}}if(boo == true){timer.stop();JOptionPane.showMessageDialog(null,"您已完成通过,没有闯关了,请退出吧!" ,"提示",JOptionPane.CLOSED_OPTION);System.exit(0);}}}@Overridepublic void actionPerformed(ActionEvent arg0) {// TODO Auto-generated method stubtimeCount -= 1000;labelTime.setText("倒计时:"+timeCount );}}

ps:如果需要图片资源的可以在底下留言!真的需要我就上传到云盘上,么么哒,与君共勉!


原创粉丝点击