一、界面展示

1、运行界面展示

2、点击求助按钮界面展示

二、所需资源

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

backgound
​​​

canzhaotu

chongzhi

qiuzhu

shang

title

xia

you

zuo

三、项目结构

四、代码展示

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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic void actionPerformed(ActionEvent actionEvent) {//System.out.println("求助");success();rePaintView();}});chongZhiButton.addActionListener(new ActionListener() {@Overridepublic 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();}
}    

好了,小伙伴们去尝试一下吧!!!!!

JavaSE小游戏(美女漫画拼图)相关推荐

  1. pygame小游戏——中国地图拼图小游戏

    pygame小游戏--中国地图拼图小游戏 游戏简介 本游戏用python开发,可对中国各个省份进行拼图,设置三个模式,便于中小学生熟悉中国地图,省份及简称等.通过鼠标拖动省份到地图相应位置,寓教于乐. ...

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

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

  3. JavaScript前端开发小游戏之智能拼图

    HTML部分 <!DOCTYPE html> <html lang="en">     <head>         <meta char ...

  4. android小游戏源码拼图,android编写的数字拼图游戏(带详细注释)

    [实例简介]自己正在学android,编写了一个简单的数字拼图游戏,有详细注释,适合初学者参考使用,比较简单易懂 [实例截图] [核心代码] package com.tsu; import java. ...

  5. python图形小游戏代码_python 拼图游戏代码

    # -*- coding: utf-8 -*- import simpleguitk as simplegui import random # 载入外部图像 baymax = simplegui.lo ...

  6. h5封装去底部_贪婪洞窟H5:也出微信小游戏了!还是原来贪婪的味道

    贪婪洞窟H5 关键词:地牢探险.闯关.贪婪洞窟 推荐星数:5星 官方介绍:贪婪洞窟,刺激的地下城冒险游戏. 沐沐带你发现好游戏! 今天沐沐给大家推荐的这款游戏叫 <贪婪洞窟h5>. 游戏改 ...

  7. JavaSE基础项目:拼图小游戏

    目录 学习资源: 源码和图片资源: 项目结构: 项目界面: 注册界面源码: 登录界面源码: 游戏界面源码: 学习资源: 视频资源: 黑马程序员拼图小游戏 源码和图片资源: 图片/源码资源: 百度网盘 ...

  8. Android案例(1)——美女拼图小游戏

    视频地址: Android美女拼图小游戏 实现功能: (1)多个难度 第一关3*3. (2)倒计时 (3)图片切分 (4)图片位置变换 第一步: 在src下创建工具包com.imooc.game.ut ...

  9. android实现九宫格拼图小游戏

    贴一下效果图 接下来随便用一张图片就好 以下是全代码 自定义View GameView类 import android.content.Context; import android.graphics ...

最新文章

  1. 寻宝机器人科技竞赛_第19届广西青少年机器人竞赛组织工作筹备会暨广西青少年科技辅导员交流活动在贺州举行...
  2. C - 3 求正弦值
  3. python 多线程读写文件错误_python多线程老是报错。大神帮忙看看哈?
  4. 【CodeForces - 520B】Two Buttons (bfs或dp或时光倒流,trick)
  5. 深入jvm虚拟机第三版源码_深入JVM虚拟机,阿里架构师直言,这份文档真的是JVM最深解读...
  6. python可以这样学读书笔记_Python小白的读书笔记
  7. php 容器对象,Laravel 6.2 中添加了可调用容器对象的方法
  8. 【06年博文搬家】一个修改时间的批处理程序
  9. 自适应自旋锁--吞吐量和延迟以及管理开销的折中
  10. java轿煤悝炾厍桴,最让人放心的汉字笔画序库.doc
  11. OpenGl 之学习笔记 glNormal3f 函数理解和光源相关知识总结
  12. 算法+剑指offerの刷题笔记
  13. check_cbss_kafka.sh
  14. 超详细!动态规划详解分析(典型例题分析和对比,附源码)
  15. HTML 信息隐藏与提取
  16. elementUI级联选择器(Cascader)回显问题和clearCheckedNodes无效的解决方法
  17. edittextview 取消下划线
  18. mongodb基本语法及操作(增删改查)
  19. java生成html 控制编码方式_JAVA中文字符编码问题详解 控制台输出
  20. python项目实战——银行取款机系统(一)

热门文章

  1. 杰里之 SPI 从机使用注意事项【篇】
  2. 美国6岁病童圆梦做一天海豹突击队员
  3. 蓝桥杯第七届省赛 风扇模拟系统
  4. 小微企业阿里云最佳实践系列(二):RDS 数据库与DMS 数据库管理(数据管理)... 1
  5. [错误解决] [Java] iphone 华为 搜狗 手机 浏览器 下载文件 名称乱码
  6. 万字详谈加密合规:Tornado Cash制裁后时代
  7. 详解蔬菜自动售货机的结构设计原理
  8. Java课堂作业 Day2
  9. 数据结构 | 二叉树的一些性质及证明、树的路径长度、结点的路径长度
  10. 基于SSM饭店点餐收银管理系统【数据库设计、论文、源码、开题报告】