当前位置:文档之家› java拼图游戏完整代码

java拼图游戏完整代码

// Cell.javapackage cn.itcast.picture.ui;import javax.swing.Icon;import javax.swing.JButton;/** 图片小方格类*/public class Cell extends JButton {//带有图片的小方格public Cell(Icon icon) {super(icon);//设置小方格大小this.setSize(150, 150);}//带有图片和文字的小方格public Cell(String text, Icon icon) {super(text, icon);//设置小方格大小this.setSize(150, 150);this.setHorizontalTextPosition(CENTER);//设置文字水平居中显示this.setVerticalTextPosition(CENTER);//设置文字垂直居中显示}//public void move(String direction) {switch (direction) {case "UP":this.setLocation(this.getBounds().x,this.getBounds().y-150);break;case "DOWN":this.setLocation(this.getBounds().x,this.getBounds().y+150);break;case "LEFT":this.setLocation(this.getBounds().x-150,this.getBounds().y);break;case "RIGHT":this.setLocation(this.getBounds().x+150,this.getBounds().y);break;default:break;}}}//PictureCanvas.javapackage cn.itcast.picture.ui;import java.awt.Rectangle;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.ImageIcon;import javax.swing.JOptionPane;import javax.swing.JPanel;/** 拼图类*/public class PictureCanvas extends JPanel implements MouseListener { //静态变量public static int pictureID = 1;//图片IDpublic static int stepNum = 0;//步数private Cell[] cell;private boolean hasAddActionListener = false;//表示是否为小方格添加了点击监听,有为trueprivate Rectangle nullCell;//构造方法public PictureCanvas() {initCanvas();}//初始化public void initCanvas(){//设置拼图区的位置this.setLayout(null);//帧布局//创建12个小方格,并添加到拼图区cell = new Cell[12];for(int i = 0;i < 4;i++){for(int j = 0;j < 3;j++){//加载图片int num = i*3+j;ImageIcon icon = new ImageIcon("picture/"+pictureID+"_"+(num+1)+".gif");//创建图片小方格cell[num] = new Cell(icon);//指定显示位置cell[num].setLocation(150*j+20, i*150+20);//把图片小方格添加到拼图区this.add(cell[num]);}}//删除第12个小方格this.remove(cell[11]);nullCell = new Rectangle(320, 470, 150, 150);hasAddActionListener = false;}//重新加载图片,并添加数字提示public void reloadPictureAddNumber() {for(int i = 0;i < 4;i++){for(int j = 0;j < 3;j++){int num = i*3+j;ImageIcon icon = new ImageIcon("picture/"+pictureID+"_"+(num+1)+".gif");cell[num].setIcon(icon);cell[num].setText(num+1+"");cell[num].setVerticalTextPosition(getY()/2);cell[num].setHorizontalTextPosition(getX()/2);}}}//重新加载图片,并清除数字提示public void reloadPictureClearNumber() {for(int i = 0;i < 4;i++){for(int j = 0;j < 3;j++){int num = i*3+j;ImageIcon icon = new ImageIcon("picture/"+pictureID+"_"+(num+1)+".gif");cell[num].setIcon(icon);cell[num].setText("");}}}//start 对小方格重新排序public void start() {//如果没有给小方格添加监听,则添加监听if(!hasAddActionListener){for(int i=0;i<11;i++){cell[i].addMouseListener(this);}hasAddActionListener=true;}//当第一个小方格离左上角比较近的话,进行空方格与就近方格的随机交换while(cell[0].getBounds().x<=170 && cell[0].getBounds().y<=170){ //获取空方格的位置int nullX = nullCell.getBounds().x;int nullY = nullCell.getBounds().y;//随机产生一个方向,进行空方格的移动互换//产生0-3之间的随机数,代表交换移动方向int direction = (int)(Math.random()*4);switch (direction){case 0://空方格向左交换移动,原左侧方格向右移动nullX -= 150;cellMove(nullX,nullY,"RIGHT");break;case 1://空方格向右交换移动,原右侧方格向左移动nullX += 150;cellMove(nullX, nullY, "LEFT");break;case 2://空方格向上交换移动,原上方方格向下移动nullY -= 150;cellMove(nullX, nullY, "DOWN");break;case 3://空方格向下交换移动,原下方方格向上移动nullY += 150;cellMove(nullX, nullY, "UP");break;}}}private void cellMove(int nullX, int nullY, String direction) {for(int i=0;i<11;i++){if(cell[i].getBounds().x == nullX && cell[i].getBounds().y == nullY){ //当前方格移动cell[i].move(direction);//空方格移动(nullX,nullY)nullCell.setLocation(nullX,nullY);break;}}}@Overridepublic void mouseClicked(MouseEvent e) {}@Overridepublic void mousePressed(MouseEvent e) {//获取当前点击的小方格Cell button = (Cell)e.getSource();//获取所点击方格的x,yint clickX = button.getBounds().x;int clickY = button.getBounds().y;//获取空方格的x,yint nullX = nullCell.getBounds().x;int nullY = nullCell.getBounds().y;//比较如果满足条件,则互换位置if(clickX == nullX && clickY - nullY == 150){//点击为空方格下方方格button.move("UP");}else if (clickX == nullX && clickY - nullY == -150) {//点击为空方格上方方格button.move("DOWN");}else if (clickX - nullX == 150 && clickY == nullY) {//点击为空方格右方方格button.move("LEFT");}else if (clickX - nullX == -150 && clickY == nullY) {//点击为空方格左方方格button.move("RIGHT");}else {return;}//更新空方格位置nullCell.setLocation(clickX, clickY);//拼图区重新绘制this.repaint();//更新游戏移动步数stepNum++;PictureMainFrame.step.setText("步数:" + stepNum);//判断若完成,则提示完成if(isFinish()){JOptionPane.showMessageDialog(this, "恭喜你完成了,共用"+stepNum+"步。

相关主题