首页 > 编程语言 >JAVA-动漫美女拼图—完结篇(重置业务实现)

JAVA-动漫美女拼图—完结篇(重置业务实现)

时间:2022-11-26 17:33:50浏览次数:43  
标签:完结篇 JAVA int 重置 new y0 x0 public datas

代码一

package com.itheima_10;

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

    }
}

代码二

package com.itheima_10;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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}
    };

    //定义一个移动成功的数组
    private int[][] winDatas = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 0}
    };

    //定义两个int类型的变量,用于记录0号图片的位置
    private int x0;
    private int y0;

    //定义上左下右,求助,重置按钮
    private JButton shangButton;
    private JButton zuoButton;
    private JButton xiaButton;
    private JButton youButton;
    private JButton qiuzhuButton;
    private JButton chongzhiButton;

    //定义面板

    private JPanel imagePanel;


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


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

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

        //给按钮添加事件
        addButtonEvent();

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

    }

    //判断移动是否成功
    public boolean isSuccess() {
        for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                if (datas[i][j] != winDatas[i][j]) {
                    return false;
                }
                ;

            }
        }
        return true;

    }

    //移动成功的操作
    public void success() {
        datas = new int[][]{
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
                {13, 14, 15, 16}

        };
        shangButton.setEnabled(false);//按钮不可点击
        zuoButton.setEnabled(false);
        xiaButton.setEnabled(false);
        youButton.setEnabled(false);

    }

    //移动的图形重新绘制
    public void rePaintView() {
        //移除面板上所有的组件
        imagePanel.removeAll();

        //遍历二维数组得到图片编号
        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);

        //重新绘制窗体
        imagePanel.repaint();
    }

    //给按钮添加事件
    public void addButtonEvent() {
        shangButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                System.out.println("上");

                //边界处理
                if (x0 == 3) {
                    return;
                }
                //位置交换
                datas[x0][y0] = datas[x0 + 1][y0];
                datas[x0 + 1][y0] = 0;
                x0 = x0 + 1;

                //判断移动是否成功
                if (isSuccess()) {
                    success();
                }


                //调用重绘的方法
                rePaintView();
            }
        });
        zuoButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                System.out.println("左");

                //边界处理
                if (y0 == 3) {
                    return;
                }

                //位置交换
                datas[x0][y0] = datas[x0][y0 + 1];
                datas[x0][y0 + 1] = 0;
                y0 = y0 + 1;
                //判断移动是否成功
                if (isSuccess()) {
                    success();
                }

                //调用重绘的方法
                rePaintView();

            }
        });
        xiaButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                System.out.println("下");

                //边界处理
                if (x0 == 0) {
                    return;
                }

                //位置交换
                datas[x0][y0] = datas[x0 - 1][y0];
                datas[x0 - 1][y0] = 0;
                x0 = x0 - 1;
                //判断移动是否成功
                if (isSuccess()) {
                    success();
                }

                //调用重绘的方法
                rePaintView();

            }
        });
        youButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                System.out.println("右");

                //边界处理
                if (y0 == 0) {
                    return;
                }
                //位置交换
                datas[x0][y0] = datas[x0][y0 - 1];
                datas[x0][y0 - 1] = 0;
                y0 = y0 - 1;
                //判断移动是否成功
                if (isSuccess()) {
                    success();
                }

                //调用重绘方法
                rePaintView();
            }
        });
        qiuzhuButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

//                System.out.println("求助");
                success();

                rePaintView();//调用重绘代码
            }

        });
        chongzhiButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

//                System.out.println("重置");
                datas = new int[][]{
                        {1, 2, 3, 4},
                        {5, 6, 7, 8},
                        {9, 10, 11, 12},
                        {13, 14, 15, 0}
                };
                randomData();
                rePaintView();
                shangButton.setEnabled(true);
                zuoButton.setEnabled(true);
                xiaButton.setEnabled(true);
                youButton.setEnabled(true);

            }
        });
    }

    //二维数组元素打乱
    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;


            }
        }
        //记录0号图片的位置
        wc:
        for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                if (datas[i][j] == 0) {
                    x0 = i;
                    y0 = j;
                    break wc;
                }
            }
        }
        System.out.println(x0 + "," + y0);
    }

    //窗体上组件的绘制
    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}
//        };
        //创建面板
        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);

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


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


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


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


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


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


        //展示背景图
        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);
    }
}

标签:完结篇,JAVA,int,重置,new,y0,x0,public,datas
From: https://www.cnblogs.com/cy-xt/p/16927852.html

相关文章

  • JAVA-动漫拼图图片移动业务遗留问题处理
    packagecom.itheima_09;publicclassApp{publicstaticvoidmain(String[]args){PictureFramepf=newPictureFrame();}}packagecom.ithe......
  • java——数据库连接池——druid_基本使用
               5.Druid:数据库连接池实现技术,由阿里巴巴提供的1.步骤:1.导入jar包druid-1.0.9.jar2.定......
  • java中 mkdirs与mkdir区别
    原文链接:https://www.jianshu.com/p/cd8bc8d92ea6mkdirs()可以建立多级文件夹,mkdir()只会建立一级的文件夹,如下:newFile("/tmp/one/two/three").mkdirs();执行后,会......
  • JAVA_动漫拼图求助业务实现
    packagecom.itheima_08;publicclassApp{publicstaticvoidmain(String[]args){PictureFramepf=newPictureFrame();}}packagecom.ithe......
  • java——数据库连接池——c3p0_基本使用
    4.C3P0:数据库连接池技术*步骤:1.导入jar包(两个)c3p0-0.9.5.2.jarmchange-commons-java-0.2.12.jar,*不要忘记导入数......
  • 【浅谈Java】this和super的用法与区别
    在Java的学习与开发者我们经常遇到this和super关键字,那么它们的用法和区别是什么呢?一、this关键字1.this是什么?this是自身的一个对象,代表对象本身,可以理解为:指向对象本......
  • java实现扑克牌游戏(洗牌,发牌,排序)
    packagepoker.bean;importlombok.AllArgsConstructor;importlombok.Getter;importlombok.NoArgsConstructor;importlombok.Setter;importjava.lang.annotatio......
  • java反射
    反射的概念:JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调......
  • java——数据库连接池——概念简介
        1.概念:其实就是一个容器(集合),存放数据库连接的容器。当系统初始化好后,容器被创建,容器中会申请一些连接对象,当用户来访问数据库时,从容器......
  • javascript面试题
    1.null和undefined区别首先Undefined和Null都是基本数据类型,这两个基本数据类型分别都只有一个值,就是undefined和null。undefined代表的含义是未定义,null代表......