目录

  • 1 继承
    • 1.1 继承概述
    • 1.2 继承的练习
  • 2:动漫美女拼图
    • 2.1 项目演示
    • 2.2 动漫美女拼图游戏实现
      • 2.2.1 窗体绘制
      • 2.2.2 窗体上组件绘制
      • 2.2.3 图片打乱
      • 2.2.4 纪录0号图片的索引
      • 2.2.5 给按钮注册事件
      • 2.2.6 移动业务实现
        • 2.2.6.1 上移业务实现
        • 2.2.6.2 其他移动业务实现
      • 2.2.7 求助业务实现
      • 2.2.8 移动业务的问题
      • 2.2.9 重置业务实现

1 继承

1.1 继承概述

  • 继承:

    • 继承是面向对象三大特征之一(封装,继承和多态)
    • 可以使得子类具有父类的属性和方法,还可以在子类中重新定义,追加属性和方法
      • 也就是说,通过继承,可以把父类中能够被访问到的成员变量和成员方法拿过来直接使用。
  • 继承的格式:
    • 格式:public class 子类名 extends 父类名 { }
    • 范例:public class Zi extends Fu { }
    • Fu:是父类,也被称为基类、超类
    • Zi:是子类,也被称为派生类
      • 在这里,Zi类和Fu类,通过extends就产生了继承关系。这样呢,Zi类就可以使用Fu类中的成员了
      • 在这里,Fu这个类,被称为是父类,也被称为基类、超类,Zi这个类:是子类,也被称为派生类
  • 继承的好处之一是:
    • 提高了代码的复用性

1.2 继承的练习

需求:
使用继承的方式,改写用户登录界面展示的案例

import javax.swing.*;public class UserLoginFrame extends JFrame {public UserLoginFrame() {//窗体初始化initFrame();//绘制窗体paintView();//显示窗体this.setVisible(true);}public void paintView() {//显示用户名文本JLabel usernameLable = new JLabel("用户名");usernameLable.setBounds(50, 50, 50, 20);this.add(usernameLable);//用户名输入框JTextField usernameField = new JTextField();usernameField.setBounds(150, 50, 180, 20);this.add(usernameField);//显示密码文本JLabel passwordLable = new JLabel("密码");passwordLable.setBounds(50, 100, 50, 20);this.add(passwordLable);//密码输入框JPasswordField passwordField = new JPasswordField();passwordField.setBounds(150, 100, 180, 20);this.add(passwordField);//登录按钮JButton loginButton = new JButton("登录");loginButton.setBounds(50, 200, 280, 20);this.add(loginButton);}public void initFrame() {this.setTitle("用户登录");this.setSize(400, 300);this.setDefaultCloseOperation(3);this.setLocationRelativeTo(null);this.setAlwaysOnTop(true);this.setLayout(null);}
}
public class App {public static void main(String[] args) {UserLoginFrame userLoginFrame = new UserLoginFrame();}
}

总结:
用继承改进后,代码看起来清晰多了,所以,如果我们做GUI开发的,在做窗体的时候,就会定义类继承JFrame来使用。

2:动漫美女拼图

2.1 项目演示


一个是动漫拼图窗体的类,一个是测试类。
右键运行测试类,我们看到了这样界面。

可以通过按钮来玩这个游戏。还可以点击求助按钮和重置按钮。
实现步骤:

  1. 我们绘制游戏界面,把界面中要使用到的组件都给展示出来。
  2. 我们讲解图片打乱,这样动漫拼图的界面就启动了。
  3. 我们给按钮注册事件,这样我们就知道在哪里实现我们的功能了。
  4. 我们来分别完成,移动业务,求助业务,重置业务,把整个项目给实现了

2.2 动漫美女拼图游戏实现

2.2.1 窗体绘制


分析思路:

  • 新建一个模块:itheima-picture-puzzle;在模块的src下新建一个包com.itheima
  • 在com.itheima这个包下定义类:PictureFrame,继承自JFrame
  • 在PictureFrame类中编写无参构造方法,在构造方法中调用两个方法:
    • 第一个方法:initFrame(),用于窗体的基本设置
    • 第二个方法:setVisible(true),用于设置窗体可见
  • 在initFrame()方法中编写代码,进行窗体的基本设置
    • 窗体大小
    • 窗体标题
    • 窗体居中
    • 窗体关闭时退出应用程序
    • 窗体位于其他窗口之上
    • 取消窗体默认布局
  • 在com.itheima包下定义测试类:App;创建PictureFrame的对象进行测试
import javax.swing.*;public class PictureFrame extends JFrame {public PictureFrame() {//用于窗体的基本设置initFrame();//设置窗体可见this.setVisible(true);}//用于窗体的基本设置public void initFrame() {//窗体大小this.setSize(960, 565);//窗体标题this.setTitle("动漫拼图");//窗体居中this.setLocationRelativeTo(null);//窗体关闭时退出应用程序this.setDefaultCloseOperation(3);//窗体位于其他窗口之上this.setAlwaysOnTop(true);//取消窗体默认布局this.setLayout(null);}
}
public class App {public static void main(String[] args) {PictureFrame pf = new PictureFrame();}
}

2.2.2 窗体上组件绘制


分析思路:

  • 定义方法,用于窗体上的组件绘制:paintView()
  • 按照如下组件绘制
    • 标题图片
    • 面板图片,存储着将来要移动的图片
    • 参照图
    • 上按钮
    • 左按钮
    • 下按钮
    • 右按钮
    • 求助按钮
    • 重置按钮
  • 在构造方法中调用paintView()方法
import javax.swing.*;public class PictureFrame extends JFrame {//构造方法方法中调用绘图方法public PictureFrame() {//用于窗体的基本设置initFrame();//窗体上组件的绘制paintView();//设置窗体可见this.setVisible(true);}//窗体上组件的绘制public void paintView() {//标题图片JLabel titleLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\title.png"));titleLabel.setBounds(354, 27, 232, 57);this.add(titleLabel);//定义一个二维数组,用来存储图片的编号int[][] datas = {{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 16}};//创建面板JPanel imagePanel = new JPanel();imagePanel.setBounds(150, 114, 360, 360);imagePanel.setLayout(null);//遍历二维数组,得到图片编号for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {//创建JLabel对象,加载图片资源JLabel imageLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\" + datas[i][j] + ".png"));//调整图片的位置imageLabel.setBounds(j * 90, i * 90, 90, 90);imagePanel.add(imageLabel);}}//把面板添加到窗体上this.add(imagePanel);//动漫参照图JLabel canZhaoTuLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\canzhaotu.png"));canZhaoTuLabel.setBounds(574,114,122,121);this.add(canZhaoTuLabel);//上下左右,求助,重置按钮JButton shangButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\shang.png"));shangButton.setBounds(732,265,57,57);this.add(shangButton);JButton zuoButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\zuo.png"));zuoButton.setBounds(650,347,57,57);this.add(zuoButton);JButton xiaButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\xia.png"));xiaButton.setBounds(732,347,57,57);this.add(xiaButton);JButton youButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\you.png"));youButton.setBounds(813,347,57,57);this.add(youButton);JButton qiuZhuButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\qiuzhu.png"));qiuZhuButton.setBounds(626,444,108,45);this.add(qiuZhuButton);JButton chongZhiButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\chongzhi.png"));chongZhiButton.setBounds(786,444,108,45);this.add(chongZhiButton);//展示背景图JLabel backgroundLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\background.png"));backgroundLabel.setBounds(0,0,960,530);this.add(backgroundLabel);}//用于窗体的基本设置public void initFrame() {//窗体大小this.setSize(960, 565);//窗体标题this.setTitle("动漫拼图");//窗体居中this.setLocationRelativeTo(null);//窗体关闭时退出应用程序this.setDefaultCloseOperation(3);//窗体位于其他窗口之上this.setAlwaysOnTop(true);//取消窗体默认布局this.setLayout(null);}
}

2.2.3 图片打乱


注意事项:

  • 由于在多个方法中使用同一个数组,故将二维数组的定义放置在成员位置
  • 为了能够进行图片的移动,把16号图片用0号图片替换,当前15个移动到正确位置,显示正确的图片

分析思路:

  • 定义方法,用于二维数组元素打乱:initData()
  • 创建Random对象
  • 遍历存储图片编号的二维数组,得到每一个元素
  • 产生两个随机索引,进行二维数组元素交换
    • int x = r.nextInt(datas.length);//行索引
    • int y = r.nextInt(datas[x].length);//列索引
    • //元素交换
    • int temp = datas[i][j];
    • datas[i][j] = datas[x][y];
    • datas[x][y] = temp;
  • 在构造方法中调用initData()方法
import javax.swing.*;
import java.util.Random;public class PictureFrame extends JFrame {//定义一个二维数组,用来存储图片的编号int[][] datas = {{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 0}};//构造方法方法中调用绘图方法public PictureFrame() {//用于窗体的基本设置initFrame();//二维数组元素打乱randomData();//窗体上组件的绘制paintView();//设置窗体可见this.setVisible(true);}//二维数组元素打乱public void randomData() {Random r = new Random();for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {int x = r.nextInt(datas.length);int y = r.nextInt(datas[i].length);int temp = datas[i][j];datas[i][j] = datas[x][y];datas[x][y] = temp;}}}//窗体上组件的绘制public void paintView() {//标题图片JLabel titleLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\title.png"));titleLabel.setBounds(354, 27, 232, 57);this.add(titleLabel);
//        //定义一个二维数组,用来存储图片的编号
//        int[][] datas = {//                {1, 2, 3, 4},
//                {5, 6, 7, 8},
//                {9, 10, 11, 12},
//                {13, 14, 15, 16}
//        };//创建面板JPanel imagePanel = new JPanel();imagePanel.setBounds(150, 114, 360, 360);imagePanel.setLayout(null);//遍历二维数组,得到图片编号for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {//创建JLabel对象,加载图片资源JLabel imageLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\" + datas[i][j] + ".png"));//调整图片的位置imageLabel.setBounds(j * 90, i * 90, 90, 90);imagePanel.add(imageLabel);}}//把面板添加到窗体上this.add(imagePanel);//动漫参照图JLabel canZhaoTuLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\canzhaotu.png"));canZhaoTuLabel.setBounds(574, 114, 122, 121);this.add(canZhaoTuLabel);//上下左右,求助,重置按钮JButton shangButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\shang.png"));shangButton.setBounds(732, 265, 57, 57);this.add(shangButton);JButton zuoButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\zuo.png"));zuoButton.setBounds(650, 347, 57, 57);this.add(zuoButton);JButton xiaButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\xia.png"));xiaButton.setBounds(732, 347, 57, 57);this.add(xiaButton);JButton youButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\you.png"));youButton.setBounds(813, 347, 57, 57);this.add(youButton);JButton qiuZhuButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\qiuzhu.png"));qiuZhuButton.setBounds(626, 444, 108, 45);this.add(qiuZhuButton);JButton chongZhiButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\chongzhi.png"));chongZhiButton.setBounds(786, 444, 108, 45);this.add(chongZhiButton);//展示背景图JLabel backgroundLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\background.png"));backgroundLabel.setBounds(0, 0, 960, 530);this.add(backgroundLabel);}//用于窗体的基本设置public void initFrame() {//窗体大小this.setSize(960, 565);//窗体标题this.setTitle("动漫拼图");//窗体居中this.setLocationRelativeTo(null);//窗体关闭时退出应用程序this.setDefaultCloseOperation(3);//窗体位于其他窗口之上this.setAlwaysOnTop(true);//取消窗体默认布局this.setLayout(null);}
}

2.2.4 纪录0号图片的索引

  • 为什么要纪录0号图片索引呢?

    • 由于将来要进行图片的移动,以实现动漫拼图的实现
    • 而移动的操作,得有一个空白的区别,这里我们采用0号图片表示,需要纪录0号图片的位置,也就是在数组中的索引

分析思路:

  • 在PictureFrame类中定义两个成员变量用于纪录0号图片的索引

    • private int x0;
    • private int y0;
  • 在initData()方法中继续编写代码,在打乱后的数组中找到0号图片的位置
    • 遍历二维数组,得到每一个元素
    • 如果元素为0,则纪录该元素的索引,并结束循环。
import javax.swing.*;
import java.util.Random;public class PictureFrame extends JFrame {//定义一个二维数组,用来存储图片的编号int[][] datas = {{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 0}};//定义两个int类型的变量,用于纪录0号图片的位置private int x0;private int y0;//构造方法方法中调用绘图方法public PictureFrame() {//用于窗体的基本设置initFrame();//二维数组元素打乱randomData();//窗体上组件的绘制paintView();//设置窗体可见this.setVisible(true);}//二维数组元素打乱public void randomData() {Random r = new Random();for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {int x = r.nextInt(datas.length);int y = r.nextInt(datas[i].length);int temp = datas[i][j];datas[i][j] = datas[x][y];datas[x][y] = temp;}}//记录0号图片位置wc:for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {if (datas[i][j] == 0) {x0 = i;y0 = j;break wc;}}}}//窗体上组件的绘制public void paintView() {//标题图片JLabel titleLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\title.png"));titleLabel.setBounds(354, 27, 232, 57);this.add(titleLabel);//创建面板JPanel imagePanel = new JPanel();imagePanel.setBounds(150, 114, 360, 360);imagePanel.setLayout(null);//遍历二维数组,得到图片编号for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {//创建JLabel对象,加载图片资源JLabel imageLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\" + datas[i][j] + ".png"));//调整图片的位置imageLabel.setBounds(j * 90, i * 90, 90, 90);imagePanel.add(imageLabel);}}//把面板添加到窗体上this.add(imagePanel);//动漫参照图JLabel canZhaoTuLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\canzhaotu.png"));canZhaoTuLabel.setBounds(574, 114, 122, 121);this.add(canZhaoTuLabel);//上下左右,求助,重置按钮JButton shangButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\shang.png"));shangButton.setBounds(732, 265, 57, 57);this.add(shangButton);JButton zuoButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\zuo.png"));zuoButton.setBounds(650, 347, 57, 57);this.add(zuoButton);JButton xiaButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\xia.png"));xiaButton.setBounds(732, 347, 57, 57);this.add(xiaButton);JButton youButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\you.png"));youButton.setBounds(813, 347, 57, 57);this.add(youButton);JButton qiuZhuButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\qiuzhu.png"));qiuZhuButton.setBounds(626, 444, 108, 45);this.add(qiuZhuButton);JButton chongZhiButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\chongzhi.png"));chongZhiButton.setBounds(786, 444, 108, 45);this.add(chongZhiButton);//展示背景图JLabel backgroundLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\background.png"));backgroundLabel.setBounds(0, 0, 960, 530);this.add(backgroundLabel);}//用于窗体的基本设置public void initFrame() {//窗体大小this.setSize(960, 565);//窗体标题this.setTitle("动漫拼图");//窗体居中this.setLocationRelativeTo(null);//窗体关闭时退出应用程序this.setDefaultCloseOperation(3);//窗体位于其他窗口之上this.setAlwaysOnTop(true);//取消窗体默认布局this.setLayout(null);}
}

新知识:嵌套循环的退出。给循环起名字就可以实现退出指定的循环。

2.2.5 给按钮注册事件

注意事项:
由于在多个方法中使用同一个按钮,故将按钮的定义放置在成员位置

分析思路:

  • 定义方法,用于给按钮添加事件:addButtonEvent()
  • 在addButtonEvent()方法中给每一个按钮添加事件,并给出输出语句提示
  • 在构造方法中调用addButtonEvent()方法
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;public class PictureFrame extends JFrame {//定义一个二维数组,用来存储图片的编号int[][] datas = {{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 0}};//定义两个int类型的变量,用于纪录0号图片的位置private int x0;private int y0;//定义上左下右,求助,重置按钮private JButton shangButton;private JButton zuoButton;private JButton xiaButton;private JButton youButton;private JButton qiuZhuButton;private JButton chongZhiButton;// 构造方法方法中调用绘图方法public PictureFrame() {//用于窗体的基本设置initFrame();//二维数组元素打乱randomData();//窗体上组件的绘制paintView();//给按钮添加事件addButtonEvent();//设置窗体可见this.setVisible(true);}//给按钮添加事件public void addButtonEvent() {shangButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("上");}});zuoButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("左");}});xiaButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("下");}});youButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("右");}});qiuZhuButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("求助");}});chongZhiButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("重置");}});}//二维数组元素打乱public void randomData() {Random r = new Random();for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {int x = r.nextInt(datas.length);int y = r.nextInt(datas[i].length);int temp = datas[i][j];datas[i][j] = datas[x][y];datas[x][y] = temp;}}//记录0号图片位置wc:for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {if (datas[i][j] == 0) {x0 = i;y0 = j;break wc;}}}}//窗体上组件的绘制public void paintView() {//标题图片JLabel titleLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\title.png"));titleLabel.setBounds(354, 27, 232, 57);this.add(titleLabel);//创建面板JPanel imagePanel = new JPanel();imagePanel.setBounds(150, 114, 360, 360);imagePanel.setLayout(null);//遍历二维数组,得到图片编号for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {//创建JLabel对象,加载图片资源JLabel imageLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\" + datas[i][j] + ".png"));//调整图片的位置imageLabel.setBounds(j * 90, i * 90, 90, 90);imagePanel.add(imageLabel);}}//把面板添加到窗体上this.add(imagePanel);//动漫参照图JLabel canZhaoTuLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\canzhaotu.png"));canZhaoTuLabel.setBounds(574, 114, 122, 121);this.add(canZhaoTuLabel);//上下左右,求助,重置按钮shangButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\shang.png"));shangButton.setBounds(732, 265, 57, 57);this.add(shangButton);zuoButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\zuo.png"));zuoButton.setBounds(650, 347, 57, 57);this.add(zuoButton);xiaButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\xia.png"));xiaButton.setBounds(732, 347, 57, 57);this.add(xiaButton);youButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\you.png"));youButton.setBounds(813, 347, 57, 57);this.add(youButton);qiuZhuButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\qiuzhu.png"));qiuZhuButton.setBounds(626, 444, 108, 45);this.add(qiuZhuButton);chongZhiButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\chongzhi.png"));chongZhiButton.setBounds(786, 444, 108, 45);this.add(chongZhiButton);//展示背景图JLabel backgroundLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\background.png"));backgroundLabel.setBounds(0, 0, 960, 530);this.add(backgroundLabel);}//用于窗体的基本设置public void initFrame() {//窗体大小this.setSize(960, 565);//窗体标题this.setTitle("动漫拼图");//窗体居中this.setLocationRelativeTo(null);//窗体关闭时退出应用程序this.setDefaultCloseOperation(3);//窗体位于其他窗口之上this.setAlwaysOnTop(true);//取消窗体默认布局this.setLayout(null);}
}

2.2.6 移动业务实现

移动原理: 图片的移动其实就是元素的交换,也就是二维数组的元素改变了,然后进行图片重绘就可以了

2.2.6.1 上移业务实现

分析思路:

  • 移动规则:竖的是x,横的是y

    • 空白图片,和下方元素(x0+1),进行交换
  • 把空白图片和下方图片的位置交换
    • datas[x0] [y0] = datas[x0 + 1] [y0];
    • datas[x0 + 1] [y0] = 0;
    • x0 = x0 + 1;
  • 编写重绘方法:rePaintView()
    • 先移除,再重新绘制
  • 调用重绘方法
  • 边界问题处理:当x0=3,不能进行上移动
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;public class PictureFrame extends JFrame {//定义一个二维数组,用来存储图片的编号int[][] datas = {{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 0}};//定义两个int类型的变量,用于纪录0号图片的位置private int x0;private int y0;//定义上左下右,求助,重置按钮private JButton shangButton;private JButton zuoButton;private JButton xiaButton;private JButton youButton;private JButton qiuZhuButton;private JButton chongZhiButton;//定义面板private JPanel imagePanel;// 构造方法方法中调用绘图方法public PictureFrame() {//用于窗体的基本设置initFrame();//二维数组元素打乱randomData();//窗体上组件的绘制paintView();//给按钮添加事件addButtonEvent();//设置窗体可见this.setVisible(true);}//移动的图形绘制//移动的图形重新绘制public void rePaintView() {//移除所有imagePanel.removeAll();//遍历二维数组,得到每一个图片编号for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {//在遍历的过程中,创建 JLabel 对象,加载图片资源JLabel imageLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\" + datas[i][j] + ".png"));//调整图片资源的摆放位置imageLabel.setBounds(j * 90, i * 90, 90, 90);imagePanel.add(imageLabel);}}//重新绘制窗体imagePanel.repaint();}//给按钮添加事件public void addButtonEvent() {shangButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {//                System.out.println("上");//边界处理if (x0 == 3) {return;}//位置交换datas[x0][y0] = datas[x0 + 1][y0];datas[x0 + 1][y0] = 0;x0 = x0 + 1;//重绘方法调用rePaintView();}});zuoButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("左");}});xiaButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("下");}});youButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("右");}});qiuZhuButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("求助");}});chongZhiButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("重置");}});}//二维数组元素打乱public void randomData() {Random r = new Random();for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {int x = r.nextInt(datas.length);int y = r.nextInt(datas[i].length);int temp = datas[i][j];datas[i][j] = datas[x][y];datas[x][y] = temp;}}//记录0号图片位置wc:for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {if (datas[i][j] == 0) {x0 = i;y0 = j;break wc;}}}}//窗体上组件的绘制public void paintView() {//标题图片JLabel titleLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\title.png"));titleLabel.setBounds(354, 27, 232, 57);this.add(titleLabel);//创建面板imagePanel = new JPanel();imagePanel.setBounds(150, 114, 360, 360);imagePanel.setLayout(null);//遍历二维数组,得到图片编号for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {//创建JLabel对象,加载图片资源JLabel imageLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\" + datas[i][j] + ".png"));//调整图片的位置imageLabel.setBounds(j * 90, i * 90, 90, 90);imagePanel.add(imageLabel);}}//把面板添加到窗体上this.add(imagePanel);//动漫参照图JLabel canZhaoTuLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\canzhaotu.png"));canZhaoTuLabel.setBounds(574, 114, 122, 121);this.add(canZhaoTuLabel);//上下左右,求助,重置按钮shangButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\shang.png"));shangButton.setBounds(732, 265, 57, 57);this.add(shangButton);zuoButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\zuo.png"));zuoButton.setBounds(650, 347, 57, 57);this.add(zuoButton);xiaButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\xia.png"));xiaButton.setBounds(732, 347, 57, 57);this.add(xiaButton);youButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\you.png"));youButton.setBounds(813, 347, 57, 57);this.add(youButton);qiuZhuButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\qiuzhu.png"));qiuZhuButton.setBounds(626, 444, 108, 45);this.add(qiuZhuButton);chongZhiButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\chongzhi.png"));chongZhiButton.setBounds(786, 444, 108, 45);this.add(chongZhiButton);//展示背景图JLabel backgroundLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\background.png"));backgroundLabel.setBounds(0, 0, 960, 530);this.add(backgroundLabel);}//用于窗体的基本设置public void initFrame() {//窗体大小this.setSize(960, 565);//窗体标题this.setTitle("动漫拼图");//窗体居中this.setLocationRelativeTo(null);//窗体关闭时退出应用程序this.setDefaultCloseOperation(3);//窗体位于其他窗口之上this.setAlwaysOnTop(true);//取消窗体默认布局this.setLayout(null);}
}

2.2.6.2 其他移动业务实现

分析思路:

  • 左移动

    • 边界:y0=3
    • 移动代码:
      • datas[x0][y0] = datas[x0][y0 + 1];
      • datas[x0][y0 + 1] = 0;
      • y0 = y0 + 1;
  • 下移动
    • 边界:x0=0
    • 移动代码:
      • datas[x0][y0] = datas[x0 - 1][y0];
      • datas[x0 - 1][y0] = 0;
      • x0 = x0 - 1;
  • 右移动
    • 边界:y0=0
    • 移动代码:
      • datas[x0][y0] = datas[x0][y0 - 1];
      • datas[x0][y0 - 1] = 0;
      • y0 = y0 - 1;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;public class PictureFrame extends JFrame {//定义一个二维数组,用来存储图片的编号int[][] datas = {{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 0}};//定义两个int类型的变量,用于纪录0号图片的位置private int x0;private int y0;//定义上左下右,求助,重置按钮private JButton shangButton;private JButton zuoButton;private JButton xiaButton;private JButton youButton;private JButton qiuZhuButton;private JButton chongZhiButton;//定义面板private JPanel imagePanel;// 构造方法方法中调用绘图方法public PictureFrame() {//用于窗体的基本设置initFrame();//二维数组元素打乱randomData();//窗体上组件的绘制paintView();//给按钮添加事件addButtonEvent();//设置窗体可见this.setVisible(true);}//移动的图形绘制//移动的图形重新绘制public void rePaintView() {//移除所有imagePanel.removeAll();//遍历二维数组,得到每一个图片编号for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {//在遍历的过程中,创建 JLabel 对象,加载图片资源JLabel imageLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\" + datas[i][j] + ".png"));//调整图片资源的摆放位置imageLabel.setBounds(j * 90, i * 90, 90, 90);imagePanel.add(imageLabel);}}//重新绘制窗体imagePanel.repaint();}//给按钮添加事件public void addButtonEvent() {shangButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {//                System.out.println("上");//边界处理if (x0 == 3) {return;}//位置交换datas[x0][y0] = datas[x0 + 1][y0];datas[x0 + 1][y0] = 0;x0 = x0 + 1;//重绘方法调用rePaintView();}});zuoButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if (y0 == 3) {return;}datas[x0][y0] = datas[x0][y0 + 1];datas[x0][y0 + 1] = 0;y0 = y0 + 1;rePaintView();}});xiaButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if (x0 == 0) {return;}datas[x0][y0] = datas[x0 - 1][y0];datas[x0 - 1][y0] = 0;x0 = x0 - 1;rePaintView();}});youButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if (y0 == 0) {return;}datas[x0][y0] = datas[x0][y0 - 1];datas[x0][y0 - 1] = 0;y0 = y0 - 1;rePaintView();}});qiuZhuButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("求助");}});chongZhiButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("重置");}});}//二维数组元素打乱public void randomData() {Random r = new Random();for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {int x = r.nextInt(datas.length);int y = r.nextInt(datas[i].length);int temp = datas[i][j];datas[i][j] = datas[x][y];datas[x][y] = temp;}}//记录0号图片位置wc:for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {if (datas[i][j] == 0) {x0 = i;y0 = j;break wc;}}}}//窗体上组件的绘制public void paintView() {//标题图片JLabel titleLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\title.png"));titleLabel.setBounds(354, 27, 232, 57);this.add(titleLabel);//创建面板imagePanel = new JPanel();imagePanel.setBounds(150, 114, 360, 360);imagePanel.setLayout(null);//遍历二维数组,得到图片编号for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {//创建JLabel对象,加载图片资源JLabel imageLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\" + datas[i][j] + ".png"));//调整图片的位置imageLabel.setBounds(j * 90, i * 90, 90, 90);imagePanel.add(imageLabel);}}//把面板添加到窗体上this.add(imagePanel);//动漫参照图JLabel canZhaoTuLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\canzhaotu.png"));canZhaoTuLabel.setBounds(574, 114, 122, 121);this.add(canZhaoTuLabel);//上下左右,求助,重置按钮shangButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\shang.png"));shangButton.setBounds(732, 265, 57, 57);this.add(shangButton);zuoButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\zuo.png"));zuoButton.setBounds(650, 347, 57, 57);this.add(zuoButton);xiaButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\xia.png"));xiaButton.setBounds(732, 347, 57, 57);this.add(xiaButton);youButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\you.png"));youButton.setBounds(813, 347, 57, 57);this.add(youButton);qiuZhuButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\qiuzhu.png"));qiuZhuButton.setBounds(626, 444, 108, 45);this.add(qiuZhuButton);chongZhiButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\chongzhi.png"));chongZhiButton.setBounds(786, 444, 108, 45);this.add(chongZhiButton);//展示背景图JLabel backgroundLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\background.png"));backgroundLabel.setBounds(0, 0, 960, 530);this.add(backgroundLabel);}//用于窗体的基本设置public void initFrame() {//窗体大小this.setSize(960, 565);//窗体标题this.setTitle("动漫拼图");//窗体居中this.setLocationRelativeTo(null);//窗体关闭时退出应用程序this.setDefaultCloseOperation(3);//窗体位于其他窗口之上this.setAlwaysOnTop(true);//取消窗体默认布局this.setLayout(null);}
}

2.2.7 求助业务实现


注意事项:

  • 数组元素应该是1-16,而不是0-15了
  • 按钮也不能在点击了

分析思路:

  • 定义移动成功的方法:success()
  • 在success()方法内部进行如下操作:
    • 修改datas数组的元素为正确的元素值
    • 按钮设置为不可用
  • 在重置操作中调用两个方法:
    • 第一个方法:success()
    • 第二个方法:rePaintView()
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;public class PictureFrame extends JFrame {//定义一个二维数组,用来存储图片的编号int[][] datas = {{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 0}};//定义两个int类型的变量,用于纪录0号图片的位置private int x0;private int y0;//定义上左下右,求助,重置按钮private JButton shangButton;private JButton zuoButton;private JButton xiaButton;private JButton youButton;private JButton qiuZhuButton;private JButton chongZhiButton;//定义面板private JPanel imagePanel;// 构造方法方法中调用绘图方法public PictureFrame() {//用于窗体的基本设置initFrame();//二维数组元素打乱randomData();//窗体上组件的绘制paintView();//给按钮添加事件addButtonEvent();//设置窗体可见this.setVisible(true);}//移动成功的操作public void success() {datas = new int[][]{{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 16}};shangButton.setEnabled(false);zuoButton.setEnabled(false);xiaButton.setEnabled(false);youButton.setEnabled(false);}//移动的图形绘制//移动的图形重新绘制public void rePaintView() {//移除所有imagePanel.removeAll();//遍历二维数组,得到每一个图片编号for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {//在遍历的过程中,创建 JLabel 对象,加载图片资源JLabel imageLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\" + datas[i][j] + ".png"));//调整图片资源的摆放位置imageLabel.setBounds(j * 90, i * 90, 90, 90);imagePanel.add(imageLabel);}}//重新绘制窗体imagePanel.repaint();}//给按钮添加事件public void addButtonEvent() {shangButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {//                System.out.println("上");//边界处理if (x0 == 3) {return;}//位置交换datas[x0][y0] = datas[x0 + 1][y0];datas[x0 + 1][y0] = 0;x0 = x0 + 1;//重绘方法调用rePaintView();}});zuoButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if (y0 == 3) {return;}datas[x0][y0] = datas[x0][y0 + 1];datas[x0][y0 + 1] = 0;y0 = y0 + 1;rePaintView();}});xiaButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if (x0 == 0) {return;}datas[x0][y0] = datas[x0 - 1][y0];datas[x0 - 1][y0] = 0;x0 = x0 - 1;rePaintView();}});youButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if (y0 == 0) {return;}datas[x0][y0] = datas[x0][y0 - 1];datas[x0][y0 - 1] = 0;y0 = y0 - 1;rePaintView();}});qiuZhuButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {success();rePaintView();}});chongZhiButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("重置");}});}//二维数组元素打乱public void randomData() {Random r = new Random();for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {int x = r.nextInt(datas.length);int y = r.nextInt(datas[i].length);int temp = datas[i][j];datas[i][j] = datas[x][y];datas[x][y] = temp;}}//记录0号图片位置wc:for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {if (datas[i][j] == 0) {x0 = i;y0 = j;break wc;}}}}//窗体上组件的绘制public void paintView() {//标题图片JLabel titleLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\title.png"));titleLabel.setBounds(354, 27, 232, 57);this.add(titleLabel);//创建面板imagePanel = new JPanel();imagePanel.setBounds(150, 114, 360, 360);imagePanel.setLayout(null);//遍历二维数组,得到图片编号for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {//创建JLabel对象,加载图片资源JLabel imageLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\" + datas[i][j] + ".png"));//调整图片的位置imageLabel.setBounds(j * 90, i * 90, 90, 90);imagePanel.add(imageLabel);}}//把面板添加到窗体上this.add(imagePanel);//动漫参照图JLabel canZhaoTuLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\canzhaotu.png"));canZhaoTuLabel.setBounds(574, 114, 122, 121);this.add(canZhaoTuLabel);//上下左右,求助,重置按钮shangButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\shang.png"));shangButton.setBounds(732, 265, 57, 57);this.add(shangButton);zuoButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\zuo.png"));zuoButton.setBounds(650, 347, 57, 57);this.add(zuoButton);xiaButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\xia.png"));xiaButton.setBounds(732, 347, 57, 57);this.add(xiaButton);youButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\you.png"));youButton.setBounds(813, 347, 57, 57);this.add(youButton);qiuZhuButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\qiuzhu.png"));qiuZhuButton.setBounds(626, 444, 108, 45);this.add(qiuZhuButton);chongZhiButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\chongzhi.png"));chongZhiButton.setBounds(786, 444, 108, 45);this.add(chongZhiButton);//展示背景图JLabel backgroundLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\background.png"));backgroundLabel.setBounds(0, 0, 960, 530);this.add(backgroundLabel);}//用于窗体的基本设置public void initFrame() {//窗体大小this.setSize(960, 565);//窗体标题this.setTitle("动漫拼图");//窗体居中this.setLocationRelativeTo(null);//窗体关闭时退出应用程序this.setDefaultCloseOperation(3);//窗体位于其他窗口之上this.setAlwaysOnTop(true);//取消窗体默认布局this.setLayout(null);}
}

新知识:按钮失效setEnabled(false);

2.2.8 移动业务的问题

问题分析:

  • 每次移动之后,都要判断是否移动成功,如果成功了,需要调用成功的方法。

分析思路:

  • 定义一个方法,用于比较两个数组元素是否相同
  • 每次移动完毕,调用该方法,如果返回值为true,则调用success()方法
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;public class PictureFrame extends JFrame {//定义一个二维数组,用来存储图片的编号int[][] datas = {{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 0}};//定义移动成功后的数组private int[][] winDatas = {{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 0}};//定义两个int类型的变量,用于纪录0号图片的位置private int x0;private int y0;//定义上左下右,求助,重置按钮private JButton shangButton;private JButton zuoButton;private JButton xiaButton;private JButton youButton;private JButton qiuZhuButton;private JButton chongZhiButton;//定义面板private JPanel imagePanel;// 构造方法方法中调用绘图方法public PictureFrame() {//用于窗体的基本设置initFrame();//二维数组元素打乱randomData();//窗体上组件的绘制paintView();//给按钮添加事件addButtonEvent();//设置窗体可见this.setVisible(true);}//判断移动是否成功public boolean isSuccess() {for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {if (datas[i][j] != winDatas[i][j]) {return false;}}}return true;}//移动成功的操作public void success() {datas = new int[][]{{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 16}};shangButton.setEnabled(false);zuoButton.setEnabled(false);xiaButton.setEnabled(false);youButton.setEnabled(false);}//移动的图形绘制//移动的图形重新绘制public void rePaintView() {//移除所有imagePanel.removeAll();//遍历二维数组,得到每一个图片编号for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {//在遍历的过程中,创建 JLabel 对象,加载图片资源JLabel imageLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\" + datas[i][j] + ".png"));//调整图片资源的摆放位置imageLabel.setBounds(j * 90, i * 90, 90, 90);imagePanel.add(imageLabel);}}//重新绘制窗体imagePanel.repaint();}//给按钮添加事件public void addButtonEvent() {shangButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {//                System.out.println("上");//边界处理if (x0 == 3) {return;}//位置交换datas[x0][y0] = datas[x0 + 1][y0];datas[x0 + 1][y0] = 0;x0 = x0 + 1;//判断移动是否成功if(isSuccess()) {success();}//重绘方法调用rePaintView();}});zuoButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if (y0 == 3) {return;}datas[x0][y0] = datas[x0][y0 + 1];datas[x0][y0 + 1] = 0;y0 = y0 + 1;//判断移动是否成功if(isSuccess()) {success();}rePaintView();}});xiaButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if (x0 == 0) {return;}datas[x0][y0] = datas[x0 - 1][y0];datas[x0 - 1][y0] = 0;x0 = x0 - 1;//判断移动是否成功if(isSuccess()) {success();}rePaintView();}});youButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if (y0 == 0) {return;}datas[x0][y0] = datas[x0][y0 - 1];datas[x0][y0 - 1] = 0;y0 = y0 - 1;//判断移动是否成功if(isSuccess()) {success();}rePaintView();}});qiuZhuButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {success();rePaintView();}});chongZhiButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("重置");}});}//二维数组元素打乱public void randomData() {Random r = new Random();for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {int x = r.nextInt(datas.length);int y = r.nextInt(datas[i].length);int temp = datas[i][j];datas[i][j] = datas[x][y];datas[x][y] = temp;}}//记录0号图片位置wc:for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {if (datas[i][j] == 0) {x0 = i;y0 = j;break wc;}}}}//窗体上组件的绘制public void paintView() {//标题图片JLabel titleLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\title.png"));titleLabel.setBounds(354, 27, 232, 57);this.add(titleLabel);//创建面板imagePanel = new JPanel();imagePanel.setBounds(150, 114, 360, 360);imagePanel.setLayout(null);//遍历二维数组,得到图片编号for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {//创建JLabel对象,加载图片资源JLabel imageLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\" + datas[i][j] + ".png"));//调整图片的位置imageLabel.setBounds(j * 90, i * 90, 90, 90);imagePanel.add(imageLabel);}}//把面板添加到窗体上this.add(imagePanel);//动漫参照图JLabel canZhaoTuLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\canzhaotu.png"));canZhaoTuLabel.setBounds(574, 114, 122, 121);this.add(canZhaoTuLabel);//上下左右,求助,重置按钮shangButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\shang.png"));shangButton.setBounds(732, 265, 57, 57);this.add(shangButton);zuoButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\zuo.png"));zuoButton.setBounds(650, 347, 57, 57);this.add(zuoButton);xiaButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\xia.png"));xiaButton.setBounds(732, 347, 57, 57);this.add(xiaButton);youButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\you.png"));youButton.setBounds(813, 347, 57, 57);this.add(youButton);qiuZhuButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\qiuzhu.png"));qiuZhuButton.setBounds(626, 444, 108, 45);this.add(qiuZhuButton);chongZhiButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\chongzhi.png"));chongZhiButton.setBounds(786, 444, 108, 45);this.add(chongZhiButton);//展示背景图JLabel backgroundLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\background.png"));backgroundLabel.setBounds(0, 0, 960, 530);this.add(backgroundLabel);}//用于窗体的基本设置public void initFrame() {//窗体大小this.setSize(960, 565);//窗体标题this.setTitle("动漫拼图");//窗体居中this.setLocationRelativeTo(null);//窗体关闭时退出应用程序this.setDefaultCloseOperation(3);//窗体位于其他窗口之上this.setAlwaysOnTop(true);//取消窗体默认布局this.setLayout(null);}
}

2.2.9 重置业务实现


分析思路:

  • 当重置的时候,需要修改数组为1,2…15,0
  • 打乱数组元素
  • 重绘面板图
  • 设置按钮可用
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;public class PictureFrame extends JFrame {//定义一个二维数组,用来存储图片的编号int[][] datas = {{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 0}};//定义移动成功后的数组private int[][] winDatas = {{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 0}};//定义两个int类型的变量,用于纪录0号图片的位置private int x0;private int y0;//定义上左下右,求助,重置按钮private JButton shangButton;private JButton zuoButton;private JButton xiaButton;private JButton youButton;private JButton qiuZhuButton;private JButton chongZhiButton;//定义面板private JPanel imagePanel;// 构造方法方法中调用绘图方法public PictureFrame() {//用于窗体的基本设置initFrame();//二维数组元素打乱randomData();//窗体上组件的绘制paintView();//给按钮添加事件addButtonEvent();//设置窗体可见this.setVisible(true);}//判断移动是否成功public boolean isSuccess() {for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {if (datas[i][j] != winDatas[i][j]) {return false;}}}return true;}//移动成功的操作public void success() {datas = new int[][]{{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 16}};shangButton.setEnabled(false);zuoButton.setEnabled(false);xiaButton.setEnabled(false);youButton.setEnabled(false);}//移动的图形绘制//移动的图形重新绘制public void rePaintView() {//移除所有imagePanel.removeAll();//遍历二维数组,得到每一个图片编号for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {//在遍历的过程中,创建 JLabel 对象,加载图片资源JLabel imageLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\" + datas[i][j] + ".png"));//调整图片资源的摆放位置imageLabel.setBounds(j * 90, i * 90, 90, 90);imagePanel.add(imageLabel);}}//重新绘制窗体imagePanel.repaint();}//给按钮添加事件public void addButtonEvent() {shangButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {//                System.out.println("上");//边界处理if (x0 == 3) {return;}//位置交换datas[x0][y0] = datas[x0 + 1][y0];datas[x0 + 1][y0] = 0;x0 = x0 + 1;//判断移动是否成功if (isSuccess()) {success();}//重绘方法调用rePaintView();}});zuoButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if (y0 == 3) {return;}datas[x0][y0] = datas[x0][y0 + 1];datas[x0][y0 + 1] = 0;y0 = y0 + 1;//判断移动是否成功if (isSuccess()) {success();}rePaintView();}});xiaButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if (x0 == 0) {return;}datas[x0][y0] = datas[x0 - 1][y0];datas[x0 - 1][y0] = 0;x0 = x0 - 1;//判断移动是否成功if (isSuccess()) {success();}rePaintView();}});youButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if (y0 == 0) {return;}datas[x0][y0] = datas[x0][y0 - 1];datas[x0][y0 - 1] = 0;y0 = y0 - 1;//判断移动是否成功if (isSuccess()) {success();}rePaintView();}});qiuZhuButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {success();rePaintView();}});chongZhiButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {datas = new int[][]{{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 0}};randomData();rePaintView();shangButton.setEnabled(true);zuoButton.setEnabled(true);xiaButton.setEnabled(true);youButton.setEnabled(true);System.out.println("重置");}});}//二维数组元素打乱public void randomData() {Random r = new Random();for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {int x = r.nextInt(datas.length);int y = r.nextInt(datas[i].length);int temp = datas[i][j];datas[i][j] = datas[x][y];datas[x][y] = temp;}}//记录0号图片位置wc:for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {if (datas[i][j] == 0) {x0 = i;y0 = j;break wc;}}}}//窗体上组件的绘制public void paintView() {//标题图片JLabel titleLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\title.png"));titleLabel.setBounds(354, 27, 232, 57);this.add(titleLabel);//创建面板imagePanel = new JPanel();imagePanel.setBounds(150, 114, 360, 360);imagePanel.setLayout(null);//遍历二维数组,得到图片编号for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {//创建JLabel对象,加载图片资源JLabel imageLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\" + datas[i][j] + ".png"));//调整图片的位置imageLabel.setBounds(j * 90, i * 90, 90, 90);imagePanel.add(imageLabel);}}//把面板添加到窗体上this.add(imagePanel);//动漫参照图JLabel canZhaoTuLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\canzhaotu.png"));canZhaoTuLabel.setBounds(574, 114, 122, 121);this.add(canZhaoTuLabel);//上下左右,求助,重置按钮shangButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\shang.png"));shangButton.setBounds(732, 265, 57, 57);this.add(shangButton);zuoButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\zuo.png"));zuoButton.setBounds(650, 347, 57, 57);this.add(zuoButton);xiaButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\xia.png"));xiaButton.setBounds(732, 347, 57, 57);this.add(xiaButton);youButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\you.png"));youButton.setBounds(813, 347, 57, 57);this.add(youButton);qiuZhuButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\qiuzhu.png"));qiuZhuButton.setBounds(626, 444, 108, 45);this.add(qiuZhuButton);chongZhiButton = new JButton(new ImageIcon("itheima-picture-puzzle\\images\\chongzhi.png"));chongZhiButton.setBounds(786, 444, 108, 45);this.add(chongZhiButton);//展示背景图JLabel backgroundLabel = new JLabel(new ImageIcon("itheima-picture-puzzle\\images\\background.png"));backgroundLabel.setBounds(0, 0, 960, 530);this.add(backgroundLabel);}//用于窗体的基本设置public void initFrame() {//窗体大小this.setSize(960, 565);//窗体标题this.setTitle("动漫拼图");//窗体居中this.setLocationRelativeTo(null);//窗体关闭时退出应用程序this.setDefaultCloseOperation(3);//窗体位于其他窗口之上this.setAlwaysOnTop(true);//取消窗体默认布局this.setLayout(null);}
}
public class App {public static void main(String[] args) {PictureFrame pf = new PictureFrame();}
}

DAY09_继承拼图游戏案例相关推荐

  1. 微信小游戏入门案例——拼图游戏

    微信小游戏入门案例--拼图游戏 涉及内容:canvas组件.小程序界面绘图API 目录结构: pages\game\game.js // pages/game/game.js // 方块的初始位置 v ...

  2. 《Unity 游戏案例开发大全》一6.5 游戏主场景

    本节书摘来异步社区<Unity 游戏案例开发大全>一书中的第6章,第6.1节,作者: 吴亚峰 , 杜化美 , 于复兴 责编: 张涛,更多章节内容可以访问云栖社区"异步社区&quo ...

  3. android 拼图课程设计,拼图游戏设计_课程设计报告.docx

    Il Il Il Il 学号 1608220203 2016-2017学年 第一学期 <Windows程序设计> 课程设计报告 题目:拼图游戏设计 专业: 班级: 姓名: 指导教师: 成绩 ...

  4. c#拼图碎片形状_使用神经网络解决拼图游戏

    在一个排列不变性的数据上神经网络是困难的.拼图游戏就是这种类型的数据,那么神经网络能解决一个2x2的拼图游戏吗? 什么是置换不变性(Permutation Invariance)? 如果一个函数的输出 ...

  5. 《Python游戏趣味编程》 第10章 拼图游戏

    10 拼图游戏 图书简介可以看这里: 童晶:<Python游戏趣味编程>新书上架了 本章我们将编写一个拼图游戏,鼠标先后点击两个小拼图块,交换其坐标,直到全部达到正确位置,效果如图10-1 ...

  6. 用Android Studio做一个超好玩的拼图游戏,附送超详细注释的源码

    文章目录 一.项目概述 二.开发环境 三.需求分析 四.实现过程 1.拼图游戏布局绘制 2.拼图游戏时间计时 3.拼图游戏打乱显示 4.拼图游戏碎片位置切换 5.拼图游戏成功的条件 6.拼图游戏重新开 ...

  7. Qt实现简单拼图游戏

    文章目录 前言 演示图 1.ShowWidget.h 2.ShowWidget.cpp 3.MainWindow.h 4.MainWindow.cpp 前言 自己简单实现了下拼图功能.本来开始只是想显 ...

  8. java拼图游戏(带文档资料)

    开发工具:eclipse,jdk1.8 1.概述 拼图游戏是广受欢迎的一种智力游戏,它的变化多端,难度不一,让人百玩不厌.拼图游戏是把一张图片分成多个小块,根据难度一般有9块(3×3).16块(4×4 ...

  9. android移动拼图小游戏的图片,利用ViewDragHelper轻松实现Android拼图游戏

    前言 最近一段时间看了一些介绍ViewDragHelper的博客,感觉这是一个处理手势滑动的神器,看完以后就想做点东西练练手,于是就做了这个Android拼图小游戏. 先上个效果图 demo.gif ...

最新文章

  1. 面试官:说说Spring AOP、AspectJ、CGLIB ?它们有什么关系?
  2. Django框架之跨站请求伪造
  3. python初级语法_python语法基础
  4. SpringBoot生成日志文件---logback和log4j
  5. 从单体到Flink:一文读懂数据架构的演变
  6. 《Go 语言程序设计》读书笔记 (三) 方法
  7. Spring学习总结(33)—— 用 Spring 的 @Transactional 注解控制事务有哪些不生效的场景?
  8. Fatal error: Allowed memory size of 524288000 bytes exhausted (tried to allocate 64 bytes) in D
  9. Skip level 1 on 1
  10. 【代码笔记】iOS-切换条
  11. python7.2抛出自定义异常
  12. linux 远程扫描仪,扫描仪Web远程控制
  13. 【读书笔记】致加西亚的信(二)
  14. 如何让p标签里面的内容首行缩进
  15. Web认证方法探视(1)
  16. 租房需要注意些什么?
  17. AP学科介绍|AP艺术与设计(2D/3D艺术与设计、绘画)
  18. 验证短信延迟?是哪里出现问题
  19. 【深度学习】参数量、模型大小、显存
  20. 编译原理:算符优先分析实验

热门文章

  1. CSDN如何赚取下载积分
  2. IconMoon字体图标使用方法
  3. 【以太网交换安全】--- 端口隔离运行原理及二层隔离三层通信实例配置讲解
  4. C# 注册机功能开发,机器码设计
  5. 12.5 地图实时路况数据分析
  6. vue 双向绑定,列表、条件渲染,解决强制刷新闪烁问题
  7. 点云降采样--ApproximateVoxelGrid点云降采样
  8. kill -15、kill -9 与 kill
  9. 这些天的生活挺无聊的!
  10. 一款很好用的内网穿透工具--FRP