最近自己看完了java初级篇,课堂上讲的太少了,有那么一会儿灵感蹦出就写了九宫格游戏,只是初版,有些细节未处理,但整体功能都已实现,贴截图:
程序有三个.class文件,依次是程序主体Nine,图片方格Plaid,空白格Blank。
—————-分割线———————-程序主体Nine
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/*** @author Zxin94264* @version 1.4 2015-04-21*/
class Nine extends JFrame {public static Nine nine = new Nine();//创建静态Nine对象,便于在类外直接引用而不用再次建立新对象,这需要构造方法私有化int n=0;//计算所用步数ImageIcon []icon = {new ImageIcon("E:/OneDrive/workspace/src/cn/itcast/s/images/SAO_0.jpg"),new ImageIcon("E:/OneDrive/workspace/src/cn/itcast/s/images/SAO_1.jpg"),new ImageIcon("E:/OneDrive/workspace/src/cn/itcast/s/images/SAO_2.jpg"),new ImageIcon("E:/OneDrive/workspace/src/cn/itcast/s/images/SAO_3.jpg"),new ImageIcon("E:/OneDrive/workspace/src/cn/itcast/s/images/SAO_4.jpg"),new ImageIcon("E:/OneDrive/workspace/src/cn/itcast/s/images/SAO_5.jpg"),new ImageIcon("E:/OneDrive/workspace/src/cn/itcast/s/images/SAO_6.jpg"),new ImageIcon("E:/OneDrive/workspace/src/cn/itcast/s/images/SAO_7.jpg")};Plaid []b ={new Plaid(icon[0]),new Plaid(icon[1]),new Plaid(icon[2]),new Plaid(icon[3]),new Plaid(icon[4]),new Plaid(icon[5]),new Plaid(icon[6]),new Plaid(icon[7])};//创建Plaid数组Point begain_point[] = {new Point(0,0),new Point(131,0),new Point(262,0),new Point(0,123),new Point(131,123),new Point(262,123),new Point(0,246),new Point(131,246)};//创建数组用于保存每个方格期望位置JPanel panel = new JPanel();//创建方格面板/*** 构造方法初始化框架* 添加组件*/private Nine(){//构造方法私有化panel.setSize(300,300);panel.setLayout(new GridLayout(3,3,0,0));//创建布局管理器,3行3列,间隔0this.mass();//调用mass()随机添加有解的排序方格panel.add(Blank.blank);//添加空白格this.add(panel);this.setSize(400,400);this.setLocation(300,200);this.setTitle("Nine拼图");this.setResizable(false);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}/*** mass()* 随机顺序添加方格* 有些情况是无解的,详见:http://bbs.csdn.net/topics/270037619* 可还原性分析: http://blog.sina.com.cn/s/blog_5396eb5301017qv0.html*/public void mass(){ int n[]={0,1,2,3,4,5,6,7};boolean flag=true;Random r = new Random();while(flag){int temp,nixu=0;for(int i=0;i<8;i++){int t = r.nextInt(8);temp=n[i];n[i]=n[t];n[t]=temp;}for(int i=0;i<8;i++){System.out.print(n[i]);}for(int i=1;i<8;i++){for(int j=i-1;j>=0;j--){if(n[j]>n[i]){nixu++;}}}nixu+=6;System.out.print("逆序数为:"+nixu+" ");if(nixu%2==0){System.out.println("找到一种可解随机排序,开始游戏>>>");for(int i=0;i<8;i++){panel.add(b[n[i]]);}flag=false;}else{nixu=0;System.out.println("无解,继续排列>>>");}} }/*** 裁判* IsOver()* 判断是否完成拼图*/public void isOver(){n++;if(b[0].getLocation().equals(begain_point[0])&b[1].getLocation().equals(begain_point[1])&b[2].getLocation().equals(begain_point[2])&b[3].getLocation().equals(begain_point[3])&b[4].getLocation().equals(begain_point[4])&b[5].getLocation().equals(begain_point[5])&b[6].getLocation().equals(begain_point[6])&b[7].getLocation().equals(begain_point[7])){JOptionPane.showMessageDialog(nine, "游戏完成,竟然用了"+n+"步,你数学是体育老师教的吗?简直弱爆了!");}}/*** 主方法,程序入口* @param args*/public static void main(String[] args){ nine.setVisible(true);}
}/***事件监听器*/
class Click extends MouseAdapter{public void mouseReleased(MouseEvent e){JButton pa = (JButton) e.getSource();//得到当前事件源/*** if()语句判断事件源的类型而做出相应的响应*/if(pa instanceof Plaid){Plaid pa1 = (Plaid) pa;//必须再把pa转换回来才能调用move()方法pa1.move();//调用当前Plaid的move()方法Nine.nine.isOver();}}}
———————————分割线———————图片方格Plaid
import java.awt.Point;
import javax.swing.Icon;
import javax.swing.JButton;public class Plaid extends JButton{private int x,y;//方格坐标boolean canrun = false;//方格可否移动/*** 构造方法初始化方格并注册监听器* @param icon*/public Plaid(Icon icon){super(icon);//加载图片this.setSize(100,100);this.addMouseListener(new Click());}/*** move()* 方格移动*/public void move(){Point plaid_point = this.getLocation();//得到当前事件源方格坐标Point blank_point = Blank.blank.getLocation();//得到空白格当前坐标x=plaid_point.x;y=plaid_point.y;int bx = blank_point.x;int by = blank_point.y;System.out.println("事件源的坐标是:"+x+","+y);if((y-by==123)&(x-bx==0)){this.setLocation(bx,by);//方格上移Blank.blank.setLocation(x,y);}else if((by-y==123)&(x-bx==0)){this.setLocation(bx,by);//方格下移Blank.blank.setLocation(x,y);}else if((y-by==0)&(x-bx==131)){this.setLocation(bx,by);//方格左移Blank.blank.setLocation(x,y);}else if((y-by==0)&(bx-x==131)){this.setLocation(bx,by);//方格右移Blank.blank.setLocation(x,y);}else{System.out.println("此方格不可移动!");//无响应}repaint();}
}
————————————分割线———空白格Black
import javax.swing.JPanel;public class Blank extends JPanel{public static Blank blank = new Blank();//静态的对象,便于在Plaid中引用,用于坐标计算,这需要构造方法私有化/*** 构造方法私有化,详细见:* http://zhidao.baidu.com/link?url=zsfuu3kF5bGpDLPiGpofHOYht7OTaoAzsniXCKOgssMiCet6R9kpDgjX6DsyqwLfyyXVgLefadVaIfMVL9Hhh_*/private Blank(){this.setSize(100,100);}
}
完毕,
欢迎指教;
向着进阶篇前进!