JAVA 利用GUI实现动漫美女拼图(附源码)(直接可复制粘贴)

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

/*
    图片展示
 */
class frame extends Frame  {
    static JFrame jf = new JFrame();
    static Random random = new Random();
    static Panel panel =new Panel();
    static int x0;
    static int y0;
    int[][] arr = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}};
    public frame() {
        jf.setTitle("图片展示");
        jf.setSize(960, 530);
        jf.setDefaultCloseOperation(3);
        jf.setLocationRelativeTo(null);
        jf.setAlwaysOnTop(true);
        jf.setLayout(null);
        panel.setBounds(150, 114, 380, 380);


        arr();
        setp();
        button();

        JLabel other = new JLabel(new ImageIcon("day03\\images\\background.png"));
        other.setBounds(0,0,960,530);
        jf.add(other);



        jf.setVisible(true);
    }

    public  void arr(){

        //打乱

        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {


                int x = random.nextInt(4);
                int y = random.nextInt(arr[x].length);

                int temp = arr[i][j];
                arr[i][j] = arr[x][y];
                arr[x][y] = temp;
            }

        }


        //添加


        //获取空白图片

        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                if (arr[i][j] == 0){
                    x0 = i;
                    y0 = j;

                }
            }
        }
        System.out.println(x0+","+y0);
    }
    public void setp(){


        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                JLabel jLabel1 = new JLabel(new ImageIcon("day03\\images\\"+arr[i][j]+".png"));
                jLabel1.setBounds(j*90, i*90, 90, 90);
                panel.add(jLabel1);

            }
        }
        jf.add(panel);
        JLabel other2 = new JLabel(new ImageIcon("day03\\images\\title.png"));
        other2.setBounds(354,27,232,57);
        jf.add(other2);

    }

    public void setpp(){
        panel.removeAll();
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                JLabel jLabel1 = new JLabel(new ImageIcon("day03\\images\\"+arr[i][j]+".png"));
                jLabel1.setBounds(j*90, i*90, 90, 90);
                panel.add(jLabel1);


            }
        }
        panel.repaint();
    }




    public  void button(){
        JLabel jLabel2 = new JLabel(new ImageIcon("day03\\images\\canzhaotu.png"));
        jLabel2.setBounds(574, 114, 122, 121);
        jf.add(jLabel2);

        //按钮
        JButton j1 =new JButton(new ImageIcon("day03\\images\\shang.png"));
        j1.setBounds(732,265,57,57);
        jf.add(j1);


        j1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (x0 == 3){
                    return;
                }
                arr[x0][y0] = arr[x0 + 1][y0];
                arr[x0 + 1][y0] = 0;
                x0 = x0 + 1;

                setpp();

                //重绘方法调用


            }
        });

        JButton j2 =new JButton(new ImageIcon("day03\\images\\xia.png"));
        j2.setBounds(732,347,57,57);
        jf.add(j2);

        j2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (x0 == 0){
                    return;
                }
                arr[x0][y0] = arr[x0 - 1][y0];
                arr[x0 - 1][y0] = 0;
                x0 = x0 - 1;

                setpp();
            }
        });

        JButton j3 =new JButton(new ImageIcon("day03\\images\\zuo.png"));
        j3.setBounds(650,347,57,57);
        jf.add(j3);

        j3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (y0 == 3) {
                    return;
                }
                arr[x0][y0] = arr[x0][y0 + 1];
                arr[x0][y0 + 1] = 0;
                y0 = y0 + 1;
                setpp();

            }
        });

        JButton j4 =new JButton(new ImageIcon("day03\\images\\you.png"));
        j4.setBounds(813,347,57,57);
        jf.add(j4);

        j4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (y0 == 0) {
                    return;
                }
                arr[x0][y0] = arr[x0][y0 - 1];
                arr[x0][y0 - 1] = 0;
                y0 = y0 - 1;

                setpp();

            }
        });

        //重置



        JButton j5 =new JButton(new ImageIcon("day03\\images\\qiuzhu.png"));
        j5.setBounds(626,444,108,45);
        jf.add(j5);

        j5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                success();
                setpp();

            }
        });



        JButton j6 =new JButton(new ImageIcon("day03\\images\\chongzhi.png"));
        j6.setBounds(786,444,108,45);
        jf.add(j6);

        j6.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                arr();
                setpp();
                j1.setEnabled(true);
                j1.setEnabled(true);
                j1.setEnabled(true);
                j1.setEnabled(true);

            }
        });

    }

    public  void success(){
         arr =new int[][] {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
    }


}
    public class ArrayTest03{
        public static void main(String[] args) {
            new frame();
    }
}
  • 17
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值