简易的Java拼图游戏

简易的Java拼图游戏,将图片用PS切成4*4,16份,命名为00.gif,01,gif,02.gif.......以此类推。(去掉00.gif,改为一张空白图片)。16个按钮上显示图片,点击按钮后,若点击的按钮与空白按钮相邻,则将按钮上的图片进行交换,从而实现拼图的效果。

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.xml.bind.JAXBElement;

@SuppressWarnings("serial")
public class MedleyGame extends JFrame {
	private JPanel centralPanel;//拼图按钮面板
	private JButton emptyButton;//空白按钮
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MedleyGame frame = new MedleyGame();
		frame.setVisible(true);
	}
	public MedleyGame(){
		super();
		setResizable(false);//窗体大小不可改变
		setTitle("拼图游戏");
		setBounds(100,100,570,700);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗体时退出程序
		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);
		final JLabel modelLable =new JLabel();//显示参考图片的标签
		modelLable.setIcon(new ImageIcon("images/model.jpg"));
		topPanel.add(modelLable, BorderLayout.WEST);
		final JButton startButton = new JButton("下一局");
		startButton.addActionListener(new StartButtonAction());
		topPanel.add(startButton, BorderLayout.CENTER);
		//创建拼图的面板
		centralPanel = new JPanel();
		centralPanel.setLayout(new GridLayout(0,4));
		getContentPane().add(centralPanel, BorderLayout.CENTER);
		String[][] randomOrder = reorder();//获得图片的随机摆放顺序
		for(int i=0;i<4;++i)
			for(int j =0;j<4;++j){
				final JButton button = new JButton();
				button.setName(i+""+j);
				button.setIcon(new ImageIcon(randomOrder[i][j]));
				if(randomOrder[i][j].equals("images/00.gif"))
					emptyButton = button;
				button.addActionListener(new ImgButtonAction());
				centralPanel.add(button);
			}
	}
	private String[][] reorder()//生成网格图片的随机摆放顺序
	{
		String[][] exactOrder = new String [4][4];//获得图片正确的摆放顺序
		for(int i=0;i<4;++i)
			for(int j =0;j<4;++j)
				exactOrder[i][j]="images/"+i+j+".gif";
		String[][] randomOrder = new String[4][4];
		for(int i=0;i<4;++i)
			for(int j =0;j<4;++j)
				while(randomOrder[i][j]==null)//随机的指定网格为空
				{
					int r = (int)(Math.random()*4);//随机行
					int c = (int)(Math.random()*4);//取随机列
					if(exactOrder[r][c]!=null){
						randomOrder[i][j] = exactOrder[r][c];
						exactOrder[r][c]=null;
					}
				}
		return randomOrder;
	}
	class ImgButtonAction implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			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("images/00.gif"));
				emptyButton = clickButton;
			}
		}
	}
	class StartButtonAction implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			String[][] randomOrder =reorder();
			int i = 0;
			for(int row=0;row<4;++row)
				for(int col=0;col<4;++col)
				{
					JButton button=(JButton)centralPanel.getComponent(i++);
					button.setIcon(new ImageIcon(randomOrder[row][col]));
					if(randomOrder[row][col].equals("images/00.jpg"))
						emptyButton = button;
				}
		}
	}
}

简易的Java拼图游戏_第1张图片

你可能感兴趣的:(java基础)