import java.io.IOException;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
// 华容道原理的拼图游戏. 利用轻组建的套用.
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MyMainFrame extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
MyCanvas myCanvas;
JPanel panelNorth,panelPreview;
Button start,preview,set;
Container container;
public MyMainFrame() {//初使化
container=this.getContentPane();
start=new Button("开始");
start.addActionListener(this);
preview=new Button("预览");
preview.addActionListener(this);
set = new Button("设置");
set.addActionListener(this);
panelPreview=new JPanel();
panelPreview.setLayout(null);
Icon icon=new ImageIcon ("images/pic_"+MyCanvas.pictureID+".jpg");
JLabel label=new JLabel(icon);
panelPreview.add(label);
panelNorth=new JPanel();
panelNorth.setBackground(Color.yellow);
panelNorth.add(start);
panelNorth.add(preview);
panelNorth.add(set);
myCanvas=new MyCanvas();
container.add(myCanvas,BorderLayout.CENTER);
container.add(panelNorth,BorderLayout.NORTH);
this.setResizable(false);
this.setVisible(true);
} //end of 初始化 构造函数
public void actionPerformed(ActionEvent e) {
Button button=(Button)e.getSource();
if(button==start){
myCanvas.Start();
}else if(button==preview){
if(button.getLabel()=="预览"){
container.remove(myCanvas);
container.add(panelPreview);
panelPreview.updateUI();
container.repaint();
button.setLabel("返回");
}else{
container.remove(panelPreview);
container.add(myCanvas);
button.setLabel("预览");
}
}else if(button==set){
Choice pic = new Choice();
//pic.add("QQ");
pic.add("美女");
int i=JOptionPane.showConfirmDialog(this,pic,"选择图片", JOptionPane.OK_CANCEL_OPTION);
//使用选择对话框来进行选择图片.
if(i==JOptionPane.YES_OPTION){
myCanvas.reLoadPictrue();
Icon icon=new ImageIcon("images/pic_"+MyCanvas.pictureID+".jpg");
panelPreview.removeAll();
panelPreview.repaint();
public static void main(String[] args) throws UnsupportedAudioFileException, LineUnavailableException, IOException
{
new MyMainFrame();
} //end of main
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class MyCanvas extends JPanel implements MouseListener
boolean hasAddActionListener=false;//设置方格的动作监听器的标志位,TRUE为已经添加上动作事件
Cell cell[];//定义方格
Rectangle cellNull;//定义空方格区域 是一个矩形类
public MyCanvas() {
this.setLayout(null);
Icon icon;
public void reLoadPictrue(){//当选择其它图形进行拼图时,需重新加载新图片
public boolean isFinish(){//判断是否拼合成功
{ int x=cell[i].getBounds().x;
int y=cell[i].getBounds().y;
return false;
} //end of for
return true;
public void Start(){//对方格进行重新排列,打乱顺序
while(cell[0].getBounds().x=100cell[0].getBounds().y=100){//当第一个方格距左上角较近时
int x=cellNull.getBounds().x;
int y=cellNull.getBounds().y;
if(direction==0){//空方格左移动,与左侧方格互换位置,左侧方格右移动
x-=100;
if(test(x,y)){
if((cell[j].getBounds().x==x)(cell[j].getBounds().y==y)){//依次寻找左侧的按钮
cell[j].move("RIGHT",100);
cellNull.setLocation(x,y);
break;//找到后跳出for循环
}else if(direction==1){//RIGHT
x+=100;
if((cell[j].getBounds().x==x)(cell[j].getBounds().y==y)){
cell[j].move("LEFT",100);
break;
y-=100;
cell[j].move("DOWN",100);
}else{//DOWN
y+=100;
cell[j].move("UP",100);
if(!hasAddActionListener)//如果尚未添加动作事件,则添加
cell[i].addMouseListener(this);
hasAddActionListener=true;
private boolean test(int x,int y){
else
public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mousePressed(MouseEvent e) {
//方格的鼠标事件,因为用到了MyCanvas中的一些方法,所以呢没有在Cell类中处理鼠标事件
Cell button=(Cell)e.getSource();
int x1=button.getBounds().x;//得到所单击方格的坐标
int y1=button.getBounds().y;
button.move("UP",100);
button.move("DOWN",100);
button.move("LEFT",100);
button.move("RIGHT",100);
return;//不满足就不进行任何处理
cellNull.setLocation(x1,y1);
this.repaint();
if(this.isFinish()){//进行是否完成的判断
JOptionPane.showMessageDialog(this,"景锋恭喜你完成拼图,加油!想继续下一关么?");
cell[i].removeMouseListener(this);//如果已完成,撤消鼠标事件,鼠标单击方格不在起作用
hasAddActionListener=false;
import javax.swing.JButton;
public class Cell extends JButton {
Cell(Icon icon){//实际为ICON
super(icon);
this.setSize(100,100);
public void move(String direction,int sleep){//方格的移动
if(direction=="UP"){
this.setLocation(this.getBounds().x,this.getBounds().y-100);
}else if(direction=="DOWN"){
this.setLocation(this.getBounds().x,this.getBounds().y+100);
}else if(direction=="LEFT"){
this.setLocation(this.getBounds().x-100,this.getBounds().y);
this.setLocation(this.getBounds().x+100,this.getBounds().y);
(1)如何实现图片移动
语言组织能力有限.讲不太清楚.
//以上均引用不同的package内的类
public class MyMainFrame extends JFrame implements ActionListener {//MyMainFrame类extends "JFrame"类实现 ActionListener的方法
MyCanvas myCanvas; //初始化对象MyCanvas类
JPanel panelNorth,panelPreview;//定义上方的面板,及预览所需的面板
Button start,preview,set;//定义开始,预览,设定按钮
Container container;//容器,得到内容面板
container=this.getContentPane(); //得到当前对象的ContentPane,并且把它赋给container
start=new Button("开始");//创建并初始新的Button(按钮)对象,赋给start
start.addActionListener(this); //在这个按钮对象中添加监听器,范围是当前对象
preview=new Button("预览");//同上,创建新的Button对象.....
preview.addActionListener(this);//同上.....
set = new Button("设置"); //同上.....(感觉代码都差不多吧?呵呵)
set.addActionListener(this);//同上
panelPreview=new JPanel(); 创建新的JPanel(面板)对象
panelPreview.setLayout(null); //设置面板对象的布局为空
JLabel label=new JLabel(icon); //定义新的JLable(java标签),并初始
panelPreview.add(label); //面板对象中添加label这个对象
panelNorth=new JPanel(); //定义新的JPanel
panelNorth.setBackground(Color.red); //设置JPanel的背景色
panelNorth.add(start); //Jpanel加入按钮
panelNorth.add(preview); //同上
panelNorth.add(set); //同上
myCanvas=new MyCanvas(); //实例化MyCanvas
container.add(myCanvas,BorderLayout.CENTER);//在容器(前边定义好了这个对象)中添加myCanvas,设置它的布局为居中
container.add(panelNorth,BorderLayout.NORTH);//添加Jpanel,布局为北(也就是上)
this.setTitle("拼图小游戏-"); //设置这个对象的题目叫.....
this.setResizable(false); //设置是否可以改变窗口的大小(否)
this.setVisible(true); //是否可以显示(是)
public static void main(String[] args) {//类进口,也就是主程序的进口
// TODO 自动生成方法存根
new MyMainFrame(); //实例化主框架
public void actionPerformed(ActionEvent arg0) {//对三个按钮事件的处理
Button button=(Button)arg0.getSource(); //取得通过监听得到的动作时间对象的源(getSource具体得到的是啥,我也不太清楚)
if(button==start){ //判断.如果监听到的是按start按钮
myCanvas.Start(); //启动myCanvas
}else if(button==preview){ //如果是preview按钮
if(button.getLabel()=="预览"){ //如果按钮的标签是"预览"
container.remove(myCanvas); //容器销毁myCanvas
container.add(panelPreview); //然后容器添加panelPreview对象
panelPreview.updateUI(); //panelPreview对象的upDateUI方法
container.repaint(); //调用容器重新画图的命令
button.setLabel("返回"); //标签设置成"返回"
}else{ //如果以上两个if都不是,执行下边的语句
container.remove(panelPreview); //cantainer销毁p....这个对象
container.add(myCanvas); //添加m...这个对象
container.repaint(); //容器重新画图
button.setLabel("预览"); //设置标签名称为"预览"
}else if(button==set){//修改所选图片 //如果间听到的是按set这个键的时候
Choice pic = new Choice(); //Choice实例化对象(这个是啥类,我也不知道,名字上看是选择?)
pic.add("小猫"); //添加小猫
pic.add("小猪"); //添加小猪
pic.add("云"); //添加云
pic.add("QQ"); //添加qq
pic.add("卡通"); //添加卡通
pic.add("花"); //花
int i=JOptionPane.showConfirmDialog(this, pic, "选择图片", JOptionPane.OK_CANCEL_OPTION);//定义一个int类型的对象i,赋值成后边的那些
if(i==JOptionPane.YES_OPTION){ //如果对象i与JOptionPane对象的YES...相等,则执行下列代码
MyCanvas.pictureID=pic.getSelectedIndex()+1; //MyC....这个类的pic...这个属性等于pic.get......这个方法的结果+1
myCanvas.reLoadPictrue(); //myCanvas对象重新读取图片
Icon icon=new ImageIcon("pic/pic_"+MyCanvas.pictureID+".jpg"); //定义新的图标对象
JLabel label=new JLabel(icon); //初始新的标签(标签中加入图标)
panelPreview.removeAll(); //调用pane....对象的remo...方法
panelPreview.add(label); //对象pan...调用add方法
panelPreview.repaint(); //调用....对象重新画的方法
------------------------
不太熟悉java的swt和swing,自己改下吧