JAVA学习第十二章——拼图游戏主体界面

拼图游戏

拼图游戏主体界面
import com.ituser.ui.GameJframe;
import com.ituser.ui.LoginJframe;
import com.ituser.ui.RegisterJframe;

public class App {
    public static void main(String[] args) {
        //程序启动入口
        new GameJframe();
        //new LoginJframe();
        //new RegisterJframe();
    }
}

package com.ituser.ui;

import jdk.swing.interop.LightweightContentWrapper;

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[][] data = new int[4][4];
    //记录空白方块的位置
    int x = 0;
    int y = 0;

    String path = "C:\\Users\\MIKU\\IdeaProjects\\try-one\\image\\girl\\girl3\\";

    //定义一个二位数组,存储正确的数据
    int[][] win = {
            {1, 5, 9, 13},
            {2, 6, 10, 14},
            {3, 7, 11, 15},
            {4, 8, 12, 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;
        }

        int index = 0;

        //遍历二维数组
        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];
        }

    }

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

        if (victory()) {
            JLabel winJlabel = new JLabel(new ImageIcon("C:\\Users\\MIKU\\IdeaProjects\\try-one\\image\\win.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);

        //外循环
        for (int j = 0; j < 4; j++) {

            //内循环
            for (int i = 0; i < 4; i++) {
                int num = data[i][j];
                //创建一个JLabel的对象(容器)
                JLabel jLabel = new JLabel(new ImageIcon(path + num + ".jpg"));

                //指定位置
                jLabel.setBounds(105 * i + 83, 105 * j + 134, 105, 105);

                //给图片添加边框
                jLabel.setBorder(new BevelBorder(0));

                //把容器添加到界面中
                this.getContentPane().add(jLabel);

            }
        }

        //添加背景图片
        JLabel background = new JLabel(new ImageIcon("C:\\Users\\MIKU\\IdeaProjects\\try-one\\image\\background.png"));
        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("关于我们");

        //条目添加到选项中
        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(603, 680);
        //设置界面标题
        this.setTitle("拼图游戏");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面位置
        this.setLocationRelativeTo(null);
        //添加键盘监听事件
        this.addKeyListener(this);
        //设置关闭模式
        this.setDefaultCloseOperation(3);

        //取消默认的居中放置
        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);

            //添加背景图片
            JLabel background = new JLabel(new ImageIcon("C:\\Users\\MIKU\\IdeaProjects\\try-one\\image\\background.png"));
            background.setBounds(40, 40, 508, 560);
            this.getContentPane().add(background);

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

    //键盘移动
    @Override
    public void keyReleased(KeyEvent e) {
        //判断游戏是否胜利
        if (victory()) {
            //结束方法
            return;
        }

        //对上下左右进行判断
        //左:37 上:38 右:39 下:40
        int code = e.getKeyCode();
        if (code == 37) {
            if (x == 3) {
                //到了边界
                return;
            }
            //左
            data[x][y] = data[x + 1][y];
            data[x + 1][y] = 0;
            x++;

            //次数自增
            step++;

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

        } else if (code == 38) {
            if (y == 3) {
                //到了边界
                return;
            }
            //上
            data[x][y] = data[x][y + 1];
            data[x][y + 1] = 0;
            y++;

            //次数自增
            step++;

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

        } else if (code == 39) {
            if (x == 0) {
                //到了边界
                return;
            }
            //右
            data[x][y] = data[x - 1][y];
            data[x - 1][y] = 0;
            x--;

            //次数自增
            step++;

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

        } else if (code == 40) {
            if (y == 0) {
                //到了边界
                return;
            }
            //下
            data[x][y] = data[x][y - 1];
            data[x][y - 1] = 0;
            y--;

            //次数自增
            step++;

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

        } else if (code == 65) {
            initImage();

        } else if (code == 87) {
            data = new int[][]{
                    {1, 5, 9, 13},
                    {2, 6, 10, 14},
                    {3, 7, 11, 15},
                    {4, 8, 12, 0}
            };

            initImage();
        }
    }

    //判断data数组是否跟win数组中相同
    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]) {
                    return false;
                }

            }
        }
        //数组遍历比较完毕
        return true;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //获取点击对象
        Object obj = e.getSource();
        if(obj == replayitem){
            //重新游戏
            //打乱数组
            initData();

            //计步器清零
            step = 0;

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

        } else if (obj == reloginitem) {
            //重新登录
            //关闭当前游戏界面
            this.setVisible(false);
            //打开登录界面
            new LoginJframe();

        } else if (obj == closeitem) {
            //关闭游戏
            //关闭虚拟机
            System.exit(0);

        } else if (obj == accountitem) {
            //公众号
            //创建弹框对象
            JDialog jDialog = new JDialog();

            //创建容器
            JLabel jLabel = new JLabel(new ImageIcon("C:\\Users\\MIKU\\IdeaProjects\\try-one\\image\\about.png"));
            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);

        }
    }
}


Tips

以上学习内容均来自于B站黑马程序员

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值