拼图小游戏---主界面2

主界面 603 * 680

3.初始化图片

图片素材

  • 未打乱效果:
    在这里插入图片描述
  • 代码:
private void initImage_origin() {
        //创建ImageIcon
        ImageIcon icon1 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\1.jpg");
        //创建JLabel
        JLabel jLabel1 = new JLabel(icon1);
        //指定具体位置
        jLabel1.setBounds(0,0,105,105);
        this.getContentPane().add(jLabel1);
        //添加到界面中
        //this.add(jLabel);

        //创建ImageIcon
        ImageIcon icon2 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\2.jpg");
        //创建JLabel
        JLabel jLabel2 = new JLabel(icon2);
        //指定具体位置
        jLabel2.setBounds(105,0,105,105);
        this.getContentPane().add(jLabel2);

        //创建ImageIcon
        ImageIcon icon3 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\3.jpg");
        //创建JLabel
        JLabel jLabel3 = new JLabel(icon3);
        //指定具体位置
        jLabel3.setBounds(210,0,105,105);
        this.getContentPane().add(jLabel3);

        //创建ImageIcon
        ImageIcon icon4 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\4.jpg");
        //创建JLabel
        JLabel jLabel4 = new JLabel(icon4);
        //指定具体位置
        jLabel4.setBounds(315,0,105,105);
        this.getContentPane().add(jLabel4);

        //创建ImageIcon
        ImageIcon icon5 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\5.jpg");
        //创建JLabel
        JLabel jLabel5 = new JLabel(icon5);
        //指定具体位置
        jLabel5.setBounds(0,105,105,105);
        this.getContentPane().add(jLabel5);

        //创建ImageIcon
        ImageIcon icon6 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\6.jpg");
        //创建JLabel
        JLabel jLabel6 = new JLabel(icon6);
        //指定具体位置
        jLabel6.setBounds(105,105,105,105);
        this.getContentPane().add(jLabel6);

        //创建ImageIcon
        ImageIcon icon7 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\7.jpg");
        //创建JLabel
        JLabel jLabel7 = new JLabel(icon7);
        //指定具体位置
        jLabel7.setBounds(210,105,105,105);
        this.getContentPane().add(jLabel7);

        //创建ImageIcon
        ImageIcon icon8 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\8.jpg");
        //创建JLabel
        JLabel jLabel8 = new JLabel(icon8);
        //指定具体位置
        jLabel8.setBounds(315,105,105,105);
        this.getContentPane().add(jLabel8);

        //创建ImageIcon
        ImageIcon icon9 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\9.jpg");
        //创建JLabel
        JLabel jLabel9 = new JLabel(icon9);
        //指定具体位置
        jLabel9.setBounds(0,210,105,105);
        this.getContentPane().add(jLabel9);

        //创建ImageIcon
        ImageIcon icon10 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\10.jpg");
        //创建JLabel
        JLabel jLabel10 = new JLabel(icon10);
        //指定具体位置
        jLabel10.setBounds(105,210,105,105);
        this.getContentPane().add(jLabel10);

        //创建ImageIcon
        ImageIcon icon11 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\11.jpg");
        //创建JLabel
        JLabel jLabel11 = new JLabel(icon11);
        //指定具体位置
        jLabel11.setBounds(210,210,105,105);
        this.getContentPane().add(jLabel11);

        //创建ImageIcon
        ImageIcon icon12 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\12.jpg");
        //创建JLabel
        JLabel jLabel12 = new JLabel(icon12);
        //指定具体位置
        jLabel12.setBounds(315,210,105,105);
        this.getContentPane().add(jLabel12);

        //创建ImageIcon
        ImageIcon icon13 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\13.jpg");
        //创建JLabel
        JLabel jLabel13 = new JLabel(icon13);
        //指定具体位置
        jLabel13.setBounds(0,315,105,105);
        this.getContentPane().add(jLabel13);

        //创建ImageIcon
        ImageIcon icon14 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\14.jpg");
        //创建JLabel
        JLabel jLabel14 = new JLabel(icon14);
        //指定具体位置
        jLabel14.setBounds(105,315,105,105);
        this.getContentPane().add(jLabel14);

        //创建ImageIcon
        ImageIcon icon15 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\15.jpg");
        //创建JLabel
        JLabel jLabel15 = new JLabel(icon15);
        //指定具体位置
        jLabel15.setBounds(210,315,105,105);
        this.getContentPane().add(jLabel15);

        //创建ImageIcon
        ImageIcon icon16 = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\16.jpg");
        //创建JLabel
        JLabel jLabel16 = new JLabel(icon16);
        //指定具体位置
        jLabel16.setBounds(315,315,105,105);
        this.getContentPane().add(jLabel16);
    }
  • 实现打乱的效果
(1) 先打乱图片的下标(随机实现)
private void initData() {
        int[] tempArr = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};

        Random rand = new Random();
        for (int i = 0; i < tempArr.length; i++) {
            int index = rand.nextInt(tempArr.length);
            int temp = tempArr[i];
            tempArr[i] = tempArr[index];
            tempArr[index] = temp;
        }
        for (int i = 0; i < tempArr.length; i++) {
            result[i/4][i%4] = tempArr[i];
        }
    }
(2)此时,result二维数组已经存储了相应的图片下标
  • 打乱效果:
    在这里插入图片描述
  • 代码:
private void initImage(){

        for (int i = 0; i < 4; i++) {
            for(int j = 0; j < 4; j++){
                //创建ImageIcon
                ImageIcon icon = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\" + result[i][j] + ".jpg");
                //创建JLabel
                JLabel jLabel = new JLabel(icon);
                //指定具体位置
                jLabel.setBounds(j * 105 ,i * 105 ,105,105);
                //jLabel.setBounds(j * 105 + 83,i * 105 + 134,105,105);
                //给图片添加边框
                jLabel.setBorder(new BevelBorder(1));
                this.getContentPane().add(jLabel);
            }
        }

美化背景

  • 效果

在这里插入图片描述

  • 代码
private void initImage(){

        for (int i = 0; i < 4; i++) {
            for(int j = 0; j < 4; j++){
                //创建ImageIcon
                ImageIcon icon = new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\animal\\animal2\\" + result[i][j] + ".jpg");
                //创建JLabel
                JLabel jLabel = new JLabel(icon);
                //指定具体位置
                //jLabel.setBounds(j * 105 ,i * 105 ,105,105);
                jLabel.setBounds(j * 105 + 83,i * 105 + 134,105,105);
                //给图片添加边框
                jLabel.setBorder(new BevelBorder(1));
                this.getContentPane().add(jLabel);
            }
        }
        //添加背景图片
        JLabel background = new JLabel(new ImageIcon("D:\\JAVA\\JAVA\\study\\image\\background.png"));
        background.setBounds(40, 40, 508, 560);
        this.getContentPane().add(background);
    }
  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值