这是我做的一个拼图小游戏,是把图片放到按钮上了,问题是怎么判断最后拼了完整的图?
import java.awt.Graphics;
import java.awt.GridLayout;
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.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
import javax.print.attribute.standard.Media;
import javax.swing.AbstractButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Pintu extends JFrame implements ActionListener{
Image img;
Icon img1;
BufferedImage bimg;
BufferedImage[] subimg;
int width,height;
JButton[] jb;
public Pintu(){
this.setLayout(new GridLayout(3,3));
img=this.getToolkit().getImage("F:\\JAVA\\eclipse文件\\pintu\\8.jpg");
MediaTracker me=new MediaTracker(this);
me.addImage(img, 0);
try {
me.waitForAll(0);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
width=img.getWidth(this);
height=img.getHeight(this);
bimg=new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
Graphics bg=bimg.getGraphics();
bg.drawImage(img,0,0,this);
subimg=new BufferedImage[9];
int a[] = new int[9];
for (int i = 0; i < a.length; i++) {
a[i] = (int) (Math.random() * 9) ;
for (int k = 0; k < i; k++) {
if (a[i] == a[k]) {
i--;
continue;
}
}
}
int z=0;
for(int x=0;x<3;x++){
for(int y=0;y<3;y++){
subimg[a[z]]=bimg.getSubimage(width*x/3, height*y/3, width/3, height/3);
z++;
}
}
jb=new JButton[9];
for(int i=0;i<jb.length;i++){
jb[i]=new JButton();
jb[i].addActionListener(this);
this.add(jb[i]);
jb[i].setIcon(new ImageIcon(subimg[i]));
}
setSize(img.getWidth(this),img.getHeight(this));
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
/*public void paint(Graphics g){
g.drawImage(subimg[0],0,0,this);
}*/
public static void main(String[] args) {
new Pintu();
}
public void actionPerformed(ActionEvent e) {
img1=jb[0].getIcon();
jb[0].setIcon(((AbstractButton) e.getSource()).getIcon());
((AbstractButton) e.getSource()).setIcon(img1);
}
}
我的想的是按钮的编号与小图片的编号全部吻合时就能判断成功了,但是我不知道具体怎么实现,希望哪位大哥能够说的清楚点,小弟谢谢了!