import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.File;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.UIManager;
import static java.lang.Math.sqrt;
public class GameView extends JFrame{//窗口框架
private GamePanel mainJPanel=new GamePanel();//拼图面板
private int rows=4;
private int cols=4;
private PazzlePieces pp[]=new PazzlePieces[rows*cols];
private static int initx=20;
private static int inity=20;
private int width=100;//100
private int height=100;
private JButton Onbutton= new JButton("S T A R T");
private JButton Overbutton=new JButton("D O N E");
private GameView() {
this.setLayout(null);
this.getContentPane().setBackground(Color.decode("#FFFFCC"));
this.setBounds(0,0,1000,800);
int i = 0;
// mainJPanel.setOpaque(false);
Onbutton.setBounds(900,30,80,50);
setVisible(true);
add(Onbutton);
Onbutton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
for(int k=0;k<pp.length;k++){
pp[k].resetsite(pp[k].oriX,pp[k].oriY);
}
JOptionPane.showMessageDialog(GameView.this, "Are you Ready ?");
for(int k=0;k<pp.length;k++) {
pp[k].ReOrder(k);
}
Onbutton.setEnabled(false);
}
});
Overbutton.setBounds(900,110,80,50);
setVisible(true);
add(Overbutton);
Overbutton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
CheckResult();
Onbutton.setEnabled(false);
Onbutton.setText("Restart");
Onbutton.setEnabled(true);
}
});
cut("pic\\1.jpg","pic\\",cols,rows);
for(;i<rows*cols;i++){
pp[i]=new PazzlePieces();
pp[i].setBounds(width*(int)(i%cols),height*(int)(i/rows),width,height);
ImageIcon icon=new ImageIcon("pic\\" + "pre_map_" + (i/cols)+ "_" + (i%rows)+ ".jpg");
Image tmp=icon.getImage().getScaledInstance(pp[i].getWidth(),pp[i].getHeight(),icon.getImage().SCALE_DEFAULT);
icon=new ImageIcon(tmp);
pp[i].setIcon(icon);
pp[i].initsite();
final int j=i;
pp[i].addMouseMotionListener( new MouseMotionAdapter(){
int preX,preY;
@Override
public void mouseDragged(MouseEvent e){
super.mouseDragged(e);
Point p=pp[j].getLocation();
pp[j].setLocation(p.x+(e.getX()-preX),p.y+(e.getY()-preY));//移动前坐标位置+鼠标移动前后的位置差
// System.out.println("pp"+j+"移动:"+pp[j].getLocation());
}
});
pp[i].addMouseListener(new MouseAdapter() {
int preX,preY;
double min=50,tmp;
int no=0;
@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
for(int n=0;n<pp.length;n++){
tmp=pp[j].caldis(pp[n].oriX,pp[n].oriY);
if(tmp<min){
min=tmp;
no=n;
}
}
if(pp[no].flag==1&no>0) {
//pp[j].setLocation();
pp[j].disordX=pp[j].getX();
pp[j].disordY=pp[j].getY();
pp[no].flag=0;
// pp[j].setEnabled(false);
}
}
@Override
public void mouseReleased(MouseEvent e) {
super.mouseReleased(e);
double min=50;
double tmp;
int no=0;
for(int n=0;n<pp.length;n++){
tmp=pp[j].caldis(pp[n].oriX,pp[n].oriY);
if(tmp<min){
min=tmp;
no=n;
}
}
if(pp[no].flag==0&no>0) {
pp[j].setLocation(pp[no].oriX, pp[no].oriY);
pp[j].disordX=pp[no].oriX;
pp[j].disordY=pp[no].oriY;
pp[no].flag=1;
// pp[j].setEnabled(true);
}
else if(pp[no].flag==1&no>0){
pp[j].setLocation(pp[j].disordX,pp[j].disordY);
}
// System.out.println("鼠标释放");
}
});
mainJPanel.add(pp[i]);
}
//ReOrder();
add(mainJPanel);
mainJPanel.setBackground(Color.decode("#99CC99"));
}
private class GamePanel extends JPanel{
private GamePanel(){
this.setLayout(null);
this.setBounds(initx, inity,800,750);
}
}
private void CheckResult(){
int result=1;
for(int n=0;n<pp.length;n++){
if(pp[n].getX()==pp[n].oriX&&pp[n].getY()==pp[n].oriY) {result*=1;pp[n].setEnabled(true);}
else result*=0;
}
if(result==1) {
JOptionPane.showMessageDialog(GameView.this, "同九年,汝何秀!");
}
if(result==0) JOptionPane.showMessageDialog(GameView.this, "emmm..没点亮的就是拼错了");
}
private int IsR(int a,int b,int xdis,int ydis){
int difx=pp[a].getX()-pp[b].getX();
int dify=pp[a].getY()-pp[b].getY();
if(difx<=xdis&dify<=ydis)
return 1;
else return 0;
}
//图片切割算法改自百度文库
public static void cut(String srcImageFile, String descDir, int cols,int rows) {
try {
Image img;
ImageFilter cropFilter;
// 读取源图像
BufferedImage bi = ImageIO.read(new File(srcImageFile));
int srcWidth = bi.getHeight(); // 源图宽度
int srcHeight = bi.getWidth(); // 源图高度
int destWidth=srcWidth/cols;
int destHeight=srcHeight/rows;
if (srcWidth > destWidth && srcHeight > destHeight) {
Image image = bi.getScaledInstance(srcWidth, srcHeight,
Image.SCALE_DEFAULT);
// 循环建立切片
// 改进的想法:是否可用多线程加快切割速度
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// 四个参数分别为图像起点坐标和宽高
// 即: CropImageFilter(int x,int y,int width,int height)
cropFilter = new CropImageFilter(j * destWidth, i * destHeight,
destWidth, destHeight);
img = Toolkit.getDefaultToolkit().createImage(
new FilteredImageSource(image.getSource(),
cropFilter));
BufferedImage tag = new BufferedImage(destWidth,
destHeight, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, null); // 绘制缩小后的图
g.dispose();
// 输出为文件
ImageIO.write(tag, "JPEG", new File(descDir
+ "pre_map_" + i + "_" + j + ".jpg"));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public class PazzlePieces extends JButton {//拼图按钮类
public int oriX, oriY;
public int disordX, disordY;
public int flag;
private PazzlePieces() {
// this.setSize(ImgW,ImgH);
this.setVisible(true);
this.setBorderPainted(false);
this.flag=0;
oriX=0;
oriY=0;
}
private void initsite(){
oriX=this.getX();
oriY=this.getY();
this.setEnabled(true);
}
public void resetsite(int newx,int newy){
this.setLocation(newx,newy);
this.flag=0;
this.setEnabled(true);
}
public double caldis(int x,int y){
int xx=this.getX()-x;
int yy=this.getY()-y;
return sqrt(xx*xx+yy*yy);
}
public void ReOrder(int k){
Random random=new Random();
int w=width;
int h=height;
if(k!=0) {
disordX = (k % 4+1) * random.nextInt(80) + (k%3+3)*80;
disordY = (k % 4+1) * random.nextInt(80) + (k%3+3)*80;
pp[k].setLocation(new Point(disordX, disordY));
}
pp[k].setEnabled(false);
}
}
public static void main(String[] args){
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");//Nimbus风格,jdk6 update10版本以后的才会出现
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());//当前系统风格
GameView mypazzle=new GameView();
mypazzle.setTitle("有本事你拼拼看啊");
mypazzle.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
得赶作业,先贴代码