package com.seventeen.ui;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
/**
* @ClassName: GameJFrame
* @Author: Kox
* @Data: 2023/1/30
* @Sketch:
*/
public class GameJFrame extends JFrame implements KeyListener, ActionListener {
// 管理数据
int[][] data = new int[4][4];
// 记录空白方块在二维数组的位置
int x = 0;
int y = 0;
// 展示当前图片的路径
String path = "course_game\\image\\girl\\girl7\\";
// 存储正确的数据
int[][] win = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 0}
};
// 定义变量用来统计步数
int step = 0;
// 选项下面的条目对象
JMenuItem replayItem = new JMenuItem("重新游戏");
JMenuItem reLoginItem = new JMenuItem("重新登录");
JMenuItem closeItem = new JMenuItem("关闭游戏");
JMenuItem accountItem = new JMenuItem("公众号");
JMenuItem beautiful = new JMenuItem("美女");
JMenuItem animal = new JMenuItem("动物");
JMenuItem exercise = new JMenuItem("运动");
// 游戏界面
public GameJFrame() {
// 初始化界面
initJFrame();
// 初始化菜单
initJMenuBar();
// 初始化数据
initDate();
// 初始化图片
initImage();
// 显示
this.setVisible(true);
}
// 数据
private void initDate() {
int[] tempArr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
Random r = new Random();
for (int i = 0; i < tempArr.length; i++) {
int index = r.nextInt(tempArr.length);
int temp = tempArr[i];
tempArr[i] = tempArr[index];
tempArr[index] = temp;
}
for (int i = 0; i < tempArr.length; i++) {
if (tempArr[i] == 0) {
x = i / 4;
y = i % 4;
}
data[i / 4][i % 4] = tempArr[i];
}
}
// 图片
private void initImage() {
// 清空原本已经出现的所有图片
this.getContentPane().removeAll();
if (victory()) {
JLabel winJLabel = new JLabel(new ImageIcon("course_game\\image\\win.png"));
winJLabel.setBounds(203, 283, 197, 73);
this.getContentPane().add(winJLabel);
}
JLabel stepCount = new JLabel("步数:" + step);
stepCount.setBounds(50, 30, 100, 20);
this.getContentPane().add(stepCount);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
int num = data[i][j];
// 创建一个图片ImageIcon对象
ImageIcon icon = new ImageIcon(path + num + ".jpg");
// 创建一个JLabel的对象
JLabel jLabel1 = new JLabel(icon);
// 指定图片位置
jLabel1.setBounds(105 * j + 83, 105 * i + 134, 105, 105);
// 给图片添加边框
jLabel1.setBorder(new BevelBorder(BevelBorder.LOWERED));
// 管理容器添加到界面中
this.getContentPane().add(jLabel1);
}
}
// 添加背景图片
JLabel background = new JLabel(new ImageIcon("course_game\\image\\background.png"));
background.setBounds(40, 40, 508, 560);
this.getContentPane().add(background);
// 刷新一下界面
this.getContentPane().repaint();
}
// 菜单
private void initJMenuBar() {
// 菜单对象
JMenuBar jMenuBar = new JMenuBar();
// 选项-功能
JMenu functionJMenu = new JMenu("功能");
// 选项-关于我们
JMenu aboutJMenu = new JMenu("关于我们");
// 选项-换图
JMenu changePicture = new JMenu("更换图片");
// 选项下面的条目添加到选项中
functionJMenu.add(changePicture);
functionJMenu.add(replayItem);
functionJMenu.add(reLoginItem);
functionJMenu.add(closeItem);
changePicture.add(beautiful);
changePicture.add(animal);
changePicture.add(exercise);
aboutJMenu.add(accountItem);
// 给条目绑定事件
replayItem.addActionListener(this);
reLoginItem.addActionListener(this);
closeItem.addActionListener(this);
accountItem.addActionListener(this);
beautiful.addActionListener(this);
animal.addActionListener(this);
exercise.addActionListener(this);
// 将菜单里面的两个选项添加到菜单当中
jMenuBar.add(functionJMenu);
jMenuBar.add(aboutJMenu);
// 给整个界面设置菜单
this.setJMenuBar(jMenuBar);
}
// 界面
private void initJFrame() {
// 设置界面的宽高
this.setSize(603, 680);
// 设置界面的标题
this.setTitle("拼图单机版");
// 设置界面置顶
this.setAlwaysOnTop(true);
// 设置界面居中
this.setLocationRelativeTo(null);
// 设置关闭模式
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// 取消默认的居中放置
this.setLayout(null);
// 添加键盘监听事件
this.addKeyListener(this);
}
@Override
public void keyTyped(KeyEvent e) {
}
// 按下
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == 65) {
// 删除界面中的素有图片
this.getContentPane().removeAll();
// 加载第一张完整的图片
JLabel all = new JLabel(new ImageIcon(path + "all.jpg"));
all.setBounds(83, 134, 420, 420);
this.getContentPane().add(all);
// 添加背景图片
JLabel background = new JLabel(new ImageIcon("course_game\\image\\background.png"));
background.setBounds(40, 40, 508, 560);
this.getContentPane().add(background);
// 刷新界面
this.getContentPane().repaint();
}
}
// 松下
@Override
public void keyReleased(KeyEvent e) {
// 判断游戏是否胜利
if (victory()) {
return;
}
int code = e.getKeyCode();
if (code == 37) {
System.out.println("向左移动");
if (y == 3) {
return;
}
data[x][y] = data[x][y + 1];
data[x][y + 1] = 0;
y++;
// 计算器
step++;
initImage();
} else if (code == 38) {
System.out.println("向上移动");
if (x == 3) {
return;
}
data[x][y] = data[x + 1][y];
data[x + 1][y] = 0;
x++;
// 计算器
step++;
initImage();
} else if (code == 39) {
System.out.println("向右移动");
if (y == 0) {
return;
}
data[x][y] = data[x][y - 1];
data[x][y - 1] = 0;
y--;
// 计算器
step++;
initImage();
} else if (code == 40) {
System.out.println("向下移动");
if (x == 0) {
return;
}
data[x][y] = data[x - 1][y];
data[x - 1][y] = 0;
x--;
// 计算器
step++;
没有合适的资源?快使用搜索试试~ 我知道了~
course_game.zip
共567个文件
jpg:527个
png:21个
class:7个
需积分: 0 0 下载量 72 浏览量
2023-06-15
09:17:22
上传
评论
收藏 10.47MB ZIP 举报
温馨提示
course_game.zip
资源推荐
资源详情
资源评论
收起资源包目录
course_game.zip (567个子文件)
GameJFrame.class 8KB
LoginJFrame.class 6KB
CodeUtil.class 2KB
User.class 884B
RegisterJFrame.class 659B
Main.class 516B
App.class 453B
.gitignore 50B
course_game.iml 433B
GameJFrame.java 10KB
LoginJFrame.java 8KB
CodeUtil.java 2KB
User.java 869B
RegisterJFrame.java 666B
App.java 404B
Main.java 116B
all.jpg 399KB
all.jpg 328KB
all.jpg 319KB
all.jpg 253KB
all.jpg 240KB
all.jpg 238KB
all.jpg 231KB
all.jpg 211KB
all.jpg 203KB
all.jpg 200KB
all.jpg 200KB
all.jpg 197KB
all.jpg 193KB
all.jpg 173KB
all.jpg 167KB
all.jpg 152KB
all.jpg 149KB
all.jpg 148KB
all.jpg 143KB
all.jpg 139KB
all.jpg 134KB
all.jpg 133KB
all.jpg 127KB
all.jpg 126KB
all.jpg 125KB
all.jpg 123KB
all.jpg 122KB
all.jpg 120KB
all.jpg 118KB
all.jpg 113KB
all.jpg 103KB
10.jpg 23KB
7.jpg 23KB
12.jpg 23KB
11.jpg 23KB
16.jpg 23KB
7.jpg 23KB
14.jpg 23KB
8.jpg 23KB
9.jpg 23KB
10.jpg 23KB
4.jpg 22KB
13.jpg 22KB
9.jpg 22KB
6.jpg 22KB
5.jpg 22KB
1.jpg 22KB
12.jpg 22KB
11.jpg 22KB
11.jpg 22KB
15.jpg 21KB
3.jpg 21KB
15.jpg 21KB
2.jpg 21KB
3.jpg 21KB
16.jpg 21KB
6.jpg 20KB
15.jpg 20KB
10.jpg 20KB
2.jpg 19KB
16.jpg 19KB
14.jpg 19KB
10.jpg 19KB
5.jpg 19KB
11.jpg 19KB
4.jpg 19KB
14.jpg 18KB
6.jpg 18KB
12.jpg 18KB
8.jpg 18KB
6.jpg 18KB
10.jpg 18KB
10.jpg 17KB
1.jpg 17KB
13.jpg 17KB
2.jpg 17KB
15.jpg 17KB
11.jpg 17KB
11.jpg 17KB
7.jpg 17KB
7.jpg 17KB
3.jpg 17KB
14.jpg 17KB
13.jpg 17KB
共 567 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
2301_77747150
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- vmware workstation pro 17 linux版
- 3479521_1710042575-1.rwmod
- 安装及环境配置UMCM-2023C-ma笔记
- (完整)数据库课程设计餐厅点餐说明书-21ab6d3c8beb172ded630b1c59eef8c75ebf952c.doc
- 2023-04-06-项目笔记 - 第一百五十四阶段 - 4.4.2.152全局变量的作用域-152 -2024.06.04
- 松哥解协议松哥解协议松哥解协议松哥解协议松哥解协议
- 618节日618节日618节日
- tensorflow-gpu-2.9.1-cp37-cp37m-win-amd64.whl
- tensorflow-gpu-2.9.0-cp37-cp37m-win-amd64.whl
- tensorflow-gpu-2.9.0-cp39-cp39-win-amd64.whl
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功