package cn.hncu.gui.swing;
import java.awt.Dialog;
import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.Icon;
import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;public class LowLow extends JFrame implements ActionListener, MouseListener {
String pic1 = "pictures\\fff-";//被分割的小图的路径和名字 String pic2 = "pictures\\lll";//完整图的路径和名字 int MouseCount = 0;//玩家的移动次数 int number =200;//系统之前的移动次数,一定程度上可以控制图片乱序程度 String DaAn = null; int brun = 15; MyJButton[] b = new MyJButton[16]; MessageDialog dlg = new MessageDialog(this); Icon[] image = new Icon[16]; int VisDong = -1; public LowLow(String str){ super(str); setBounds(600,200,400,400); setResizable(false); getContentPane().setLayout(new GridLayout(4, 4)); int[] Arr = Arr(); for(int i =0 ;i<16;i++){ image[i] = new ImageIcon(pic1+i+".png"); } for(int i=0;i<16;i++){ b[i]=new MyJButton(image[i],i); b[i].setSize(100, 100); getContentPane().add(b[i]); b[i].addActionListener(this); b[i].addMouseListener(this); } run(); setVisible(true); } public void Rrun(int i){ MyJButton bb = new MyJButton(); bb.setIcon(b[brun].getIcon());bb.setZhi(b[brun].getZhi()); b[brun].setIcon(b[brun+i].getIcon());b[brun].setZhi(b[brun+i].getZhi()); b[brun+i].setIcon(bb.getIcon());b[brun+i].setZhi(bb.getZhi()); brun+=i; } private void run() { int[] Arr = Arr(); for(int i =0;i<number;i++){ if(Arr[i]==-4&&brun-4>=0){//上 Rrun(Arr[i]); DaAn = "下"+DaAn; } if(Arr[i]==4&&brun+4<=15){//下 Rrun(Arr[i]); DaAn = "上"+DaAn; } if(Arr[i]==-1&&brun%4!=0){//左 Rrun(Arr[i]); DaAn = "右"+DaAn; } if(Arr[i]==1&&(brun+1)%4!=0){//右 Rrun(Arr[i]); DaAn = "左"+DaAn; } } System.out.println("必胜:"+DaAn); }public static void main (String[] args){
new LowLow("低级拼图"); } //产生随机数组 public int[] Arr(){ int[] t = {-1,1,-4,4}; int[] a = new int[number]; for(int i=0;i<number;i++){ int temp = (int) (Math.random()*10)%4; a[i] = t[temp]; } return a; } //判断是否移动 public boolean Mo(int i){ VisDong = -1; for(int j=0;j<16;j++){ if(b[j].p==15){ VisDong=j; break; } } if(i+1==VisDong||i-1==VisDong||i+4==VisDong||i-4==VisDong){ return true; } return false; } //判断是否结束 public boolean gameOver(){ for(int i =0;i<16;i++){ if(b[i].p!=i)return false; } return true; } class MyJButton extends JButton{ private int p; private Icon image; public MyJButton(){ } public MyJButton(Icon image,int p){ super(image); this.p = p; } public int getZhi(){ return p; } public void setZhi(int p){ this.p=p; } } @Override public void actionPerformed(ActionEvent e) { MouseCount++; for(int i=0;i<16;i++){ if(e.getSource()==b[i]&&Mo(i)){ MyJButton bb = new MyJButton(); bb.setIcon(b[i].getIcon());bb.setZhi(b[i].getZhi()); b[i].setIcon(b[VisDong].getIcon());b[i].setZhi(b[VisDong].getZhi()); b[VisDong].setIcon(bb.getIcon());b[VisDong].setZhi(bb.getZhi()); } } if(gameOver()){ JOptionPane.showMessageDialog(this, "恭喜您赢了!!\r\n移动次数:"+MouseCount); System.out.println("恭喜您赢了!!"); System.out.println("移动次数:"+MouseCount); } }@Override
public void mouseClicked(MouseEvent e) { }@Override
public void mousePressed(MouseEvent e) { for(int i=0;i<16;i++){ if(e.getSource()==b[i]&&b[i].p==15){ dlg.show(""); } } } public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub }@Override
public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub }@Override
public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } //自定义的对话框类 class MessageDialog extends JDialog{ private JFrame jframe; private JLabel label; public MessageDialog(JFrame jframe){ super(jframe,"原图",true); setBounds(150, 200, 400, 400); setDefaultCloseOperation(HIDE_ON_CLOSE); setResizable(false); label = new JLabel(new ImageIcon(pic2+".png")); getContentPane().add(label); } public void show(String str){ label.setText(str); setVisible(true); } }}