JAVA的拼图游戏

2023-12-24 18:04
文章标签 java 拼图游戏

本文主要是介绍JAVA的拼图游戏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

看好路径

MyActionListener
public class MyActionListener implements ActionListener {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("按钮被点击了");}
}

 

MyJFrame
public class MyJFrame extends JFrame implements ActionListener {//创建一个按钮对象JButton jtb1 = new JButton("戳我呀");JButton jtb2 = new JButton("点击我呀");public MyJFrame() {this.setSize(603, 680);this.setTitle("拼图单机版 v1.0");//设置页面置顶this.setAlwaysOnTop(true);//设置页面居中this.setLocationRelativeTo(null);//设置关闭模式this.setDefaultCloseOperation(3);//取消居中放置,按照x,y轴分布this.setLayout(null);//设置位置和高jtb1.setBounds(0, 0, 100, 50);jtb1.addActionListener(this);//设置位置和高jtb2.setBounds(0, 0, 100, 50);jtb2.addActionListener(this);//这里的this时 本类对象//给按钮添加动作监听//jtb:组件对象,表示给哪个组件添加事件//addActionListener: 表示给哪个组件添加监听(动作监听,鼠标左键&&空格)//按钮添加到界面当中this.getContentPane().add(jtb1);this.getContentPane().add(jtb2);this.setVisible(true);}@Overridepublic void actionPerformed(ActionEvent e) {//对当前被操作的那个按钮对象//获取当前被操作的那个按钮对象Object source = e.getSource();if(source == jtb1){jtb1.setSize(200,200);}else if(source == jtb2){Random r = new Random();jtb2.setLocation(r.nextInt(500),r.nextInt(500));}}
}
MyJFrame2
public class MyJFrame2 extends JFrame implements MouseListener {//创建一个按钮对象JButton jtb1 = new JButton("点我呀");public MyJFrame2(){this.setSize(603, 680);this.setTitle("拼图单机版 v1.0");//设置页面置顶this.setAlwaysOnTop(true);//设置页面居中this.setLocationRelativeTo(null);//设置关闭模式this.setDefaultCloseOperation(3);//取消居中放置,按照x,y轴分布this.setLayout(null);//给按钮设置位置和宽高jtb1.setBounds(0,0,100,50);//给按钮绑定事件jtb1.addMouseListener(this);//本类对象this//按钮添加到界面内this.getContentPane().add(jtb1);//界面显示this.setVisible(true);}@Overridepublic void mouseClicked(MouseEvent e) {System.out.println("单击");}@Overridepublic void mousePressed(MouseEvent e) {System.out.println("按下不松");}@Overridepublic void mouseReleased(MouseEvent e) {System.out.println("松开");}@Overridepublic void mouseEntered(MouseEvent e) {System.out.println("松开");}@Overridepublic void mouseExited(MouseEvent e) {System.out.println("滑出");}
}
MyJFrame3
public class MyJFrame3 extends JFrame implements KeyListener {public MyJFrame3() {this.setSize(603, 680);this.setTitle("拼图单机版 v1.0");//设置页面置顶this.setAlwaysOnTop(true);//设置页面居中this.setLocationRelativeTo(null);//设置关闭模式this.setDefaultCloseOperation(3);//取消居中放置,按照x,y轴分布this.setLayout(null);//给整个窗体添加键盘监听//参数this,当事件被触发后,执行本类中对应的代码this.addKeyListener(this);this.setVisible(true);}@Overridepublic void keyTyped(KeyEvent e) {}@Overridepublic void keyPressed(KeyEvent e) {System.out.println("按下不松");}@Overridepublic void keyReleased(KeyEvent e) {System.out.println("松开按键");int code = e.getKeyCode();if(code == 65){System.out.println("现在按住的是A");}else if(code == 66){System.out.println("现在按住的是B");}}
}

Test1

public class Test1{public static void main(String[] args){//需求://打乱数据0~9,三个一组加到二维数组中//定义一个一维数组int[] tempArr = {0,1,2,3,4,5,6,7,8};//打乱数组中的数据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++) {System.out.println(tempArr[i] + " ");}System.out.println();//创建二维数组int[][] data = new int[3][3];//方法二:遍历二维数组int index = 0;for (int i = 0; i < data.length; i++) {for (int j = 0; j < data[i].length; j++) {data[i][j] = tempArr[index];index++;}}//遍历二维数组for (int i = 0; i < data.length; i++) {for (int j = 0; j < data[i].length; j++) {System.out.println(data[i][j] + " ");}System.out.println();}}
}

Test2

public class Test2 {public static void main(String[] args){JFrame jFrame = new JFrame();//涉恶之界面的宽高jFrame.setSize(603,680);//设置界面的标题jFrame.setTitle("事件演示");//设置页面置顶jFrame.setAlwaysOnTop(true);//设置页面居中jFrame.setLocationRelativeTo(null);//设置关闭模式jFrame.setDefaultCloseOperation(3);//取消居中放置,按照x,y轴分布jFrame.setLayout(null);//创建一个按钮对象JButton jtb = new JButton("戳我呀");//设置位置和高jtb.setBounds(0,0,100,50);//给按钮添加动作监听//jtb:组件对象,表示给哪个组件添加事件//addActionListener: 表示给哪个组件添加监听(动作监听,鼠标左键&&空格)jtb.addActionListener(new MyActionListener());jtb.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("不要点击我呀~");}});//把按钮添加到界面中jFrame.getContentPane().add(jtb);jFrame.setVisible(true);}
}

Test3

public class Test3 {public static void main(String[] args){new MyJFrame2();}
}

Test4

public class  Test4 {public static void main(String[] args){new MyJFrame3();}
}
GameJFrame
public class GameJFrame extends JFrame implements KeyListener, ActionListener {//游戏的主界面//创建二维数组//目的:用来管理数据//加载图片时,会根据二维数组中的数据进行加载int[][] data = new int[4][4];//记录空白方块在二维数组中的位置int x = 0;int y = 0;//定义一个变量,记录当前展示图片的路径String path = "src\\com\\yst\\ui\\image\\";//定义一个二维数组,储存正确的数据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("公众号");public GameJFrame(){//初始化界面initJFrame();//初始化菜单initJMenuBar();//初始化数据(打乱)initData();//初始化图片initImage();//让显示出来this.setVisible(true);}private void initData(){//定义一个一维数组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];}//        //方法二:遍历二维数组
//        int index = 0;
//        for (int i = 0; i < data.length; i++) {
//            for (int j = 0; j < data[i].length; j++) {
//                data[i][j] = tempArr[index];
//                index++;
//            }
//        }
//        //遍历二维数组
//        for (int i = 0; i < data.length; i++) {
//            for (int j = 0; j < data[i].length; j++) {
//                System.out.println(data[i][j] + " ");
//            }
//            System.out.println();
//        }}//初始化图片private void initImage(){//清空原本已经出现的所有图片this.getContentPane().removeAll();if(victory()){//显示胜利图标JLabel winJLabel = new JLabel(new ImageIcon(path + "19.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);//细节//先加载的图片在上方//外循环表示内循环纪念性4次for (int i = 0; i < 4; i++) {//内循环表示一行加载4张图片for (int j = 0; j < 4; j++) {//获取当前要加载的图片序号int num = data[i][j];//创建一个图片ImageIcon的对象JLabel jLabel = new JLabel(new ImageIcon(path +num+".png"));//指定图片位置jLabel.setBounds(210*j + 83,210*i + 134,210,210);//给图片设置边框//1:图片凹陷//2:图片突起jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));// 把管理容器添加到界面this.getContentPane().add(jLabel);}}//添加背景图片JLabel background = new JLabel(new ImageIcon(path + "18.png"));background.setBounds(40,40,900,900);//添加到界面中this.getContentPane().add(background);//刷新界面this.getContentPane().repaint();}private void initJMenuBar() {//初始化菜单//创建整个菜单对象JMenuBar jMenuBar = new JMenuBar();//创建菜单上面的两个对象JMenu functionJMenu = new JMenu("功能");JMenu aboutJMenu = new JMenu("关于我们");// 将每一个选项下面的条目添加到选项当中functionJMenu.add(replayItem);functionJMenu.add(reLoginItem);functionJMenu.add(closeItem);aboutJMenu.add(accountItem);//给条目绑定监听事件replayItem.addActionListener(this);reLoginItem.addActionListener(this);closeItem.addActionListener(this);accountItem.addActionListener(this);//将菜单里面的两个选项添加到菜单jMenuBar.add(functionJMenu);jMenuBar.add(aboutJMenu);//给整个界面设置菜单this.setJMenuBar(jMenuBar);}private void initJFrame() {this.setSize(1000,1000);//设置界面标题this.setTitle("拼图单机版 v1.0");//设置界面置顶this.setAlwaysOnTop(true);//设置界面居中this.setLocationRelativeTo(null);//设置关闭模式this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//取消默认的居中放置,只有取消了才会按照x,y轴添加组件this.setLayout(null);//给整个界面添加键盘监听事件this.addKeyListener(this);}@Overridepublic void keyTyped(KeyEvent e) {}//按住不松时调用这个方法@Overridepublic void keyPressed(KeyEvent e) {int code = e.getKeyCode();if(code == 65){//界面中的所有图片全部删除this.getContentPane().removeAll();//加载第一张完整图片JLabel all = new JLabel(new ImageIcon(path + "21.png"));all.setBounds(83,50,820,820);this.getContentPane().add(all);//加载背景图片//添加背景图片JLabel background = new JLabel(new ImageIcon(path + "10.png"));background.setBounds(40,40,913,769);//添加到界面中this.getContentPane().add(background);//刷新界面this.getContentPane().repaint();}}//松开按键时调用此方法@Overridepublic void keyReleased(KeyEvent e) {//如果游戏胜利,直接结束游戏不再进行if(victory()){//结束方法return;}int code = e.getKeyCode();System.out.println(code);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++;//调用方法按照最新数字加载图片initImage();} else if (code == 65) {initImage();} else if (code == 87){{data = new int[][]{{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}};initImage();}}}public boolean victory(){for(int i = 0; i < data.length; i++){//i: 一次表示二维数组data里面的索引//data[i]: 依次表示每一个一维数组for(int j = 0; j < data[i].length; j++){if(data[i][j] != win[i][j]){return false;}}}return true;}@Overridepublic void actionPerformed(ActionEvent e) {//获取当前被点击的条目对象Object obj = e.getSource();//判断if(obj == replayItem){System.out.println("重新游戏");//计步器清零step = 0;//再次打乱二维数组中的数据initData();//重新加载图片initImage();} else if (obj == reLoginItem) {System.out.println("重新登陆");//关闭当前的游戏界面this.setVisible(false);//打开游戏的登陆界面new LoginJFrame();} else if (obj == closeItem) {System.out.println("关闭游戏");//直接关闭虚拟机System.exit(0);} else if (obj == accountItem) {System.out.println("公众号");//创建一个弹窗对象JDialog jDialog = new JDialog();JLabel jLabel = new JLabel(new ImageIcon("src\\com\\yst\\ui\\image\\20.png"));//宽高是相对于弹窗而言的jLabel.setBounds(0,0,172,172);//把图片添加到弹窗中jDialog.getContentPane().add(jLabel);//给弹窗设置大小jDialog.setSize(344,344);//弹窗置顶jDialog.setAlwaysOnTop(true);//弹框居中jDialog.setLocationRelativeTo(null);//弹窗不关闭则无法进行下面操作的界面jDialog.setModal(true);//让弹框显示出来jDialog.setVisible(true);}}
}
LoginJFrame
public class LoginJFrame extends JFrame {public LoginJFrame(){//在创建登陆界面时,同时设置一些信息this.setSize(488,430);//设置界面标题this.setTitle("拼图 登录");//设置界面置顶this.setAlwaysOnTop(true);//设置界面居中this.setLocationRelativeTo(null);//设置关闭模式this.setDefaultCloseOperation(3);//让显示出来this.setVisible(true);}
}
RegisterJFrame
public class RegisterJFrame extends JFrame {public RegisterJFrame(){this.setSize(488,500);//设置界面标题this.setTitle("拼图 注册");//设置界面置顶this.setAlwaysOnTop(true);//设置界面居中this.setLocationRelativeTo(null);//设置关闭模式this.setDefaultCloseOperation(3);//让显示出来this.setVisible(true);}
}
App
public class App {public static void main(String[] args){//表示程序的启动入口//如果想开启一个界面,就创建谁的对象就可以
//        new LoginJFrame();
//
//        new RegisterJFrame();new GameJFrame();}
}

图片有21张,注意相对地址和大小,有作弊码和快捷键

这篇关于JAVA的拼图游戏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/532644

相关文章

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

Java.lang.InterruptedException被中止异常的原因及解决方案

《Java.lang.InterruptedException被中止异常的原因及解决方案》Java.lang.InterruptedException是线程被中断时抛出的异常,用于协作停止执行,常见于... 目录报错问题报错原因解决方法Java.lang.InterruptedException 是 Jav