import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.swing.filechooser.FileFilter;
import java.util.Random;
import javax.swing.*;
import java.awt.Point;
import java.awt.Color;
public class PinTu extends JFrame implements ActionListener{
private int imglenth,imgheith; //图片的长宽
JButton[][] button = new JButton[3][3];
byte[][] bytes = new byte[3][3];
private BufferedImage bfimg;
public PinTu(){
loadimg();
loadbutton();
this.setTitle("拼图游戏---PowerBy 张养静");
this.setResizable(false);
this.setBounds(100,100,imglenth + 8,imgheith + 25);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setLayout(null);
this.setVisible(true);
}
private void loadimg(){
String url = "img.jpg";
JFileChooser fc=new JFileChooser(); //用文件选择框进行文件选择
fc.setDialogTitle("打开一个图片");
fc.addChoosableFileFilter(new ImageFilter());
int returnVal = fc.showOpenDialog(this);
fc.setVisible(true);
if (returnVal == JFileChooser.APPROVE_OPTION){
File file = fc.getSelectedFile();
url = file.getPath();
}
Image img = this.getToolkit().createImage(url); //加载图片
MediaTracker tracker = new MediaTracker(this); //等待图片加载完成
tracker.addImage(img, 0);
try
{
tracker.waitForAll(); //等待全部加入
}
catch (Exception ex)
{
System.err.println(ex.toString());
}
imglenth = img.getWidth(null); //得到图片的宽,高
imgheith = img.getHeight(null);
bfimg = new BufferedImage(img.getWidth(null),img.getHeight(null),BufferedImage.TYPE_INT_ARGB);
bfimg.getGraphics().drawImage(img,0,0,null); //把图片画到缓冲区图片上
}
private void loadbutton(){
byte num = 0;
for(int i = 0;i < 3;i++){ //对各按钮加载图片
for(int j = 0;j < 3;j++){
bytes[i][j] = num++;
button[i][j] = new JButton(new ImageIcon(bfimg.getSubimage(j*bfimg.getWidth()/3,i*bfimg.getHeight()/3,bfimg.getWidth()/3,bfimg.getHeight()/3)));
button[i][j].addActionListener(this);
button[i][j].setBounds(j*bfimg.getWidth()/3,i*bfimg.getHeight()/3,bfimg.getWidth()/3,bfimg.getHeight()/3);
this.add(button[i][j]);
}
}
button[2][2].setBackground(new Color(39,150,231));
button[2][2].setIcon(new ImageIcon(bfimg.getScaledInstance(bfimg.getWidth()/3 - 19,bfimg.getHeight()/3 - 19,Image.SCALE_SMOOTH))); //设置右下角的图片为整张图片的缩略图
/*
for(int i = 0;i < 3;i++){
for(int j = 0;j < 3;j++){
System.out.print(bytes[i][j]);
}
System.out.println();
}
*/
Random suiji = new Random();
for(int i = 0;i < 50;i++){ //打乱图片按钮的顺序
exchange(suiji.nextInt(9),suiji.nextInt(9));
}
/*
for(int i = 0;i < 3;i++){
for(int j = 0;j < 3;j++){
System.out.print(bytes[i][j]);
}
System.out.println();
}
//*/
}
public void actionPerformed(ActionEvent arg0) { //对鼠标单击事件的监听
for(int i = 0;i < 3;i++){ //循环找出点击了哪个按钮
for(int j = 0;j < 3;j++){
if(arg0.getSource() == button[i][j]){
//System.out.println(i);
//System.out.println(j);
byte tem = (byte)(i*3 + j);
for(int y = 0;y < 3;y++){ //循环找出点击按钮在数组位图中的位置
for(int x = 0;x < 3;x++){
if(bytes[y][x] == tem){
//System.out.println();
//System.out.println(y);
//System.out.println(x);
if(y > 0){ //对所点的按钮的四周进行判断,是否有空位置,是否可以移动
if(bytes[y - 1][x] == 8){
exchange(i*3 + j,8);
check();
return;
}
}
if(y < 2){ //对所点的按钮的四周进行判断,是否有空位置,是否可以移动
if(bytes[y + 1][x] == 8){
exchange(i*3 + j,8);
check();
return;
}
}
if(x > 0){ //对所点的按钮的四周进行判断,是否有空位置,是否可以移动
if(bytes[y][x - 1] == 8){
exchange(i*3 + j,8);
check();
return;
}
}
if(x < 2){ //对所点的按钮的四周进行判断,是否有空位置,是否可以移动
if(bytes[y][x + 1] == 8){
exchange(i*3 + j,8);
check();
return;
}
}
}
}
}
}
}
}
}
private void exchange(int a,int b){ //传入参数是按钮最初的编号,在标志数组中的编号
int xtem1,ytem1,xtem2,ytem2; //x,y坐标缓存
//int loctem1,loctem2; //位置缓存
xtem1 = a%3;
ytem1 = a/3;
xtem2 = b%3;
ytem2 = b/3;
Point ptem = button[ytem1][xtem1].getLocation(); //交换按钮位置
button[ytem1][xtem1].setLocation(button[ytem2][xtem2].getLocation());
button[ytem2][xtem2].setLocation(ptem);
//交换标志数组内容
for(int i = 0;i < 3;i++){
for(int j = 0;j < 3;j++){
if(bytes[i][j] == a){
xtem1 = j;
ytem1 = i;
}
if(bytes[i][j] == b){
xtem2 = j;
ytem2 = i;
}
}
}
byte btem;
btem = bytes[ytem1][xtem1];
bytes[ytem1][xtem1] = bytes[ytem2][xtem2];
bytes[ytem2][xtem2] = btem;
}
private void check(){ //检查移动后的位置是否导致游戏结束
for(int i = 0;i < 3;i++){
for(int j = 0;j < 3;j++){
if(bytes[i][j] != i*3 +j){
System.out.println(i);
System.out.println(j);
return;
}
}
}
JOptionPane.showMessageDialog(null,"聪明小孩!!");
}
private class ImageFilter extends FileFilter { //文件类型过滤类
//Accept all directories and all gif, jpg, tiff, or png files.
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
}
if(f.getAbsolutePath().endsWith(".gif") || f.getAbsolutePath().endsWith(".jpg") || f.getAbsolutePath().endsWith(".bmp")){
return true;
}
return false;
}
public String getDescription() {
return "仅图片文件";
}
}
public static void main(String args[]){
new PinTu();
}
}