java:拼图游戏

package com.hheima.ui;

import javax.swing.*;
import java.awt.*;
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 {
    //记录空白方块在二维数组中的位置
    int x=0;
    int y=0;
    int[][] data=new int[3][3];
    int win[][]={{1,2,3},{4,5,6},{7,8,0}};
    public GameJFrame() {
        //存储数据,打乱图片

        //初始化界面
        initJframe();
        //初始化菜单
        initJMrameBar();
        //初始化数据(打乱)
        initDate();


        //初始化图片
        InitImage();


        //添加键盘监听
        //参数this表示当前对象,会调用本类方法,下面重写的
        this.addKeyListener(this);




        //让界面显示,建议放在最后写
        this.setVisible(true);





    }
    /**********************************************************/
    public void initDate() {
        int[] tempArr={0,1,2,3,4,5,6,7,8};


        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++){
            data[i/3][i%3]=tempArr[i];
           if(tempArr[i]==0){
               x=i/3;
               y=i%3;
           }

        }

    }


    private void InitImage() {
//删除已经出现的所有图片
this.getContentPane().removeAll();

        //设置图片位置
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                //创建图片对象
                int num=data[i][j];
                //创建图像ImageIcon对象
                ImageIcon imageIcon = new ImageIcon("C:\\Users\\王建宝\\IdeaProjects\\pinngtuyoux\\分割图片_(1)\\"+num+".jpg");
                //创建JLable对象(管理容器)
                JLabel jLabel = new JLabel(imageIcon);
                //设置图片位置
                jLabel.setBounds(200*j+40,200*i+40,200,200);
                this.getContentPane().add(jLabel);
                //设计边框
                jLabel.setBorder(BorderFactory.createMatteBorder(1,1,0,0, Color.BLUE));
            }
        }

        //设计背景图片,覆盖
        JLabel imageLabel = new JLabel(new ImageIcon("C:\\Users\\王建宝\\IdeaProjects\\pinngtuyoux\\分割图片_(1)\\91.jpg"));
        imageLabel.setBounds(0,0,1100,1100);
        add(imageLabel);


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

        if(vicyory())
        {
            System.out.println("胜利");
           
        }
        //创建图像ImageIcon对象
       // Image image = Toolkit.getDefaultToolkit().getImage("C:\\Users\\王建宝\\Pictures.png");
       // ImageIcon icon1 = new ImageIcon(image);
       /*ImageIcon icon1 = new ImageIcon("C:\\Users\\王建宝\\IdeaProjects\\pinngtuyoux\\w.png");//图片路径
        System.out.println(icon1.getImageLoadStatus());
        //创建JLable对象(管理容器)
        JLabel JLabel = new JLabel(icon1);
        //设置图片位置
        JLabel.setBounds(0,0,icon1.getIconWidth()/4,icon1.getIconHeight()/4);



        ImageIcon icon2 = new ImageIcon("C:\\Users\\王建宝\\IdeaProjects\\pinngtuyoux\\1.png");//图片路径
        System.out.println(icon1.getImageLoadStatus());
        //创建JLable对象(管理容器)
        JLabel JLabel2 = new JLabel(icon2);
        //设置图片位置
        JLabel.setBounds(0,105,icon1.getIconWidth()/4,icon1.getIconHeight()/4);*/


        //把管理容器放到界面中
       // this.add(JLabel);
        //this.getContentPane().add(JLabel);
    }







    private void initJMrameBar() {
        //初始菜单
        //创建菜单对象
        JMenuBar jMenuBar = new JMenuBar();
        JMenu functionJMenu = new JMenu("功能");
        JMenu aboutJMenu = new JMenu("关于");
        //创建条目下面的条目对象
        JMenuItem replayItem = new JMenuItem("重新游戏");
        JMenuItem reLoginItem = new JMenuItem("重新登录");
        JMenuItem closeItem = new JMenuItem("关闭游戏");

        JMenuItem accountItem = new JMenuItem("公众号");

        //把条目添加到菜单中
        //重新开始
        functionJMenu.add(replayItem);
        //重新登录
        functionJMenu.add(reLoginItem);
        //关闭游戏
        functionJMenu.add(closeItem);
        //关于
        aboutJMenu.add(accountItem);

        //把菜单添加到整个界面中
        jMenuBar.add(functionJMenu);
        jMenuBar.add(aboutJMenu);
        //给整个界面设置菜单
        this.setJMenuBar(jMenuBar);



    }

    private void initJframe() {
        //设置界面宽高
        this.setSize(730,730);
        //设置界面标题
        this.setTitle("2048小游戏");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(3);
        //把面板对象添加到界面中
        this.setLocationRelativeTo(null);
        //取消默认居中放置,只有取消了才会按照xy轴放置
        this.setLayout(null);


    }



    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
    //按下不松时,查看图片
        int code = e.getKeyCode();
        if (code == 65) {
            //按a
            this.getContentPane().removeAll();
            JLabel jLabel = new JLabel(new ImageIcon("C:\\Users\\王建宝\\IdeaProjects\\pinngtuyoux\\分割图片_(1)\\91.jpg"));
       jLabel.setBounds(0,0,100,100);
       this.getContentPane().add(jLabel);
//刷新一下
            this.getContentPane().repaint();
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {
    //对上下左右进行判断
        int keyCode = e.getKeyCode();
        if (keyCode == 37) {
            if(y==2)
            {
                return;
            }
            data[x][y]=data[x][y+1];
            data[x][y+1]=0;
            y++;
            InitImage();

        }
        if(keyCode == 38){
            //向上
            if(x==2)
            {
                return;
            }
            data[x][y]=data[x+1][y];
            data[x+1][y]=0;
            x++;
            InitImage();
        }
        if(keyCode == 39){
            if(y==0)
            {
                return;
            }
            data[x][y]=data[x][y-1];
            data[x][y-1]=0;
            y--;
            InitImage();
        }
        if(keyCode == 40){
            if(x==0)
            {
                return;
            }
            data[x][y]=data[x-1][y];
            data[x-1][y]=0;
            x--;
            InitImage();
        }else if(keyCode == 65){
            InitImage();

        }else if(keyCode == 87){
            data=new int[][]{
                    {1,2,3},
                    {4,5,6},
                    {7,8,0}};
            InitImage();

        }

}
public boolean vicyory(){
    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;
}


    }

package com.hheima.ui;

import javax.swing.*;

public class loginJFrame extends JFrame {
    //登录界面
    public loginJFrame() {
        this.setSize(488,430);

        //设置界面标题
        this.setTitle("游戏 登录");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(3);


        this.setLocationRelativeTo(null);
        //把面板对象添加到界面中

        //让界面显示
        this.setVisible(true);
    }
}
import com.hheima.ui.GameJFrame;
import com.hheima.ui.RegisterJFrame;
import com.hheima.ui.loginJFrame;

public class App {
    public static void main(String[] args) {
        //程序启动入口
      // new loginJFrame();//直接调用就行,方法里有参数
        new GameJFrame();
       // new RegisterJFrame();


}

}

package com.hheima.ui;

import javax.swing.*;

public class RegisterJFrame extends JFrame {
    //注册界面
    public RegisterJFrame() {
        this.setSize(488,500);
        //设置界面标题
        this.setTitle("游戏 注册");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(3);

        this.setLocationRelativeTo(null);
        //把面板对象添加到界面中

        //让界面显示
        this.setVisible(true);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宝耶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值