一、界面展示
1、运行界面展示
2、点击求助按钮界面展示
二、所需资源
三、项目结构
四、代码展示
1、创建一个类PictureFrame继承JFrame
代码展示:
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class PictureFrame extends JFrame {
//定义一个二位数组用来存储图片的编号
private 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);//取消该按钮的点击
xiaButton.setEnabled(false);
zuoButton.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("02picture_puzzle\\images\\" + datas[i][j] + ".png"));
//调整图片的位置
imageLabel.setBounds(j * 90, i * 90, 90, 90);//设置位置和大小
imagePanel.add(imageLabel);//把图片放置到面板上
}
}
this.add(imagePanel);//把面板添加到窗体上
imagePanel.repaint();//重新绘制窗体
}
//给按钮添加事件
public void addButtonEvent() {
shangButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
//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() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
//System.out.println("左");
//边界处理
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() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
//System.out.println("下");
//边界处理
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() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
//System.out.println("右");
//边界处理
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() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
//System.out.println("求助");
success();
rePaintView();
}
});
chongZhiButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
//System.out.println("重置");
datas = new int[][]{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 0}
};
randomData();//打乱数组
rePaintView();//重绘图片
shangButton.setEnabled(true);//设置按钮可重新使用
xiaButton.setEnabled(true);
zuoButton.setEnabled(true);
youButton.setEnabled(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[x].length);
int temp = datas[i][j];
datas[i][j] = datas[x][y];
datas[x][y] = temp;
}
}
//记录0号图片的位置
//wc:表示给这个循环起了一个名字
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;//退出指定的循环
}
}
}
//System.out.println(x0 + "," + y0);
}
//窗体上组件的绘制
public void paintView() {
//标题图片
JLabel titleLabel = new JLabel(new ImageIcon("02picture_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("02picture_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("02picture_puzzle\\images\\canzhaotu.png"));
canZhaoTuLabel.setBounds(574, 114, 122, 121);//设置位置和大小
this.add(canZhaoTuLabel);//把参照图添加到窗体上
//完成上下左右按钮以及重置和求助按钮
shangButton = new JButton(new ImageIcon("02picture_puzzle\\images\\shang.png"));
shangButton.setBounds(732, 256, 57, 57);//设置位置和大小
this.add(shangButton);//把向上添加到窗体上
xiaButton = new JButton(new ImageIcon("02picture_puzzle\\images\\xia.png"));
xiaButton.setBounds(732, 347, 57, 57);//设置位置和大小
this.add(xiaButton);
zuoButton = new JButton(new ImageIcon("02picture_puzzle\\images\\zuo.png"));
zuoButton.setBounds(650, 347, 57, 57);//设置位置和大小
this.add(zuoButton);
youButton = new JButton(new ImageIcon("02picture_puzzle\\images\\you.png"));
youButton.setBounds(813, 347, 57, 57);//设置位置和大小
this.add(youButton);
qiuZhuButton = new JButton(new ImageIcon("02picture_puzzle\\images\\qiuzhu.png"));
qiuZhuButton.setBounds(626, 444, 108, 45);//设置位置和大小
this.add(qiuZhuButton);
chongZhiButton = new JButton(new ImageIcon("02picture_puzzle\\images\\chongzhi.png"));
chongZhiButton.setBounds(786, 444, 108, 45);//设置位置和大小
this.add(chongZhiButton);
//展示背景图
JLabel backgroundLabel = new JLabel(new ImageIcon("02picture_puzzle\\images\\background.png"));
backgroundLabel.setBounds(0, 0, 960, 530);
this.add(backgroundLabel);
}
//用于窗体的基本设置
private void initFrame() {
this.setTitle("漫画拼图");//窗体标题
this.setSize(960, 565);//窗体大小
this.setDefaultCloseOperation(3);//窗体关闭时退出应用程序
this.setLocationRelativeTo(null);//窗体居中
this.setAlwaysOnTop(true);//窗体位于其他窗体之上
this.setLayout(null);//取消窗体默认布局
}
}
2、创建一个测试方法App.java
代码展示:
public class App {
public static void main(String[] args) {
PictureFrame pf = new PictureFrame();
}
}
好了,小伙伴们去尝试一下吧!!!!!