
正文
拼图小游戏java源代码 java编写拼图游戏
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java做的拼图游戏怎么实现更换图片哦
编写拼图按钮的监听器类,该类为主类的内部类。
在actionPerformed()方法中,首先获得空白按钮和被单击按钮的所在行和列,
然后判断这两个按钮是否相邻,如果相邻则将被单击按钮显示的图片移动到空白按钮上,并令被单击按钮显示空白图片,以及将在类中声明的空白按钮对象设置为被单击的按钮对象
思路就这样,我给你个示例代码,你参考参考哦:
class ImgButtonAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
String emptyName = emptyButton.getName();
char emptyRow = emptyName.charAt(0);
char emptyCol = emptyName.charAt(1);
JButton clickButton = (JButton) e.getSource();
String clickName =clickButton.getName();
char clickRow = clickName.charAt(0);
char clickCol = clickName.charAt(1);
if(Math.abs(clickRow - emptyRow) + Math.abs(clickCol - emptyCol) == 1) {
emptyButton.setIcon(clickButton.getIcon()) ;
clickButton.setIcon(new ImageIcon("img/00.jpg"));
emptyButton = clickButton;
}
}
}
大概就这样了
希望对你有帮助哈
相关问答
Q1: java!怎么把三行三列的拼图游戏改成四行四列!!跪求高手帮忙修改!!下面是源码!!
拼图小游戏java源代码你拼图小游戏java源代码的程序问题很多啊 首先没有main函数 根本运行不拼图小游戏java源代码了 拼图小游戏java源代码,其次你没有输出,根本不可能打出表格的, 所有的3都改成4就是四行四列
Q2: 找高手帮我翻译这个JAVA拼图游戏代码(希望能帮我解析每句中用到的JAVA技术)
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;
//以上均引用不同的package内的类
public class MyMainFrame extends JFrame implements ActionListener {//MyMainFrame类extends “JFrame”类实现 ActionListener的方法
MyCanvas myCanvas; //初始化对象MyCanvas类
JPanel panelNorth,panelPreview;//定义上方的面板拼图小游戏java源代码,及预览所需的面板
Button start,preview,set;//定义开始,预览,设定按钮
Container container;//容器,得到内容面板
public MyMainFrame() {//初使化
container=this.getContentPane(); //得到当前对象的ContentPane,并且把它赋给container
start=new Button("开始");//创建并初始新的Button(按钮)对象,赋给start
start.addActionListener(this); //在这个按钮对象中添加监听器,范围是当前对象
preview=new Button("预览");//同上,创建新的Button对象。。。。。
preview.addActionListener(this);//同上。。。。。
set = new Button("设置"); //同上。。。。。(感觉代码都差不多吧拼图小游戏java源代码?呵呵)
set.addActionListener(this);//同上
panelPreview=new JPanel(); 创建新的JPanel(面板)对象
panelPreview.setLayout(null); //设置面板对象的布局为空
Icon icon=new ImageIcon("pic/pic_"+MyCanvas.pictureID+".jpg"); //创建并初始新的图标对象。图标的图片路径是pic目录下的pic与通过MyCanvas.pictureId取得字符串再与.jpg合并后的名称。例如(pic/pic_1234.jsp)
JLabel label=new JLabel(icon); //定义新的JLable(java标签),并初始
label.setBounds(0,0,300,300); //设置标签的范围(长x轴,宽y轴,长多,宽多少)
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);//在容器(前边定义好拼图小游戏java源代码了这个对象)中添加myCanvas,设置它的布局为居中
container.add(panelNorth,BorderLayout.NORTH);//添加Jpanel,布局为北(也就是上)
this.setTitle("拼图小游戏-"); //设置这个对象的题目叫。。。。。
this.setLocation(300,200); //设置它的初始位置
this.setSize(308,365); //设置大小
this.setResizable(false); //设置是否可以改变窗口的大小(否)
this.setVisible(true); //是否可以显示(是)
this.setDefaultCloseOperation(3); //(swt和swing本人用的少)这个好像是按钮的初始样式是哪种吧。自己改下
}
public static void main(String[] args) {//类进口,也就是主程序的进口
// TODO 自动生成方法存根
new MyMainFrame(); //实例化主框架
}
public void actionPerformed(ActionEvent arg0) {//对三个按钮事件的处理
// TODO 自动生成方法存根
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); //初始新的标签(标签中加入图标)
label.setBounds(0,0,300,300); //标签设置范围
panelPreview.removeAll(); //调用pane....对象的remo...方法
panelPreview.add(label); //对象pan...调用add方法
panelPreview.repaint(); //调用。。。。对象重新画的方法
}
}
}
}
------------------------
不太熟悉java的swt和swing,自己改下吧
Q3: 求java小游戏源代码
表1. CheckerDrag.java
// CheckerDrag.javaimport java.awt.*;import java.awt.event.*;public class CheckerDrag extends java.applet.Applet{ // Dimension of checkerboard square. // 棋盘上每个小方格的尺寸 final static int SQUAREDIM = 40; // Dimension of checkerboard -- includes black outline. // 棋盘的尺寸 – 包括黑色的轮廓线 final static int BOARDDIM = 8 * SQUAREDIM + 2; // Dimension of checker -- 3/4 the dimension of a square. // 棋子的尺寸 – 方格尺寸的3/4 final static int CHECKERDIM = 3 * SQUAREDIM / 4; // Square colors are dark green or white. // 方格的颜色为深绿色或者白色 final static Color darkGreen = new Color (0, 128, 0); // Dragging flag -- set to true when user presses mouse button over checker // and cleared to false when user releases mouse button. // 拖动标记 --当用户在棋子上按下鼠标按键时设为true, // 释放鼠标按键时设为false boolean inDrag = false; // Left coordinate of checkerboard's upper-left corner. // 棋盘左上角的左方向坐标 int boardx; // Top coordinate of checkerboard's upper-left corner. //棋盘左上角的上方向坐标 int boardy; // Left coordinate of checker rectangle origin (upper-left corner). // 棋子矩形原点(左上角)的左方向坐标 int ox; // Top coordinate of checker rectangle origin (upper-left corner). // 棋子矩形原点(左上角)的上方向坐标 int oy; // Left displacement between mouse coordinates at time of press and checker // rectangle origin. // 在按键时的鼠标坐标与棋子矩形原点之间的左方向位移 int relx; // Top displacement between mouse coordinates at time of press and checker // rectangle origin. // 在按键时的鼠标坐标与棋子矩形原点之间的上方向位移 int rely; // Width of applet drawing area. // applet绘图区域的宽度 int width; // Height of applet drawing area. // applet绘图区域的高度 int height; // Image buffer. // 图像缓冲 Image imBuffer; // Graphics context associated with image buffer. // 图像缓冲相关联的图形背景 Graphics imG; public void init () { // Obtain the size of the applet's drawing area. // 获取applet绘图区域的尺寸 width = getSize ().width; height = getSize ().height; // Create image buffer. // 创建图像缓冲 imBuffer = createImage (width, height); // Retrieve graphics context associated with image buffer. // 取出图像缓冲相关联的图形背景 imG = imBuffer.getGraphics (); // Initialize checkerboard's origin, so that board is centered. // 初始化棋盘的原点,使棋盘在屏幕上居中 boardx = (width - BOARDDIM) / 2 + 1; boardy = (height - BOARDDIM) / 2 + 1; // Initialize checker's rectangle's starting origin so that checker is // centered in the square located in the top row and second column from // the left. // 初始化棋子矩形的起始原点,使得棋子在第一行左数第二列的方格里居中 ox = boardx + SQUAREDIM + (SQUAREDIM - CHECKERDIM) / 2 + 1; oy = boardy + (SQUAREDIM - CHECKERDIM) / 2 + 1; // Attach a mouse listener to the applet. That listener listens for // mouse-button press and mouse-button release events. // 向applet添加一个用来监听鼠标按键的按下和释放事件的鼠标监听器 addMouseListener (new MouseAdapter () { public void mousePressed (MouseEvent e) { // Obtain mouse coordinates at time of press. // 获取按键时的鼠标坐标 int x = e.getX (); int y = e.getY (); // If mouse is over draggable checker at time // of press (i.e., contains (x, y) returns // true), save distance between current mouse // coordinates and draggable checker origin // (which will always be positive) and set drag // flag to true (to indicate drag in progress). // 在按键时如果鼠标位于可拖动的棋子上方 // (也就是contains (x, y)返回true),则保存当前 // 鼠标坐标与棋子的原点之间的距离(始终为正值)并且 // 将拖动标志设为true(用来表明正处在拖动过程中) if (contains (x, y)) { relx = x - ox; rely = y - oy; inDrag = true; } } boolean contains (int x, int y) { // Calculate center of draggable checker. // 计算棋子的中心位置 int cox = ox + CHECKERDIM / 2; int coy = oy + CHECKERDIM / 2; // Return true if (x, y) locates with bounds // of draggable checker. CHECKERDIM / 2 is the // radius. // 如果(x, y)仍处于棋子范围内则返回true // CHECKERDIM / 2为半径 return (cox - x) * (cox - x) + (coy - y) * (coy - y) CHECKERDIM / 2 * CHECKERDIM / 2; } public void mouseReleased (MouseEvent e) { // When mouse is released, clear inDrag (to // indicate no drag in progress) if inDrag is // already set. // 当鼠标按键被释放时,如果inDrag已经为true, // 则将其置为false(用来表明不在拖动过程中) if (inDrag) inDrag = false; } }); // Attach a mouse motion listener to the applet. That listener listens // for mouse drag events. //向applet添加一个用来监听鼠标拖动事件的鼠标运动监听器 addMouseMotionListener (new MouseMotionAdapter () { public void mouseDragged (MouseEvent e) { if (inDrag) { // Calculate draggable checker's new // origin (the upper-left corner of // the checker rectangle). // 计算棋子新的原点(棋子矩形的左上角) int tmpox = e.getX () - relx; int tmpoy = e.getY () - rely; // If the checker is not being moved // (at least partly) off board, // assign the previously calculated // origin (tmpox, tmpoy) as the // permanent origin (ox, oy), and // redraw the display area (with the // draggable checker at the new // coordinates). // 如果棋子(至少是棋子的一部分)没有被 // 移出棋盘,则将之前计算的原点 // (tmpox, tmpoy)赋值给永久性的原点(ox, oy), // 并且刷新显示区域(此时的棋子已经位于新坐标上) if (tmpox boardx tmpoy boardy tmpox + CHECKERDIM boardx + BOARDDIM tmpoy + CHECKERDIM boardy + BOARDDIM) { ox = tmpox; oy = tmpoy; repaint (); } } } }); } public void paint (Graphics g) { // Paint the checkerboard over which the checker will be dragged. // 在棋子将要被拖动的位置上绘制棋盘 paintCheckerBoard (imG, boardx, boardy); // Paint the checker that will be dragged. // 绘制即将被拖动的棋子 paintChecker (imG, ox, oy); // Draw contents of image buffer. // 绘制图像缓冲的内容 g.drawImage (imBuffer, 0, 0, this); } void paintChecker (Graphics g, int x, int y) { // Set checker shadow color. // 设置棋子阴影的颜色 g.setColor (Color.black); // Paint checker shadow. // 绘制棋子的阴影 g.fillOval (x, y, CHECKERDIM, CHECKERDIM); // Set checker color. // 设置棋子颜色 g.setColor (Color.red); // Paint checker. // 绘制棋子 g.fillOval (x, y, CHECKERDIM - CHECKERDIM / 13, CHECKERDIM - CHECKERDIM / 13); } void paintCheckerBoard (Graphics g, int x, int y) { // Paint checkerboard outline. // 绘制棋盘轮廓线 g.setColor (Color.black); g.drawRect (x, y, 8 * SQUAREDIM + 1, 8 * SQUAREDIM + 1); // Paint checkerboard. // 绘制棋盘 for (int row = 0; row 8; row++) { g.setColor (((row 1) != 0) ? darkGreen : Color.white); for (int col = 0; col 8; col++) { g.fillRect (x + 1 + col * SQUAREDIM, y + 1 + row * SQUAREDIM, SQUAREDIM, SQUAREDIM); g.setColor ((g.getColor () == darkGreen) ? Color.white : darkGreen); } } } // The AWT invokes the update() method in response to the repaint() method // calls that are made as a checker is dragged. The default implementation // of this method, which is inherited from the Container class, clears the // applet's drawing area to the background color prior to calling paint(). // This clearing followed by drawing causes flicker. CheckerDrag overrides // update() to prevent the background from being cleared, which eliminates // the flicker. // AWT调用了update()方法来响应拖动棋子时所调用的repaint()方法。该方法从 // Container类继承的默认实现会在调用paint()之前,将applet的绘图区域清除 // 为背景色,这种绘制之后的清除就导致了闪烁。CheckerDrag重写了update()来 // 防止背景被清除,从而消除了闪烁。 public void update (Graphics g) { paint (g); }}
Q4: 急求用JAVA编写的图形化界面拼图小游戏代码!
个人见解,总体需要两个二维数组(一个存储正确图片排列 Array1 String[][],一个随机生成图片排列Array2 String[][]),一个一维数组来存储图片的名称Array3 String[],。
(1)如何实现图片移动
使用带图片的按钮(button =new button(getImage(Array[2][4]))),然后通过单击事件来更改按钮的图片来源。 把被点击的按钮的图片路径更新到空白按钮,并且把被点击的按钮图片更新的成空白。其实就是变换两个的二维数组成员的值。更新Array2中的值,然后重绘按钮
如 Array[2][3]=“3.image”
Array[2][4]=“”
图片3.image右移
Array[2][3]=“”
Array[2][4]=“3.image”
(2)如何判断被单击的网格与空白的网格是否相邻
后台使用一个二维数组Array2来做映射。通过二维数组的下标来判断,如Array[2][3]可以知道Array[2][4]是它右边的那个。
(3)如何实现图片的随机摆放
比如有9个图片,你可以命名1-9,然后初始化一个长度为9的一维String 数组Array3来存储图片的名称,
使用随机函数给二维数组Array2赋值,如Array2[2][3]=Array3[random()],这里要判断这个图片是否已被使用过,可以通过遍历Array2来确定当前Array3这个值是否已经在Array2中了
最后通过Array1 和Array2来比较,用户的拼图是否正确。
语言组织能力有限。讲不太清楚。
Q5: 1200分跪求JAVA数字拼图游戏源代码!
1:
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);
label.setBounds(0,0,400,400);
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.setTitle("成型拼图小游戏-1212");
this.setLocation(300,200);
this.setSize(408,465);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(3);
} //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);
container.repaint();
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.pictureID=pic.getSelectedIndex()+5;
myCanvas.reLoadPictrue();
Icon icon=new ImageIcon("images/pic_"+MyCanvas.pictureID+".jpg");
JLabel label=new JLabel(icon);
label.setBounds(0,0,400,400);
panelPreview.removeAll();
panelPreview.add(label);
panelPreview.repaint();
}
}
}
public static void main(String[] args) throws UnsupportedAudioFileException, LineUnavailableException, IOException
{
new MyMainFrame();
} //end of main
}
2:
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MyCanvas extends JPanel implements MouseListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
boolean hasAddActionListener=false;//设置方格的动作监听器的标志位,TRUE为已经添加上动作事件
Cell cell[];//定义方格
Rectangle cellNull;//定义空方格区域 是一个矩形类
public static int pictureID=4;// 当前选择的图片代号
public MyCanvas() {
this.setLayout(null);
this.setSize(400,400);
cellNull=new Rectangle(300,300,100,100);//空方格区域在第三行每三列
cell=new Cell[16];
Icon icon;
for (int i = 0; i 4; i++) {
for(int j=0;j4;j++){
icon=new ImageIcon("images/pic_"+pictureID+"_"+(i*4+j+1)+".jpg");
cell[i*4+j]=new Cell(icon);
cell[i*4+j].setLocation(j*100,i*100);
this.add(cell[i*4+j]);
}
}
this.remove(cell[15]);//移除最后一个多余的方格
} //放置9张小图片并且移调最后一张
public void reLoadPictrue(){//当选择其它图形进行拼图时,需重新加载新图片
Icon icon;
for (int i = 0; i 4; i++) {
for(int j=0;j4;j++){
icon=new ImageIcon("images/pic_"+pictureID+"_"+(i*4+j+1)+".jpg");
cell[i*4+j].setIcon(icon);
}
}
}
public boolean isFinish(){//判断是否拼合成功
for(int i=0;i15;i++)
{ int x=cell[i].getBounds().x;
int y=cell[i].getBounds().y;
if(y/100*4+x/100!=i)
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;
int direction=(int)(Math.random()*4);//产生0-4,对应空方格的上下左右移动
if(direction==0){//空方格左移动,与左侧方格互换位置,左侧方格右移动
x-=100;
if(test(x,y)){
for(int j=0;j15;j++){
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(test(x,y)){
for(int j=0;j15;j++){
if((cell[j].getBounds().x==x)(cell[j].getBounds().y==y)){
cell[j].move("LEFT",100);
cellNull.setLocation(x,y);
break;
}
}
}
}else if(direction==2){//UP
y-=100;
if(test(x,y)){
for(int j=0;j15;j++){
if((cell[j].getBounds().x==x)(cell[j].getBounds().y==y)){
cell[j].move("DOWN",100);
cellNull.setLocation(x,y);
break;
}
}
}
}else{//DOWN
y+=100;
if(test(x,y)){
for(int j=0;j15;j++){
if((cell[j].getBounds().x==x)(cell[j].getBounds().y==y)){
cell[j].move("UP",100);
cellNull.setLocation(x,y);
break;
}
}
}
}
}
if(!hasAddActionListener)//如果尚未添加动作事件,则添加
for(int i=0;i15;i++)//为第个方格添加动作事件,这样单击按钮就能移动了
cell[i].addMouseListener(this);
hasAddActionListener=true;
}
private boolean test(int x,int y){
if((x=0x=200)||(y=0y=200))
return true;
else
return false;
}
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;
int x2=cellNull.getBounds().x;//得到空方格的坐标
int y2=cellNull.getBounds().y;
if(x1==x2y1-y2==100)//进行比较,如果满足条件则进行交换
button.move("UP",100);
else if(x1==x2y1-y2==-100)
button.move("DOWN",100);
else if(x1-x2==100y1==y2)
button.move("LEFT",100);
else if(x1-x2==-100y1==y2)
button.move("RIGHT",100);
else
return;//不满足就不进行任何处理
cellNull.setLocation(x1,y1);
this.repaint();
if(this.isFinish()){//进行是否完成的判断
JOptionPane.showMessageDialog(this,"景锋恭喜你完成拼图,加油!想继续下一关么?");
for(int i=0;i15;i++)
cell[i].removeMouseListener(this);//如果已完成,撤消鼠标事件,鼠标单击方格不在起作用
hasAddActionListener=false;
}
}
}
3:
import javax.swing.Icon;
import javax.swing.JButton;
public class Cell extends JButton {
/**
*
*/
private static final long serialVersionUID = 1L;
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);
}else{
this.setLocation(this.getBounds().x+100,this.getBounds().y);
}
}
}
关于拼图小游戏java源代码和java编写拼图游戏的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







