JAVA-动漫美女拼图 (图片打乱)

代码一

package com.itheima_02;

public class App{
    public static void main(String[] args) {
        PictureFrame pf=new PictureFrame();

    }
}

代码二

package com.itheima_02;

import javax.swing.*;
import java.util.Random;

public class PictureFrame extends JFrame {

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


    //二维数组元素打乱
    public  void randomData(){
        Random r =new Random();
        for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                int x=r.nextInt(datas.length);
                int y=r.nextInt(datas[i].length);


                 int temp =datas[i][j];
                 datas[i][j]=datas[x][y];
                 datas[x][y]=temp;




            }
        }
    }

    public PictureFrame() {
        //用于窗体的基本设置
        initFrame();

        //二维数组元素打乱
        randomData();

        //窗体上组件的绘制
        paintView();

        //设置窗体可见
        this.setVisible(true);

    }

    //窗体上组件的绘制
    public void paintView() {
        //标题图片
        JLabel titleLabel = new JLabel(new ImageIcon("itheima_picture_puzzle\\images\\title.png"));
        titleLabel.setBounds(354, 27, 232, 57);
        this.add(titleLabel);

        //定义一个二维数组用来存储图片的编号
//        int[][] datas={
//                {1,2,3,4},
//                {5,6,7,8},
//                {9,10,11,12},
//                {13,14,15,16}
//        };
        //创建面板
        JPanel imagePanel = new JPanel();
        imagePanel.setBounds(150, 114, 360, 360);
        imagePanel.setLayout(null);//取消面板的默认布局
        //遍历二维数组得到图片编号
        for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                //创建JLabel对象加载图片资源
                JLabel imageLabel = new JLabel(new ImageIcon("itheima_picture_puzzle\\images\\" + datas[i][j] + ".png"));
                //调整图片的位置
                imageLabel.setBounds(j * 90, i * 90, 90, 90);
                imagePanel.add(imageLabel);
            }
        }
//        把面板添加到窗体上
        this.add(imagePanel);

        //动漫参照图
        JLabel canZhaoTuLabel = new JLabel(new ImageIcon("itheima_picture_puzzle\\images\\canzhaotu.png"));
        canZhaoTuLabel.setBounds(574, 114, 122, 121);
        this.add(canZhaoTuLabel);

        //上下左右按钮以及求助重置按钮
        JButton shangButton = new JButton(new ImageIcon("itheima_picture_puzzle\\images\\shang.png"));
        shangButton.setBounds(732, 265, 57, 57);
        this.add(shangButton);


        JButton zuoButton = new JButton(new ImageIcon("itheima_picture_puzzle\\images\\zuo.png"));
        zuoButton.setBounds(650, 347, 57, 57);
        this.add(zuoButton);


        JButton xiaButton = new JButton(new ImageIcon("itheima_picture_puzzle\\images\\xia.png"));
        xiaButton.setBounds(732, 347, 57, 57);
        this.add(xiaButton);


        JButton youButton = new JButton(new ImageIcon("itheima_picture_puzzle\\images\\you.png"));
        youButton.setBounds(813, 347, 57, 57);
        this.add(youButton);


        JButton qiuzhuButton = new JButton(new ImageIcon("itheima_picture_puzzle\\images\\qiuzhu.png"));
        qiuzhuButton.setBounds(626, 444, 108, 45);
        this.add(qiuzhuButton);


        JButton chognzhiButton = new JButton(new ImageIcon("itheima_picture_puzzle\\images\\chongzhi.png"));
        chognzhiButton.setBounds(786, 444, 108, 45);
        this.add(chognzhiButton);


        //展示背景图
        JLabel backgroundLabel = new JLabel(new ImageIcon("itheima_picture_puzzle\\images\\background.png"));
        backgroundLabel.setBounds(0, 0, 960, 530);
        this.add(backgroundLabel);
    }


    //用于窗体的基本设置
    public void initFrame() {
        //窗体大小
        this.setSize(960, 565);

        //窗体标题
        this.setTitle("动漫拼图");

        //窗体居中
        this.setLocationRelativeTo(null);

        //窗体关闭时退出程序
        this.setDefaultCloseOperation(3);

        //窗体要位于其他窗口之上
        this.setAlwaysOnTop(true);

        //取消窗体默认布局
        this.setLayout(null);
    }
}

执行结果

image

posted @ 2022-11-22 21:37  NiceTwocu  阅读(50)  评论(0编辑  收藏  举报