java swing实现九宫格拼图游戏

weblog 517 0 0

源码:

package win;
import java.awt.EventQueue;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import java.awt.Color;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class TestWin extends JFrame {
	
	private static final long serialVersionUID = 1L;

	private JPanel contentPane;
	
	private boolean over=true;
	
	private static ImageIcon i=new ImageIcon("E:\\workspace\\oauth\\win\\src\\win\\a.png");

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TestWin frame = new TestWin();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	Map<Integer,JLabel> map=new HashMap<Integer,JLabel>();
	JLabel[][] labels=null;

	/**
	 * Create the frame.
	 */
	public TestWin() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 335, 388);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		panel.setBounds(10, 10, 300, 300);
		contentPane.add(panel);
		panel.setLayout(null);
		
		MyJLabel lblNewLabel = new MyJLabel(1,i);
		lblNewLabel.setVerticalAlignment(SwingConstants.TOP);
		lblNewLabel.setHorizontalAlignment(SwingConstants.LEADING);
		lblNewLabel.setBounds(0, 0, 100, 100);
		panel.add(lblNewLabel);
		map.put(1, lblNewLabel);
		
		MyJLabel label = new MyJLabel(2,i);
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setVerticalAlignment(SwingConstants.TOP);
		label.setBounds(100, 0, 100, 100);
		panel.add(label);
		map.put(2, label);
		
		MyJLabel label_1 = new MyJLabel(3,i);
		label_1.setHorizontalAlignment(SwingConstants.RIGHT);
		label_1.setVerticalAlignment(SwingConstants.TOP);
		label_1.setBounds(200, 0, 100, 100);
		panel.add(label_1);
		map.put(3, label_1);
		
		MyJLabel label_2 = new MyJLabel(4,i);
		label_2.setHorizontalAlignment(SwingConstants.LEADING);
		label_2.setVerticalAlignment(SwingConstants.CENTER);
		label_2.setBounds(0, 100, 100, 100);
		panel.add(label_2);
		map.put(4, label_2);
		
		MyJLabel label_3 = new MyJLabel(5,i);
		label_3.setBounds(100, 100, 100, 100);
		label_3.setHorizontalAlignment(SwingConstants.CENTER);
		label_3.setVerticalAlignment(SwingConstants.CENTER);
		panel.add(label_3);
		map.put(5, label_3);
		
		MyJLabel label_4 = new MyJLabel(6,i);
		label_4.setHorizontalAlignment(SwingConstants.RIGHT);
		label_4.setVerticalAlignment(SwingConstants.CENTER);
		label_4.setBounds(200, 100, 100, 100);
		panel.add(label_4);
		map.put(6, label_4);
		
		MyJLabel label_5 = new MyJLabel(7,i);
		label_5.setVerticalAlignment(SwingConstants.BOTTOM);
		label_5.setHorizontalAlignment(SwingConstants.LEADING);
		label_5.setBounds(0, 200, 100, 100);
		panel.add(label_5);
		map.put(7, label_5);
		
		MyJLabel label_6 = new MyJLabel(8,i);
		label_6.setVerticalAlignment(SwingConstants.BOTTOM);
		label_6.setHorizontalAlignment(SwingConstants.CENTER);
		label_6.setBounds(100, 200, 100, 100);
		panel.add(label_6);
		map.put(8, label_6);
		
		MyJLabel label_7 = new MyJLabel(0,i);
		label_7.setBackground(Color.LIGHT_GRAY);
		label_7.setVerticalAlignment(SwingConstants.BOTTOM);
		label_7.setHorizontalAlignment(SwingConstants.RIGHT);
		label_7.setBounds(200, 200, 100, 100);
		panel.add(label_7);
		map.put(9, label_7);
		
		/**
		 * 	所有label添加点击事件
		 */
		for (Map.Entry<Integer, JLabel> m : map.entrySet()) {
			JLabel jl=m.getValue();
			jl.addMouseListener(new MyMouseAdapter(jl) {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					if(over) {
						return;
					}
					for(int i=0;i<=2;i++) {
						for(int j=0;j<=2;j++) {
							System.out.print(((MyJLabel)labels[i][j]).index+" ");
						}
						System.out.println();
					}
					int w=this.jlabel.getX()/100;
					int h=this.jlabel.getY()/100;
					boolean b=bar(w,h,label_7);
					if(b) {
						int jw=label_7.getX()/100;
						int jh=label_7.getY()/100;
						labels[h][w]=label_7;
						labels[jh][jw]=this.jlabel;
						int lx=label_7.getX();
						int ly=label_7.getY();
						label_7.setLocation(this.jlabel.getX(),this.jlabel.getY());
						this.jlabel.setLocation(lx, ly);
					}
					System.out.println();
					for(int i=0;i<=2;i++) {
						for(int j=0;j<=2;j++) {
							System.out.print(((MyJLabel)labels[i][j]).index+" ");
						}
						System.out.println();
					}
					if(isOver()) {
						over=true;
						label_7.setIcon(i);
						System.out.println("结束");
						JOptionPane.showMessageDialog(null, "完成拼图", "成功",JOptionPane.WARNING_MESSAGE);
					}
				}
			});
	    }
		/**
		 * 	随机打乱顺序
		 */
		JButton button_1 = new JButton("\u968F\u673A\u751F\u6210");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				over=false;
				labels=new JLabel[3][3];
				LinkedList<JLabel> l=new LinkedList<JLabel>();
				for (Map.Entry<Integer, JLabel> m : map.entrySet()) {
			        l.add(m.getValue());
			    }
				/**
				 * 	集合了乱序
				 */
				Collections.shuffle(l);
				for(int i=0;i<=2;i++) {
					for(int j=0;j<=2;j++) {
						labels[i][j]=l.removeFirst();
						labels[i][j].setBounds(j*100, i*100, 100, 100);
					}
				}
				label_7.setIcon(null);
				/**
				 * 	下方判断 乱序后的 九宫格 是否可以复原  判断方法(逆序数)
				 */
				int cnt = 0;
				int m[]=new int[9];
				int t=0;
				for(int i=0;i<3;i++) {
					for(int j=0;j<3;j++) {
						m[t]=((MyJLabel)labels[i][j]).index;
						t++;
					}
				}
				for(int p = 0; p < 8; p++)
				     for(int q = p + 1; q < 9; q++)
				          if(m[p] > m[q] && m[q] != 0)
				               cnt++;
				if(cnt % 2==1){
					System.out.println("无解");
					for(int i=0;i<=2;i++) {
						for(int j=0;j<=2;j++) {
							System.out.print(((MyJLabel)labels[i][j]).index+" ");
						}
						System.out.println();
					}
					JLabel jl1=map.get(1);
					JLabel jl2=map.get(2);
					/**
					 * 	将其变为有解
					 */
					if(jl1!=null&&jl1!=label_7&&jl2!=null&&jl2!=label_7) {
						int w1=jl1.getX()/100;
						int h1=jl1.getY()/100;
						int w2=jl2.getX()/100;
						int h2=jl2.getY()/100;
						System.out.println(jl1+":"+jl2);
						labels[h2][w2]=jl1;
						labels[h1][w1]=jl2;
						jl1.setBounds(w2*100, h2*100, 100, 100);
						jl2.setBounds(w1*100, h1*100, 100, 100);
						for(int h=0;h<=2;h++) {
							for(int j=0;j<=2;j++) {
								System.out.print(((MyJLabel)labels[h][j]).index+" ");
							}
							System.out.println();
						}
					}
				}else {
					System.out.println("有解");
				}
			}
		});
		button_1.setBounds(10, 316, 93, 23);
		contentPane.add(button_1);
	}
	/**
	 * 	判断是否完成拼图
	 * @return
	 */
	public boolean isOver() {
		int t=1;
		for(int i=0;i<3;i++) {
			for(int j=0;j<3;j++) {
				if(labels[i][j]!=map.get(t)) {
					System.out.print("i:"+i+" j:"+j+" "+"t:"+t+"    "+labels[i][j]+" - ");
					System.out.println(map.get(t));
					return false;
				}
				t++;
			}
		}
		return true;
	}
	/**
	 * 	判断是否可以移动
	 * @param x
	 * @param y
	 * @param label7
	 * @return
	 */
	public boolean bar(int w,int h,JLabel label7) {
		if(labels==null) {
			return false;
		}else {
			if(w-1>=0)
				if(labels[h][w-1]==label7)
					return true;
			if(h-1>=0)
				if(labels[h-1][w]==label7)
					return true;
			if(w+1<=2)
				if(labels[h][w+1]==label7)
					return true;
			if(h+1<=2)
				if(labels[h+1][w]==label7)
					return true;
		}
		return false;
	}
	class MyMouseAdapter extends MouseAdapter {
		public JLabel jlabel;
		public MyMouseAdapter(JLabel jlabel) {
			this.jlabel=jlabel;
		}
	}
	class MyJLabel extends JLabel{
		private static final long serialVersionUID = 1L;
		public int index;
		public MyJLabel(int  index,ImageIcon icon) {
			this.index=index;
			this.setIcon(icon);
		}
		@Override
		public String toString() {
			return "MyJLabel [index=" + index + "]";
		}
	}
}

 

需要 原 图片 资源请下载压缩包

猜你喜欢
数据结构与算法 1165 java五子棋人机对战packagefir;importjava.awt.*;importjavax.swing.JPanel;/***有背景片的Panel类*@authortntxia
工具 2620 java完美html转pdf1.pom依赖:dependencygroupIdcom.itextpdf/groupIdartifactIditextpdf
jar依赖 923 javaworld转pdf所需jar包-pdf在线预览一、依赖文件二、java代码packagetest.pdftest;importjava.awt.Color
weblog 389 java服务端sha256加密importjava.io.UnsupportedEncodingException;importjava.security.MessageDigest
weblog 2983 是时间复杂度确能降低到o(n2)。百科迪杰斯特拉算法(Dijkstra)是由荷兰计算机科学家狄克斯特拉于1959年提出的,因此又叫狄克斯特拉算法。是从一个顶点到其余各顶点的最短路径算法,解决的是有权
java基础 567 java中break的特别用法(goto)publicclassA8{ publicstaticvoidmain(String[]args){ a: for(inti=0;i10;i
java基础 581 java集合之TreeMap原理TreeMap集合的说简单也简单说复杂也复杂,说简单是因为TreeMap底层完全依靠红黑树这个数据结构,相比与HashMap来说TreeMap不用考虑
工具 651 javaword转pdf在线预览(可以带水印)使用之前需要两个jar包的依赖文件下载地址:http://www.jiajiajia.club/file/info/4uwe2d/111Java
目录