Java拼图游戏

Java拼图游戏

2301_76549057

于 2023-11-19 04:43:05 发布

阅读量345

 收藏

点赞数

文章标签: java 开发语言

版权

运行出的游戏界面如下:

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

User类

 
  1. package domain;

  2. /**

  3. * @ClassName: User

  4. * @Author: Kox

  5. * @Data: 2023/2/2

  6. * @Sketch:

  7. */

  8. public class User {

  9. private String username;

  10. private String password;

  11. public User() {

  12. }

  13. public User(String username, String password) {

  14. this.username = username;

  15. this.password = password;

  16. }

  17. /**

  18. * 获取

  19. * @return username

  20. */

  21. public String getUsername() {

  22. return username;

  23. }

  24. /**

  25. * 设置

  26. * @param username

  27. */

  28. public void setUsername(String username) {

  29. this.username = username;

  30. }

  31. /**

  32. * 获取

  33. * @return password

  34. */

  35. public String getPassword() {

  36. return password;

  37. }

  38. /**

  39. * 设置

  40. * @param password

  41. */

  42. public void setPassword(String password) {

  43. this.password = password;

  44. }

  45. }

CodeUtil类

 
  1. package util;

  2. import java.util.ArrayList;

  3. import java.util.Random;

  4. public class CodeUtil {

  5. public static String getCode(){

  6. //1.创建一个集合

  7. ArrayList<Character> list = new ArrayList<>();//52 索引的范围:0 ~ 51

  8. //2.添加字母 a - z A - Z

  9. for (int i = 0; i < 26; i++) {

  10. list.add((char)('a' + i));//a - z

  11. list.add((char)('A' + i));//A - Z

  12. }

  13. //3.打印集合

  14. //System.out.println(list);

  15. //4.生成4个随机字母

  16. String result = "";

  17. Random r = new Random();

  18. for (int i = 0; i < 4; i++) {

  19. //获取随机索引

  20. int randomIndex = r.nextInt(list.size());

  21. char c = list.get(randomIndex);

  22. result = result + c;

  23. }

  24. //System.out.println(result);//长度为4的随机字符串

  25. //5.在后面拼接数字 0~9

  26. int number = r.nextInt(10);

  27. //6.把随机数字拼接到result的后面

  28. result = result + number;

  29. //System.out.println(result);//ABCD5

  30. //7.把字符串变成字符数组

  31. char[] chars = result.toCharArray();//[A,B,C,D,5]

  32. //8.在字符数组中生成一个随机索引

  33. int index = r.nextInt(chars.length);

  34. //9.拿着4索引上的数字,跟随机索引上的数字进行交换

  35. char temp = chars[4];

  36. chars[4] = chars[index];

  37. chars[index] = temp;

  38. //10.把字符数组再变回字符串

  39. String code = new String(chars);

  40. //System.out.println(code);

  41. return code;

  42. }

  43. }

登陆代码

 
  1. package ui;

  2. import domain.User;

  3. import util.CodeUtil;

  4. import javax.swing.*;

  5. import java.awt.event.MouseEvent;

  6. import java.awt.event.MouseListener;

  7. import java.io.BufferedReader;

  8. import java.io.File;

  9. import java.io.FileReader;

  10. import java.util.ArrayList;

  11. public class LoginJFrame extends JFrame implements MouseListener {

  12. static ArrayList<User> allUsers = new ArrayList<>();

  13. static {

  14. allUsers.add(new User("zhangsan","123"));

  15. allUsers.add(new User("lisi","1234"));

  16. }

  17. JButton login = new JButton();

  18. JButton register = new JButton();

  19. JTextField username = new JTextField();

  20. //JTextField password = new JTextField();

  21. JPasswordField password = new JPasswordField();

  22. JTextField code = new JTextField();

  23. //正确的验证码

  24. JLabel rightCode = new JLabel();

  25. public LoginJFrame() {

  26. //初始化界面

  27. initJFrame();

  28. //在这个界面中添加内容

  29. initView();

  30. //让当前界面显示出来

  31. this.setVisible(true);

  32. }

  33. public void initView() {

  34. //1. 添加用户名文字

  35. JLabel usernameText = new JLabel(new ImageIcon("拼图小游戏_image\\image\\login\\用户名.png"));

  36. usernameText.setBounds(116, 135, 47, 17);

  37. this.getContentPane().add(usernameText);

  38. //2.添加用户名输入框

  39. username.setBounds(195, 134, 200, 30);

  40. this.getContentPane().add(username);

  41. //3.添加密码文字

  42. JLabel passwordText = new JLabel(new ImageIcon("拼图小游戏_image\\image\\login\\密码.png"));

  43. passwordText.setBounds(130, 195, 32, 16);

  44. this.getContentPane().add(passwordText);

  45. //4.密码输入框

  46. password.setBounds(195, 195, 200, 30);

  47. this.getContentPane().add(password);

  48. //验证码提示

  49. JLabel codeText = new JLabel(new ImageIcon("拼图小游戏_image\\image\\login\\验证码.png"));

  50. codeText.setBounds(133, 256, 50, 30);

  51. this.getContentPane().add(codeText);

  52. //验证码的输入框

  53. code.setBounds(195, 256, 100, 30);

  54. this.getContentPane().add(code);

  55. String codeStr = CodeUtil.getCode();

  56. //设置内容

  57. rightCode.setText(codeStr);

  58. //绑定鼠标事件

  59. rightCode.addMouseListener(this);

  60. //位置和宽高

  61. rightCode.setBounds(300, 256, 50, 30);

  62. //添加到界面

  63. this.getContentPane().add(rightCode);

  64. //5.添加登录按钮

  65. login.setBounds(123, 310, 128, 47);

  66. login.setIcon(new ImageIcon("拼图小游戏_image\\image\\login\\登录按钮.png"));

  67. //去除按钮的边框

  68. login.setBorderPainted(false);

  69. //去除按钮的背景

  70. login.setContentAreaFilled(false);

  71. //给登录按钮绑定鼠标事件

  72. login.addMouseListener(this);

  73. this.getContentPane().add(login);

  74. //6.添加注册按钮

  75. register.setBounds(256, 310, 128, 47);

  76. register.setIcon(new ImageIcon("拼图小游戏_image\\image\\login\\注册按钮.png"));

  77. //去除按钮的边框

  78. register.setBorderPainted(false);

  79. //去除按钮的背景

  80. register.setContentAreaFilled(false);

  81. //给注册按钮绑定鼠标事件

  82. register.addMouseListener(this);

  83. this.getContentPane().add(register);

  84. //7.添加背景图片

  85. JLabel background = new JLabel(new ImageIcon("拼图小游戏_image\\image\\login\\background.png"));

  86. background.setBounds(0, 0, 470, 390);

  87. this.getContentPane().add(background);

  88. }

  89. public void initJFrame() {

  90. this.setSize(488, 430);//设置宽高

  91. this.setTitle("拼图游戏 V1.0登录");//设置标题

  92. this.setDefaultCloseOperation(3);//设置关闭模式

  93. this.setLocationRelativeTo(null);//居中

  94. this.setAlwaysOnTop(true);//置顶

  95. this.setLayout(null);//取消内部默认布局

  96. }

  97. //点击

  98. @Override

  99. public void mouseClicked(MouseEvent e) {

  100. if (e.getSource() == login) {

  101. System.out.println("点击了登录按钮");

  102. //获取两个文本输入框中的内容

  103. String usernameInput = username.getText();

  104. String passwordInput = password.getText();

  105. //获取用户输入的验证码

  106. String codeInput = code.getText();

  107. //创建一个User对象

  108. User userInfo = new User(usernameInput, passwordInput);

  109. System.out.println("用户输入的用户名为" + usernameInput);

  110. System.out.println("用户输入的密码为" + passwordInput);

  111. if (codeInput.length() == 0) {

  112. showJDialog("验证码不能为空");

  113. } else if (usernameInput.length() == 0 || passwordInput.length() == 0) {

  114. //校验用户名和密码是否为空

  115. System.out.println("用户名或者密码为空");

  116. //调用showJDialog方法并展示弹框

  117. showJDialog("用户名或者密码为空");

  118. } else if (!codeInput.equalsIgnoreCase(rightCode.getText())) {

  119. showJDialog("验证码输入错误");

  120. } else if (contains(userInfo)) {

  121. System.out.println("用户名和密码正确可以开始玩游戏了");

  122. //关闭当前登录界面

  123. this.setVisible(false);

  124. //打开游戏的主界面

  125. //需要把当前登录的用户名传递给游戏界面

  126. new GameJFrame();

  127. } else {

  128. System.out.println("用户名或密码错误");

  129. showJDialog("用户名或密码错误");

  130. }

  131. } else if (e.getSource() == register) {

  132. System.out.println("点击了注册按钮");

  133. } else if (e.getSource() == rightCode) {

  134. System.out.println("更换验证码");

  135. //获取一个新的验证码

  136. String code = CodeUtil.getCode();

  137. rightCode.setText(code);

  138. }

  139. }

  140. public void showJDialog(String content) {

  141. //创建一个弹框对象

  142. JDialog jDialog = new JDialog();

  143. //给弹框设置大小

  144. jDialog.setSize(200, 150);

  145. //让弹框置顶

  146. jDialog.setAlwaysOnTop(true);

  147. //让弹框居中

  148. jDialog.setLocationRelativeTo(null);

  149. //弹框不关闭永远无法操作下面的界面

  150. jDialog.setModal(true);

  151. //创建Jlabel对象管理文字并添加到弹框当中

  152. JLabel warning = new JLabel(content);

  153. warning.setBounds(0, 0, 200, 150);

  154. jDialog.getContentPane().add(warning);

  155. //让弹框展示出来

  156. jDialog.setVisible(true);

  157. }

  158. //按下不松

  159. @Override

  160. public void mousePressed(MouseEvent e) {

  161. if (e.getSource() == login) {

  162. login.setIcon(new ImageIcon("拼图小游戏_image\\image\\login\\登录按下.png"));

  163. } else if (e.getSource() == register) {

  164. register.setIcon(new ImageIcon("拼图小游戏_image\\image\\login\\注册按下.png"));

  165. }

  166. }

  167. //松开按钮

  168. @Override

  169. public void mouseReleased(MouseEvent e) {

  170. if (e.getSource() == login) {

  171. login.setIcon(new ImageIcon("jigsawgame\\image\\login\\登录按钮.png"));

  172. } else if (e.getSource() == register) {

  173. register.setIcon(new ImageIcon("jigsawgame\\image\\login\\注册按钮.png"));

  174. }

  175. }

  176. //鼠标划入

  177. @Override

  178. public void mouseEntered(MouseEvent e) {

  179. }

  180. //鼠标划出

  181. @Override

  182. public void mouseExited(MouseEvent e) {

  183. }

  184. //判断用户在集合中是否存在

  185. public boolean contains(User userInput){

  186. for (int i = 0; i < allUsers.size(); i++) {

  187. User rightUser = allUsers.get(i);

  188. if(userInput.getUsername().equals(rightUser.getUsername()) && userInput.getPassword().equals(rightUser.getPassword())){

  189. //有相同的代表存在,返回true,后面的不需要再比了

  190. return true;

  191. }

  192. }

  193. //循环结束之后还没有找到就表示不存在

  194. return false;

  195. }

  196. }

 注册代码

 
  1. package ui;

  2. import domain.User;

  3. import util.CodeUtil;

  4. import javax.swing.*;

  5. import java.awt.event.MouseEvent;

  6. import java.awt.event.MouseListener;

  7. import java.io.*;

  8. import java.util.ArrayList;

  9. import java.util.List;

  10. public class RegisterJFrame extends JFrame implements MouseListener {

  11. ArrayList<User> list = new ArrayList<>();

  12. //提升三个输入框的变量的作用范围,让这三个变量可以在本类中所有方法里面可以使用。

  13. JTextField username = new JTextField();

  14. JTextField password = new JTextField();

  15. JTextField rePassword = new JTextField();

  16. //提升两个按钮变量的作用范围,让这两个变量可以在本类中所有方法里面可以使用。

  17. JButton submit = new JButton();

  18. JButton reset = new JButton();

  19. public RegisterJFrame() throws IOException {

  20. user();

  21. initFrame();

  22. initView();

  23. setVisible(true);

  24. }

  25. public void user() throws IOException {

  26. File file = new File("user.txt");

  27. file.createNewFile();

  28. BufferedReader br = new BufferedReader(new FileReader("user.txt"));

  29. String str;

  30. while ((str = br.readLine()) != null) {

  31. String[] user = str.split("&");

  32. //name

  33. String name = user[0].split("=")[1];

  34. //password

  35. String password = user[1].split("=")[1];

  36. list.add(new User(name, password));

  37. }

  38. }

  39. @Override

  40. public void mouseClicked(MouseEvent e) {

  41. //获取输入框中的内容

  42. String userNameStr = username.getText();

  43. String passWordStr = password.getText();

  44. String rePasswordText = rePassword.getText();

  45. if (e.getSource() == submit){//注册

  46. System.out.println("注册");

  47. //判断输入框是否有空

  48. if ((userNameStr.length() == 0) || (passWordStr.length() == 0) || (rePasswordText.length() == 0)){

  49. showDialog("账号或密码不能为空");

  50. //清空密码

  51. password.setText("");

  52. rePassword.setText("");

  53. } else if (!passWordStr.equals(rePasswordText)) {

  54. showDialog("密码不一致");

  55. //清空密码

  56. rePassword.setText("");

  57. } else if (!tfUsername(userNameStr)) { //账户已存在

  58. showDialog("账号已存在");

  59. } else {

  60. try {

  61. //将数据存入本地文件中

  62. User(userNameStr,passWordStr);

  63. showDialog("注册成功");

  64. this.setVisible(false);

  65. new LoginJFrame();

  66. } catch (IOException ex) {

  67. throw new RuntimeException(ex);

  68. }

  69. }

  70. //

  71. }else if(e.getSource() == reset){//重置

  72. System.out.println("重置");

  73. password.setText("");

  74. rePassword.setText("");

  75. username.setText("");

  76. }

  77. }

  78. /*

  79. * 将数据账号数据存入本地文件中

  80. * 参数1:账号 参数2:密码

  81. * */

  82. private void User(String name , String password) throws IOException {

  83. String user = "name="+name+"&password="+password;

  84. BufferedWriter bw = new BufferedWriter(new FileWriter("user.txt",true));

  85. bw.write(user);

  86. bw.newLine();

  87. bw.close();

  88. }

  89. /*

  90. * 检测账号是否存在

  91. * 返回值 boolean

  92. * 传入 username

  93. * */

  94. private boolean tfUsername(String userName){

  95. for (User user : list) {

  96. if(user.getUsername().equals(userName))

  97. return false;

  98. }

  99. return true;

  100. }

  101. @Override

  102. public void mousePressed(MouseEvent e) {

  103. if (e.getSource() == submit) {

  104. submit.setIcon(new ImageIcon("拼图小游戏_image\\image\\register\\注册按下.png"));

  105. } else if (e.getSource() == reset) {

  106. reset.setIcon(new ImageIcon("拼图小游戏_image\\image\\register\\重置按下.png"));

  107. }

  108. }

  109. @Override

  110. public void mouseReleased(MouseEvent e) {

  111. if (e.getSource() == submit) {

  112. submit.setIcon(new ImageIcon("拼图小游戏_image\\image\\register\\注册按钮.png"));

  113. } else if (e.getSource() == reset) {

  114. reset.setIcon(new ImageIcon("拼图小游戏_image\\image\\register\\重置按钮.png"));

  115. }

  116. }

  117. @Override

  118. public void mouseEntered(MouseEvent e) {

  119. }

  120. @Override

  121. public void mouseExited(MouseEvent e) {

  122. }

  123. private void initView() {

  124. //添加注册用户名的文本

  125. JLabel usernameText = new JLabel(new ImageIcon("拼图小游戏_image\\image\\login\\用户名.png"));

  126. usernameText.setBounds(85, 135, 80, 20);

  127. //添加注册用户名的输入框

  128. username.setBounds(195, 134, 200, 30);

  129. //添加注册密码的文本

  130. JLabel passwordText = new JLabel(new ImageIcon("拼图小游戏_image\\image\\register\\注册密码.png"));

  131. passwordText.setBounds(97, 193, 70, 20);

  132. //添加密码输入框

  133. password.setBounds(195, 195, 200, 30);

  134. //添加再次输入密码的文本

  135. JLabel rePasswordText = new JLabel(new ImageIcon("拼图小游戏_image\\image\\register\\再次输入密码.png"));

  136. rePasswordText.setBounds(64, 255, 95, 20);

  137. //添加再次输入密码的输入框

  138. rePassword.setBounds(195, 255, 200, 30);

  139. //注册的按钮

  140. submit.setIcon(new ImageIcon("拼图小游戏_image\\image\\register\\注册按钮.png"));

  141. submit.setBounds(123, 310, 128, 47);

  142. submit.setBorderPainted(false);

  143. submit.setContentAreaFilled(false);

  144. submit.addMouseListener(this);

  145. //重置的按钮

  146. reset.setIcon(new ImageIcon("拼图小游戏_image\\image\\register\\重置按钮.png"));

  147. reset.setBounds(256, 310, 128, 47);

  148. reset.setBorderPainted(false);

  149. reset.setContentAreaFilled(false);

  150. reset.addMouseListener(this);

  151. //背景图片

  152. JLabel background = new JLabel(new ImageIcon("拼图小游戏_image\\image\\background.png"));

  153. background.setBounds(0, 0, 470, 390);

  154. this.getContentPane().add(usernameText);

  155. this.getContentPane().add(passwordText);

  156. this.getContentPane().add(rePasswordText);

  157. this.getContentPane().add(username);

  158. this.getContentPane().add(password);

  159. this.getContentPane().add(rePassword);

  160. this.getContentPane().add(submit);

  161. this.getContentPane().add(reset);

  162. this.getContentPane().add(background);

  163. }

  164. private void initFrame() {

  165. //对自己的界面做一些设置。

  166. //设置宽高

  167. setSize(488, 430);

  168. //设置标题

  169. setTitle("拼图游戏 V1.0注册");

  170. //取消内部默认布局

  171. setLayout(null);

  172. //设置关闭模式

  173. setDefaultCloseOperation(3);

  174. //设置居中

  175. setLocationRelativeTo(null);

  176. //设置置顶

  177. setAlwaysOnTop(true);

  178. }

  179. //只创建一个弹框对象

  180. JDialog jDialog = new JDialog();

  181. //因为展示弹框的代码,会被运行多次

  182. //所以,我们把展示弹框的代码,抽取到一个方法中。以后用到的时候,就不需要写了

  183. //直接调用就可以了。

  184. public void showDialog(String content){

  185. if(!jDialog.isVisible()){

  186. //把弹框中原来的文字给清空掉。

  187. jDialog.getContentPane().removeAll();

  188. JLabel jLabel = new JLabel(content);

  189. jLabel.setBounds(0,0,200,150);

  190. jDialog.add(jLabel);

  191. //给弹框设置大小

  192. jDialog.setSize(200, 150);

  193. //要把弹框在设置为顶层 -- 置顶效果

  194. jDialog.setAlwaysOnTop(true);

  195. //要让jDialog居中

  196. jDialog.setLocationRelativeTo(null);

  197. //让弹框

  198. jDialog.setModal(true);

  199. //让jDialog显示出来

  200. jDialog.setVisible(true);

  201. }

  202. }

  203. }

游戏代码

 
  1. import java.io.IOException;

  2. import ui.GameJFrame;

  3. import ui.LoginJFrame;

  4. import ui.RegisterJFrame;

  5. /**

  6. * @ClassName: App

  7. * @Author: Kox

  8. * @Data: 2023/1/30

  9. * @Sketch:

  10. */

  11. public class App {

  12. public static void main(String[] args) throws IOException {

  13. // 登录界面

  14. new LoginJFrame();

  15. // 注册界面

  16. new RegisterJFrame();

  17. // 游戏界面

  18. // new GameJFrame();

  19. }

  20. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值