Java版拼圖遊戲(基於swing編程提升版)

此拼圖遊戲是九宮格,圖片是提前分割好的,未涉及自動分割圖片算法。廢話不說,代碼如下:

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 stub
		PaintGame 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 stub
			strings = 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 stub
			timer.restart();
			sum = 30;
			labelBu.setText("剩餘"+sum+"步");
			timeCount = 120000;
			labelTime.setText("倒計時:"+timeCount );
			blackrow = 0;
			blackcol = 0;
			if(num == 5)
				num = 0;
			else
				num++;
			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 stub
			this.i = i;
			this.j = j;	
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			
			if(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);	
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		timeCount -= 1000;
		labelTime.setText("倒計時:"+timeCount );
	}

}

ps:如果需要圖片資源的可以在底下留言!真的需要我就上傳到雲盤上,麼麼噠,與君共勉!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章