拼图小游戏(页面板)(实现游戏主界面)(未连接数据库)

 注释:

基于IDEA,创建窗体进行游戏

默认的用户名和密码为:zhangsan,123

                                        lisi,1234

App界面

package marchwho.ui;

public class App {
    public static void main(String[] args) {

        //登录的窗体
        new LogInJFrame();

        //注册的窗体
      //  new RegisterJFrame();

        //游戏主界面的窗体
      //  new GameJFrame();

    }
}

游戏主界面

package marchwho.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;

public class GameJFrame extends JFrame implements KeyListener, ActionListener {
    //游戏主界面

    //创建胜利的二位数组
    int[][] win = new int[][]{
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 0}

    };

    //创建一个二维数组
    //用于打乱图片
    int[][] data = new int[4][4];

    //用于空白图片的移动
    int x = 0;
    int y = 0;

    //加载图片的路径
    String path = "../puzzleagame/image/girl/girl1/";

    //统计步数
    int step = 0;

    //创建选项下面的条目
    JMenuItem replayItem = new JMenuItem("重新游戏");
    JMenuItem reLoginItem = new JMenuItem("重新登录");
    JMenuItem closeItem = new JMenuItem("关闭游戏");

    JMenuItem codeItem = new JMenuItem("作者");
    //创建更换图片下的条目
    JMenuItem GirlItem = new JMenuItem("美女");
    JMenuItem AnimalItem = new JMenuItem("动物");
    JMenuItem SportItem = new JMenuItem("运动");
    //创建帮助下面的条目
    JMenuItem AItem = new JMenuItem("按A查看完整图片");

    public GameJFrame() {
        //初始化界面
        initJFrame();

        //初始化菜单
        initJMenuBar();

        //打乱图片
        initData();

        //初始化图片
        initImage();

        //让界面可以显示出来
        this.setVisible(true);
    }

    private void initData() {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0};

        Random r = new Random();

        //将一维数组打乱
        for (int i = 0; i < arr.length; i++) {
            int index = r.nextInt(arr.length);

            int temp = arr[i];
            arr[i] = arr[index];
            arr[index] = temp;


        }

        //将打乱的一维数组依次放入二维数组中
        for (int i = 0; i < arr.length; i++) {
            //获得空白图片所对应的位置
            if (arr[i] == 0) {
                x = i / 4;
                y = i % 4;
            }
            data[i / 4][i % 4] = arr[i];
        }

    }

    private void initImage() {
        //清空已经出现的所有图片
        this.getContentPane().removeAll();

        //添加胜利的图标
        if (victory()) {
            ImageIcon winicon = new ImageIcon("../puzzleagame/image/win.png");
            JLabel winJLabel = new JLabel(winicon);
            winJLabel.setBounds(203, 283, 197, 73);
            this.getContentPane().add(winJLabel);
        }

        JLabel Step = new JLabel("步数" + step);
        Step.setBounds(50, 30, 100, 20);
        this.getContentPane().add(Step);

        for (int j = 0; j < 4; j++) {
            //外循环---将内循环执行四次,即添加四行图片
            for (int i = 0; i < 4; i++) {
                //内循环---将一行的图片进行添加

                //将打乱的二维数组所对应的数字将图片进行打乱
                int number = data[j][i];

                //创建一个图片ImageIcon对象
                ImageIcon icon = new ImageIcon(path + number + ".jpg");
                //创建一个JLabel对象(管理容器)
                JLabel jLabel = new JLabel(icon);
                //指定图片位置
                jLabel.setBounds(105 * i + 83, 105 * j + 134, 105, 105);

                //添加图片边框
                //0---凸出来
                //1---凹出来
                jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));

                //获取隐藏窗口,通过XY轴设置图片的位置与图片的大小
                this.getContentPane().add(jLabel);

            }
        }

        //添加背景图片
        ImageIcon background = new ImageIcon("../puzzleagame/image/background.png");
        JLabel BackGround = new JLabel(background);
        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 helpJMenu = new JMenu("帮助");
        JMenu changeJMenu = new JMenu("更换图片");

        //将条目添加到选项中
        functionJMenu.add(changeJMenu);

        changeJMenu.add(GirlItem);
        changeJMenu.add(AnimalItem);
        changeJMenu.add(SportItem);

        helpJMenu.add(AItem);

        functionJMenu.add(replayItem);
        functionJMenu.add(reLoginItem);
        functionJMenu.add(closeItem);

        aboutJMenu.add(codeItem);

        //给条目绑定事件
        replayItem.addActionListener(this);
        reLoginItem.addActionListener(this);
        closeItem.addActionListener(this);
        codeItem.addActionListener(this);

        GirlItem.addActionListener(this);
        AnimalItem.addActionListener(this);
        SportItem.addActionListener(this);

        //将选项添加到菜单中
        jMenuBar.add(functionJMenu);
        jMenuBar.add(aboutJMenu);
        jMenuBar.add(helpJMenu);

        //给整个界面设置菜单
        this.setJMenuBar(jMenuBar);
    }

    private void initJFrame() {
        //设置界面的宽高
        this.setSize(603, 680);
        //设置界面的标题
        this.setTitle("拼图游戏 V1.0");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中,处于屏幕中间
        this.setLocationRelativeTo(null);
        //设置关闭模式
        //关闭之后虚拟机也会停止运行
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //创建键盘监听
        this.addKeyListener(this);

        //取消内部默认居中格式
        this.setLayout(null);
    }

    @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);

            //添加背景图片
            ImageIcon background = new ImageIcon("../puzzleagame/image/background.png");
            JLabel BackGround = new JLabel(background);
            BackGround.setBounds(40, 40, 508, 560);
            this.getContentPane().add(BackGround);

            //刷新界面
            this.getContentPane().repaint();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        //判断如果胜利则将不能移动
        if (victory()) {
            return;
        }

        //上:38 下:40 左:37 右:39
        int code = e.getKeyCode();
        //上移
        if (code == 38) {
            if (x == 0) {
                return;
            }
            System.out.println("向左移动");
            data[x][y] = data[x - 1][y];
            data[x - 1][y] = 0;
            x--;

            initImage();

            step++;
        }
        //下移
        else if (code == 40) {
            if (x == 3) {
                return;
            }
            System.out.println("向右移动");
            data[x][y] = data[x + 1][y];
            data[x + 1][y] = 0;
            x++;

            initImage();

            step++;
        }
        //左移
        else if (code == 37) {
            if (y == 0) {
                return;
            }
            System.out.println("向上移动");
            data[x][y] = data[x][y - 1];
            data[x][y - 1] = 0;
            y--;

            initImage();

            step++;
        }
        //右移
        else if (code == 39) {
            if (y == 3) {
                return;
            }
            System.out.println("向下移动");
            data[x][y] = data[x][y + 1];
            data[x][y + 1] = 0;
            y++;

            initImage();

            step++;
        } else if (code == 65) {
            initImage();
        } else if (code == 81) {
            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++) {
            for (int j = 0; j < data[i].length; j++) {
                if (data[i][j] != win[i][j]) {
                    //只要有一个不一样,就返回false
                    return false;
                }

            }
        }
        return true;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (source == replayItem) {
            System.out.println("重新游戏");

            //步数清零
            step = 0;

            //重新打乱二维数组
            initData();

            //重新加载图片
            initImage();

        } else if (source == reLoginItem) {
            System.out.println("重新登录");
            //关闭游戏界面
            this.setVisible(false);
            //返回登录
            new LogInJFrame();

        } else if (source == closeItem) {
            System.out.println("关闭游戏");
            //关闭虚拟机
            System.exit(0);

        } else if (source == codeItem) {
            System.out.println("作者");

            //创建弹框对象
            JDialog jDialog = new JDialog();
            //创建一个管理容器的对象
            ImageIcon icon = new ImageIcon();
            JLabel jLabel = new JLabel(icon);
            jLabel.setBounds(0, 0, 258, 258);
            //将图片放到弹框中
            jDialog.getContentPane().add(jLabel);

            //设置弹框大小
            jDialog.setSize(344, 344);
            //设置弹框置顶
            jDialog.setAlwaysOnTop(true);
            //设置弹框居中
            jDialog.setLocationRelativeTo(null);
            //弹框不关闭无法再进行操作
            jDialog.setModal(true);
            //显示弹框
            jDialog.setVisible(true);
        } else if (source == GirlItem) {
            System.out.println("美女");

            //获得随机的索引代表不同的美女图片
            Random r  =new Random();
            int index = r.nextInt(13);

            //加载图片的路径
            path = "../puzzleagame/image/girl/girl"+index+"/";

            //步数清零
            step = 0;

            //打乱图片
            initImage();

        }else if (source == AnimalItem) {
            System.out.println("动物");

            //获得随机的索引代表不同的动物图片
            Random r  =new Random();
            int index = r.nextInt(8);

            //加载图片的路径
            path = "../puzzleagame/image/animal/animal"+index+"/";

            //步数清零
            step = 0;

            //打乱图片
            initImage();

        }else if (source == SportItem) {
            System.out.println("运动");

            //获得随机的索引代表不同的运动图片
            Random r  =new Random();
            int index = r.nextInt(10);

            //加载图片的路径
            path = "../puzzleagame/image/sport/sport"+index+"/";

            //步数清零
            step = 0;

            //打乱图片
            initImage();

        }
    }
}

登录界面

package marchwho.ui;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

public class LogInJFrame extends JFrame implements MouseListener {
    //登录界面

    //创建一个集合存储正确的用户名和密码
    static ArrayList<User> list = new ArrayList<>();

    static {
        list.add(new User("zhangsan", "123"));
        list.add(new User("lisi", "1234"));
    }


    //2.添加用户名输入框
    JTextField username = new JTextField();
    //4.密码输入框
    JPasswordField password = new JPasswordField();
    //验证码的输入框
    JTextField code = new JTextField();

    //5.添加登录按钮
    JButton login = new JButton();
    //6.添加注册按钮
    JButton register = new JButton();
    //生成的验证码
    JLabel rightCode = new JLabel();


    public LogInJFrame() {
        //初始化界面
        initJFrame();

        //在这个界面中添加内容
        initView();

        //让当前界面显示出来
        this.setVisible(true);
    }

    public void initView() {
        //1. 添加用户名文字
        JLabel usernameText = new JLabel(new ImageIcon("D:\\IDEA\\puzzleagame\\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("D:\\IDEA\\puzzleagame\\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("D:\\IDEA\\puzzleagame\\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.setBounds(300, 256, 50, 30);
        //添加到界面
        this.getContentPane().add(rightCode);

        //5.添加登录按钮
        login.setBounds(123, 310, 128, 47);
        login.setIcon(new ImageIcon("D:\\IDEA\\puzzleagame\\image\\login\\登录按钮.png"));
        //去除按钮的默认边框
        login.setBorderPainted(false);
        //去除按钮的默认背景
        login.setContentAreaFilled(false);
        this.getContentPane().add(login);

        //6.添加注册按钮
        register.setBounds(256, 310, 128, 47);
        register.setIcon(new ImageIcon("D:\\IDEA\\puzzleagame\\image\\login\\注册按钮.png"));
        //去除按钮的默认边框
        register.setBorderPainted(false);
        //去除按钮的默认背景
        register.setContentAreaFilled(false);
        this.getContentPane().add(register);

        //7.添加背景图片
        JLabel background = new JLabel(new ImageIcon("D:\\IDEA\\puzzleagame\\image\\login\\background.png"));
        background.setBounds(0, 0, 470, 390);
        this.getContentPane().add(background);

        //设置注册、登录、验证码的鼠标监听
        register.addMouseListener(this);
        login.addMouseListener(this);
        rightCode.addMouseListener(this);
    }


    public void initJFrame() {
        //设置界面的宽高
        this.setSize(488, 430);
        //设置界面的标题
        this.setTitle("拼图游戏 登录");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中,处于屏幕中间
        this.setLocationRelativeTo(null);
        //设置关闭模式
        //关闭之后虚拟机也会停止运行
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


        //让界面可以显示出来
        this.setVisible(true);
    }

    //创建弹窗显示
    //要展示用户名或密码错误
    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);
    }

    //点击
    @Override
    public void mouseClicked(MouseEvent e) {
        Object source = e.getSource();
        if (source == login) {
            System.out.println("点击登录按钮");
            //先获取用户输入的用户名、密码、验证码
            String usernameText = username.getText();
            String passwordText = password.getText();
            String codeText = code.getText();

            //先判断输入是否为空
            if (usernameText.length() == 0 || passwordText.length() == 0) {
                showJDialog("用户名或密码不能为空");
            } else if (codeText.length() == 0) {
                showJDialog("验证码不能为空");
            }
            //不为空,开始判断
            else {
                if (compare(list, usernameText, passwordText)) {
                    if (codeText.equals(rightCode.getText())) {
                        showJDialog("登录成功");
                        //关闭登录界面
                        this.setVisible(false);
                        //打开游戏主界面
                        new GameJFrame();
                    } else {
                        showJDialog("验证码输入错误");
                    }
                } else {
                    showJDialog("用户名或密码输入错误");
                }


            }


        } else if (source == register) {
            System.out.println("点击注册按钮");
        } else if (source == rightCode) {
            System.out.println("点击验证码,重新获取验证码");
            String codeStr = CodeUtil.getCode();
            rightCode.setText(codeStr);

        }


    }

    //判断用户名是否存在
    public static boolean compare(ArrayList<User> list, String usernameText, String passwordText) {
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            if (user.getusername().equals(usernameText) && user.getpassword().equals(passwordText)) {
                return true;
            }
        }
        return false;
    }


    //按下不松
    @Override
    public void mousePressed(MouseEvent e) {

        Object source = e.getSource();
        if (source == register) {
            //设置点击注册之后的颜色加深
            //相当于换一个图片
            register.setIcon(new ImageIcon("D:\\IDEA\\puzzleagame\\image\\login\\注册按下.png"));
        } else if (source == login) {
            //设置点击登录之后的颜色加深
            //相当于换一个图片
            login.setIcon(new ImageIcon("D:\\IDEA\\puzzleagame\\image\\login\\登录按下.png"));
        }


    }

    //松开
    @Override
    public void mouseReleased(MouseEvent e) {
        Object source = e.getSource();
        if (source == register) {
            //变回原来的图片
            register.setIcon(new ImageIcon("D:\\IDEA\\puzzleagame\\image\\login\\注册按钮.png"));
        } else if (source == login) {
            //变回原来的图片
            login.setIcon(new ImageIcon("D:\\IDEA\\puzzleagame\\image\\login\\登录按钮.png"));
        }


    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

}

登录界面所需要的User类

package marchwho.ui;

public class User {
    private String username;
    private String password;

    public User() {
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getusername() {
        return username;
    }

    public void setusername(String username) {
        this.username = username;
    }

    public String getpassword() {
        return password;
    }

    public void setpassword(String password) {
        this.password = password;
    }
}

 生成验证码的工具类

package marchwho.ui;

import java.util.Random;

public class CodeUtil {
    //工具类
    //生成验证码
    public CodeUtil() {
    }

    //生成一个随机的验证码
    //四个字母+一个数字
    //数字位置随机
    //将生成的字符数组内容放到一个字符串里面

    public static String getCode() {
        String result = changeLetter();

        return result;
    }


    public static String changeLetter() {
        char[] code = getArr();

        String getcode = "";
        for (int i = 0; i < code.length; i++) {
            getcode = getcode + code[i];
        }
        return getcode;
    }


    public static char[] getArr() {
        //创建一个字符串数组,长度为5
        char[] arr = new char[5];

        Random r = new Random();

        //获得随机的一个数字
        int number = r.nextInt(9);
        //获得随机的一个字母数组
        char[] letter = getLetter();

        String result = "";
        //先将数字放在最后一位
        for (int i = 0; i < arr.length - 1; i++) {
            int letterNumber = r.nextInt(52);
            result = result + letter[letterNumber];
        }
        result = result + number;

        //将数字索引进行打乱
        int numberIndex = r.nextInt(arr.length - 1);
        //将结果变成一个字符串数组
        for (int i = 0; i < result.length(); i++) {
            char c = result.charAt(i);
            arr[i] = c;
        }

        //将最后一个索引的内容与前面交换
        char temp = arr[arr.length - 1];
        arr[arr.length - 1] = arr[numberIndex];
        arr[numberIndex] = temp;

        return arr;
    }

    //获得随机的一个字母数组
    public static char[] getLetter() {
        char[] arr = new char[52];
        for (int i = 0; i < arr.length; i++) {
            if (i <= 25) {
                arr[i] = (char) (97 + i);
            } else {
                arr[i] = (char) (65 + i - 26);
            }
        }
        return arr;
    }
}

  • 28
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Marchwho

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值