Java拼图游戏

运行出的游戏界面如下:

按住A不松开,显示完整图片;松开A显示随机打乱的图片。

User类

package domain;/*** @ClassName: User* @Author: Kox* @Data: 2023/2/2* @Sketch:*/
public class User {private String username;private String password;public User() {}public User(String username, String password) {this.username = username;this.password = password;}/*** 获取* @return username*/public String getUsername() {return username;}/*** 设置* @param username*/public void setUsername(String username) {this.username = username;}/*** 获取* @return password*/public String getPassword() {return password;}/*** 设置* @param password*/public void setPassword(String password) {this.password = password;}}

CodeUtil类

 

package util;import java.util.ArrayList;
import java.util.Random;public class CodeUtil {public static String getCode(){//1.创建一个集合ArrayList<Character> list = new ArrayList<>();//52  索引的范围:0 ~ 51//2.添加字母 a - z  A - Zfor (int i = 0; i < 26; i++) {list.add((char)('a' + i));//a - zlist.add((char)('A' + i));//A - Z}//3.打印集合//System.out.println(list);//4.生成4个随机字母String result = "";Random r = new Random();for (int i = 0; i < 4; i++) {//获取随机索引int randomIndex = r.nextInt(list.size());char c = list.get(randomIndex);result = result + c;}//System.out.println(result);//长度为4的随机字符串//5.在后面拼接数字 0~9int number = r.nextInt(10);//6.把随机数字拼接到result的后面result = result + number;//System.out.println(result);//ABCD5//7.把字符串变成字符数组char[] chars = result.toCharArray();//[A,B,C,D,5]//8.在字符数组中生成一个随机索引int index = r.nextInt(chars.length);//9.拿着4索引上的数字,跟随机索引上的数字进行交换char temp = chars[4];chars[4] = chars[index];chars[index] = temp;//10.把字符数组再变回字符串String code = new String(chars);//System.out.println(code);return code;}
}

登陆代码

package ui;import domain.User;
import util.CodeUtil;import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;public class LoginJFrame extends JFrame implements MouseListener {static ArrayList<User> allUsers = new ArrayList<>();static {allUsers.add(new User("zhangsan","123"));allUsers.add(new User("lisi","1234"));}JButton login = new JButton();JButton register = new JButton();JTextField username = new JTextField();//JTextField password = new JTextField();JPasswordField password = new JPasswordField();JTextField code = new JTextField();//正确的验证码JLabel rightCode = new JLabel();public LoginJFrame() {//初始化界面initJFrame();//在这个界面中添加内容initView();//让当前界面显示出来this.setVisible(true);}public void initView() {//1. 添加用户名文字JLabel usernameText = new JLabel(new ImageIcon("拼图小游戏_image\\image\\login\\用户名.png"));usernameText.setBounds(116, 135, 47, 17);this.getContentPane().add(usernameText);//2.添加用户名输入框username.setBounds(195, 134, 200, 30);this.getContentPane().add(username);//3.添加密码文字JLabel passwordText = new JLabel(new ImageIcon("拼图小游戏_image\\image\\login\\密码.png"));passwordText.setBounds(130, 195, 32, 16);this.getContentPane().add(passwordText);//4.密码输入框password.setBounds(195, 195, 200, 30);this.getContentPane().add(password);//验证码提示JLabel codeText = new JLabel(new ImageIcon("拼图小游戏_image\\image\\login\\验证码.png"));codeText.setBounds(133, 256, 50, 30);this.getContentPane().add(codeText);//验证码的输入框code.setBounds(195, 256, 100, 30);this.getContentPane().add(code);String codeStr = CodeUtil.getCode();//设置内容rightCode.setText(codeStr);//绑定鼠标事件rightCode.addMouseListener(this);//位置和宽高rightCode.setBounds(300, 256, 50, 30);//添加到界面this.getContentPane().add(rightCode);//5.添加登录按钮login.setBounds(123, 310, 128, 47);login.setIcon(new ImageIcon("拼图小游戏_image\\image\\login\\登录按钮.png"));//去除按钮的边框login.setBorderPainted(false);//去除按钮的背景login.setContentAreaFilled(false);//给登录按钮绑定鼠标事件login.addMouseListener(this);this.getContentPane().add(login);//6.添加注册按钮register.setBounds(256, 310, 128, 47);register.setIcon(new ImageIcon("拼图小游戏_image\\image\\login\\注册按钮.png"));//去除按钮的边框register.setBorderPainted(false);//去除按钮的背景register.setContentAreaFilled(false);//给注册按钮绑定鼠标事件register.addMouseListener(this);this.getContentPane().add(register);//7.添加背景图片JLabel background = new JLabel(new ImageIcon("拼图小游戏_image\\image\\login\\background.png"));background.setBounds(0, 0, 470, 390);this.getContentPane().add(background);}public void initJFrame() {this.setSize(488, 430);//设置宽高this.setTitle("拼图游戏 V1.0登录");//设置标题this.setDefaultCloseOperation(3);//设置关闭模式this.setLocationRelativeTo(null);//居中this.setAlwaysOnTop(true);//置顶this.setLayout(null);//取消内部默认布局}//点击@Overridepublic void mouseClicked(MouseEvent e) {if (e.getSource() == login) {System.out.println("点击了登录按钮");//获取两个文本输入框中的内容String usernameInput = username.getText();String passwordInput = password.getText();//获取用户输入的验证码String codeInput = code.getText();//创建一个User对象User userInfo = new User(usernameInput, passwordInput);System.out.println("用户输入的用户名为" + usernameInput);System.out.println("用户输入的密码为" + passwordInput);if (codeInput.length() == 0) {showJDialog("验证码不能为空");} else if (usernameInput.length() == 0 || passwordInput.length() == 0) {//校验用户名和密码是否为空System.out.println("用户名或者密码为空");//调用showJDialog方法并展示弹框showJDialog("用户名或者密码为空");} else if (!codeInput.equalsIgnoreCase(rightCode.getText())) {showJDialog("验证码输入错误");} else if (contains(userInfo)) {System.out.println("用户名和密码正确可以开始玩游戏了");//关闭当前登录界面this.setVisible(false);//打开游戏的主界面//需要把当前登录的用户名传递给游戏界面new GameJFrame();} else {System.out.println("用户名或密码错误");showJDialog("用户名或密码错误");}} else if (e.getSource() == register) {System.out.println("点击了注册按钮");} else if (e.getSource() == rightCode) {System.out.println("更换验证码");//获取一个新的验证码String code = CodeUtil.getCode();rightCode.setText(code);}}public void showJDialog(String content) {//创建一个弹框对象JDialog jDialog = new JDialog();//给弹框设置大小jDialog.setSize(200, 150);//让弹框置顶jDialog.setAlwaysOnTop(true);//让弹框居中jDialog.setLocationRelativeTo(null);//弹框不关闭永远无法操作下面的界面jDialog.setModal(true);//创建Jlabel对象管理文字并添加到弹框当中JLabel warning = new JLabel(content);warning.setBounds(0, 0, 200, 150);jDialog.getContentPane().add(warning);//让弹框展示出来jDialog.setVisible(true);}//按下不松@Overridepublic void mousePressed(MouseEvent e) {if (e.getSource() == login) {login.setIcon(new ImageIcon("拼图小游戏_image\\image\\login\\登录按下.png"));} else if (e.getSource() == register) {register.setIcon(new ImageIcon("拼图小游戏_image\\image\\login\\注册按下.png"));}}//松开按钮@Overridepublic void mouseReleased(MouseEvent e) {if (e.getSource() == login) {login.setIcon(new ImageIcon("jigsawgame\\image\\login\\登录按钮.png"));} else if (e.getSource() == register) {register.setIcon(new ImageIcon("jigsawgame\\image\\login\\注册按钮.png"));}}//鼠标划入@Overridepublic void mouseEntered(MouseEvent e) {}//鼠标划出@Overridepublic void mouseExited(MouseEvent e) {}//判断用户在集合中是否存在public boolean contains(User userInput){for (int i = 0; i < allUsers.size(); i++) {User rightUser = allUsers.get(i);if(userInput.getUsername().equals(rightUser.getUsername()) && userInput.getPassword().equals(rightUser.getPassword())){//有相同的代表存在,返回true,后面的不需要再比了return true;}}//循环结束之后还没有找到就表示不存在return false;}}

 注册代码

package ui;import domain.User;
import util.CodeUtil;import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;
import java.util.ArrayList;
import java.util.List;public class RegisterJFrame extends JFrame implements MouseListener {ArrayList<User> list = new ArrayList<>();//提升三个输入框的变量的作用范围,让这三个变量可以在本类中所有方法里面可以使用。JTextField username = new JTextField();JTextField password = new JTextField();JTextField rePassword = new JTextField();//提升两个按钮变量的作用范围,让这两个变量可以在本类中所有方法里面可以使用。JButton submit = new JButton();JButton reset = new JButton();public RegisterJFrame() throws IOException {user();initFrame();initView();setVisible(true);}public void user() throws IOException {File file = new File("user.txt");file.createNewFile();BufferedReader br = new BufferedReader(new FileReader("user.txt"));String str;while ((str = br.readLine()) != null) {String[] user = str.split("&");//nameString name = user[0].split("=")[1];//passwordString password = user[1].split("=")[1];list.add(new User(name, password));}}@Overridepublic void mouseClicked(MouseEvent e) {//获取输入框中的内容String userNameStr = username.getText();String passWordStr = password.getText();String rePasswordText = rePassword.getText();if (e.getSource() == submit){//注册System.out.println("注册");//判断输入框是否有空if ((userNameStr.length() == 0) || (passWordStr.length() == 0) || (rePasswordText.length() == 0)){showDialog("账号或密码不能为空");//清空密码password.setText("");rePassword.setText("");} else if (!passWordStr.equals(rePasswordText)) {showDialog("密码不一致");//清空密码rePassword.setText("");} else if (!tfUsername(userNameStr)) { //账户已存在showDialog("账号已存在");} else {try {//将数据存入本地文件中User(userNameStr,passWordStr);showDialog("注册成功");this.setVisible(false);new LoginJFrame();} catch (IOException ex) {throw new RuntimeException(ex);}}//}else if(e.getSource() == reset){//重置System.out.println("重置");password.setText("");rePassword.setText("");username.setText("");}}/** 将数据账号数据存入本地文件中* 参数1:账号 参数2:密码* */private void User(String name , String password) throws IOException {String user = "name="+name+"&password="+password;BufferedWriter bw = new BufferedWriter(new FileWriter("user.txt",true));bw.write(user);bw.newLine();bw.close();}/** 检测账号是否存在* 返回值 boolean* 传入 username* */private boolean tfUsername(String userName){for (User user : list) {if(user.getUsername().equals(userName))return false;}return true;}@Overridepublic void mousePressed(MouseEvent e) {if (e.getSource() == submit) {submit.setIcon(new ImageIcon("拼图小游戏_image\\image\\register\\注册按下.png"));} else if (e.getSource() == reset) {reset.setIcon(new ImageIcon("拼图小游戏_image\\image\\register\\重置按下.png"));}}@Overridepublic void mouseReleased(MouseEvent e) {if (e.getSource() == submit) {submit.setIcon(new ImageIcon("拼图小游戏_image\\image\\register\\注册按钮.png"));} else if (e.getSource() == reset) {reset.setIcon(new ImageIcon("拼图小游戏_image\\image\\register\\重置按钮.png"));}}@Overridepublic void mouseEntered(MouseEvent e) {}@Overridepublic void mouseExited(MouseEvent e) {}private void initView() {//添加注册用户名的文本JLabel usernameText = new JLabel(new ImageIcon("拼图小游戏_image\\image\\login\\用户名.png"));usernameText.setBounds(85, 135, 80, 20);//添加注册用户名的输入框username.setBounds(195, 134, 200, 30);//添加注册密码的文本JLabel passwordText = new JLabel(new ImageIcon("拼图小游戏_image\\image\\register\\注册密码.png"));passwordText.setBounds(97, 193, 70, 20);//添加密码输入框password.setBounds(195, 195, 200, 30);//添加再次输入密码的文本JLabel rePasswordText = new JLabel(new ImageIcon("拼图小游戏_image\\image\\register\\再次输入密码.png"));rePasswordText.setBounds(64, 255, 95, 20);//添加再次输入密码的输入框rePassword.setBounds(195, 255, 200, 30);//注册的按钮submit.setIcon(new ImageIcon("拼图小游戏_image\\image\\register\\注册按钮.png"));submit.setBounds(123, 310, 128, 47);submit.setBorderPainted(false);submit.setContentAreaFilled(false);submit.addMouseListener(this);//重置的按钮reset.setIcon(new ImageIcon("拼图小游戏_image\\image\\register\\重置按钮.png"));reset.setBounds(256, 310, 128, 47);reset.setBorderPainted(false);reset.setContentAreaFilled(false);reset.addMouseListener(this);//背景图片JLabel background = new JLabel(new ImageIcon("拼图小游戏_image\\image\\background.png"));background.setBounds(0, 0, 470, 390);this.getContentPane().add(usernameText);this.getContentPane().add(passwordText);this.getContentPane().add(rePasswordText);this.getContentPane().add(username);this.getContentPane().add(password);this.getContentPane().add(rePassword);this.getContentPane().add(submit);this.getContentPane().add(reset);this.getContentPane().add(background);}private void initFrame() {//对自己的界面做一些设置。//设置宽高setSize(488, 430);//设置标题setTitle("拼图游戏 V1.0注册");//取消内部默认布局setLayout(null);//设置关闭模式setDefaultCloseOperation(3);//设置居中setLocationRelativeTo(null);//设置置顶setAlwaysOnTop(true);}//只创建一个弹框对象JDialog jDialog = new JDialog();//因为展示弹框的代码,会被运行多次//所以,我们把展示弹框的代码,抽取到一个方法中。以后用到的时候,就不需要写了//直接调用就可以了。public void showDialog(String content){if(!jDialog.isVisible()){//把弹框中原来的文字给清空掉。jDialog.getContentPane().removeAll();JLabel jLabel = new JLabel(content);jLabel.setBounds(0,0,200,150);jDialog.add(jLabel);//给弹框设置大小jDialog.setSize(200, 150);//要把弹框在设置为顶层 -- 置顶效果jDialog.setAlwaysOnTop(true);//要让jDialog居中jDialog.setLocationRelativeTo(null);//让弹框jDialog.setModal(true);//让jDialog显示出来jDialog.setVisible(true);}}
}

游戏代码

import java.io.IOException;import ui.GameJFrame;
import ui.LoginJFrame;
import ui.RegisterJFrame;/*** @ClassName: App* @Author: Kox* @Data: 2023/1/30* @Sketch:*/
public class App {public static void main(String[] args) throws IOException {// 登录界面new LoginJFrame();// 注册界面new RegisterJFrame();// 游戏界面//    new GameJFrame();}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/192702.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Michael Jordan最新报告:去中心化机器学习中的契约、不确定性和激励

‍ ‍导读 11月3日&#xff0c;智源研究院学术顾问委员会委员、机器学习泰斗Michael Jordan在以“新一代人工智能前沿”为主题的2023北京论坛 新工科专题论坛上&#xff0c;发表了题为Contracts, Uncertainty, and Incentives in Decentralized Machine Learning&#xff08;去…

Thrift协议详解

前言特点高效性的体现可拓展性的体现 应用场景示例拓展其他常用协议接口描述语言&#xff08;IDL&#xff09;TBinaryProtocolTCompactProtocolTDebugProtocolTDenseProtocolTJSONProtocol 前言 Thrift协议是一种接口描述语言和二进制通讯协议&#xff0c;它被用来定义和创建跨…

Linux procps-ng - top

procps-ng 是一个开源的进程管理工具集&#xff0c;它提供了一系列用于监控和管理系统进程的命令行工具。它是 procps 工具集的一个分支&#xff0c;旨在改进和增强原有的 procps 工具。 procps-ng 包括了一些常用的命令行工具&#xff0c;例如&#xff1a; ps&#xff1a;用于…

SVG的viewBox、width和height释义, 示例及代码

svg的是没有边界的&#xff0c;svg画布只是用于展示svg世界中某一个范围的内容&#xff0c;而对于超过了svg画布范围的内容&#xff0c;则会被遮挡。默认svg画布默认显示世界坐标下原点坐标的width*height面积的矩形视野。 ​ 我们可以通过viewBox来修改默认的显示配置&#…

测不准原理

测不准原理 算符的对易关系 commutation relation 测不准原理的矢量推导 Schwarz inequality: 设对易关系&#xff1a; 设一个新态&#xff1a; 投影&#xff1a; 那么有&#xff1a; 代回Schwarz inequality 即可证明&#xff1a;

创建谷歌账号 绕过手机验证(2023.11亲测有效)

如何成功注册谷歌账号&#xff1a;一个详细实用指南 写在最前面谷歌注册全流程环境配置切换至全英文环境 开通foxmail.com邮箱在英文环境下注册验证邮箱注册过程中的注意事项完成&#xff01;总结 写在最前面 在这个数字化迅速发展的时代&#xff0c;谷歌账号几乎成为了我们日…

Linux非阻塞等待示例

Linux非阻塞等待实例 非阻塞等待的意义&#xff1a;简单的多进程编程示例代码解释 非阻塞等待的意义&#xff1a; 非阻塞等待在多进程编程中的意义主要体现在提高系统的响应性、实现异步任务执行、动态任务管理和多任务协同工作等方面。它允许父进程在等待子进程退出的同时&…

windows nodejs 15.0.0下载安装

下载 Node v15.0.0 (Current) | Node.js (nodejs.org) 下载地址 https://nodejs.org/dist/v15.0.0/node-v15.0.0-x64.msi 安装 双击运行 等待安装完成 确认安装成功 管理员运行cmd 查看版本号

python算法例15 合并数字

1. 问题描述 给出n个数&#xff0c;将这n个数合并成一个数&#xff0c;每次只能选择两个数a、b合并&#xff0c;合并需要消耗的能量为ab&#xff0c;输出将n个数合并成一个数后消耗的最小能量。 2. 问题示例 给出[1&#xff0c;2&#xff0c;3&#xff0c;4]&#xff0c;返回…

【作业】操作系统实验一:进程和线程

文章目录 实验内容一、进程的创建1、编辑源程序2、编辑结果3、编译和运行程序4、解释运行结果 二、进程共享1、运行2、解释运行结果 三、进程终止1、运行2、解释运行结果 四、进程同步1、运行2、解释运行结果 五、Linux中子进程映像的重新装入1、运行2、解释运行结果 六、线程1…

多因素方差分析(Multi-way Analysis of Variance) R实现

1, data0507 flower 是某种植物在两个海拔和两个气温下的开花高度&#xff0c;采用合适 的统计方法&#xff0c;检验该种植物的开花高度在不同的海拔之间和不同的气温之间有无差异&#xff1f;如果有差异&#xff0c;具体如何差异的&#xff1f;&#xff08;说明依据、结论等关…

GSVA,GSEA,KEGG,GO学习

目录 GSVA 1&#xff1a;获取注释基因集 2&#xff1a;运行 GSEA 1,示例数据集 2,运行 GSEA_KEGG富集分析 GSEA_GO富集分析 DO数据库GSEA MSigDB数据库选取GSEA KEGG 1&#xff1a;运行 2&#xff1a;绘图 bar图 气泡图 绘图美化 GO GSVA 1&#xff1a;获取注…

【网络通信】探索UDP与TCP协议、IP地址和端口号的奥妙

&#x1f33a;个人主页&#xff1a;Dawn黎明开始 &#x1f380;系列专栏&#xff1a;网络奇幻之旅 ⭐每日一句&#xff1a;往前走&#xff0c;朝着光 &#x1f4e2;欢迎大家&#xff1a;关注&#x1f50d;点赞&#x1f44d;评论&#x1f4dd;收藏⭐️ 文章目录 &#x1f4cb;前…

4.2 Windows驱动开发:内核中进程线程与模块

内核进程线程和模块是操作系统内核中非常重要的概念。它们是操作系统的核心部分&#xff0c;用于管理系统资源和处理系统请求。在驱动安全开发中&#xff0c;理解内核进程线程和模块的概念对于编写安全的内核驱动程序至关重要。 内核进程是在操作系统内核中运行的程序。每个进…

可燃气体监测仪|燃气管网监测解决办法

可燃气体监测仪是城市生命线中&#xff0c;燃气监测运行系统的前端监测设备&#xff0c;其主要作用是对燃气管网的安全状况进行实时监测。燃气管道在使用过程中&#xff0c;由于老化、裂纹、锈蚀等问题&#xff0c;容易导致燃气出现泄漏问题&#xff0c;从而引发一系列的安全事…

MySQL/Oracle用逗号分割的id怎么实现in (逗号分割的id字符串)。find_in_set(`id`, ‘1,2,3‘) 函数,

1.MySQL 1.1.正确写法 select * from student where find_in_set(s_id, 1,2,3); 1.2.错误示范 select * from student where find_in_set(s_id, 1,2 ,3); -- 注意&#xff0c;中间不能有空格。1、3 select * from student where find_in_set(s_id, 1,2, 3); -- 注意…

leetcode系列(双语)003——GO无重复字符的最长子串

文章目录 003、Longest Substring Without Repeating Characters个人解题官方解题扩展 003、Longest Substring Without Repeating Characters 无重复字符的最长子串 Given a string s, find the length of the longest substring without repeating characters. 给定一个字符…

解决:ERROR: No matching distribution found for PIL

解决&#xff1a;ERROR: No matching distribution found for PIL 背景 在搭建之前的代码环境时&#xff0c;报错&#xff1a; ERROR: Could not find a wersion that satisfies the requirement PIL&#xff08;from versions: none&#xff09; ERROR: No matching distribu…

wpf devexpress 创建布局

模板解决方案 例子是一个演示连接数据库连接程序。打开RegistrationForm.BaseProject项目和如下步骤 RegistrationForm.Lesson1 项目包含结果 审查Form设计 使用LayoutControl套件创建混合控件和布局 LayoutControl套件包含三个主控件&#xff1a; LayoutControl - 根布局…

【机器学习算法】机器学习:支持向量机(SVM)

转载自&#xff1a; 【精选】机器学习&#xff1a;支持向量机&#xff08;SVM&#xff09;-CSDN博客 1.概述 1.1&#xff0c;概念 支持向量机&#xff08;SVM&#xff09;是一类按监督学习方式对数据进行二元分类的广义线性分类器&#xff0c;其决策边界是对学习样本求解的最…