好玩的拼图游戏

照片自己放

1.gamejframe.class类

package test;

import javax.swing.*;
import javax.swing.border.BevelBorder;
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, ActionListener {

    int[][] date = new int[4][4];
    //空白方块的位置
    int x=0;
    int y=0;
    String path ="image\\小羊(1)\\";
    int win[][] ={
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12},
        {13, 14, 15, 0}
    };
    //步数
    int step=0;

    //选项下的条目
    JMenuItem replay = new JMenuItem("重新游戏");
    JMenuItem reLoginItem = new JMenuItem("重新登陆");
    JMenuItem closeTtem = new JMenuItem("关闭游戏");
    JMenuItem accountItem = new JMenuItem("微信二维码");


    public gamejframe(){
        //初始化界面
        initJframe();

        //初始化菜单
        initJmenuBar();

        //初始化数据
        initData();

        //初始化照片
        initImage();

        //展示
        this.setVisible(true);

    }

    private void initData() {
        int [] tempArr = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
        Random r = new Random();
        for (int i=0;i<tempArr.length;i++){
            int index = r.nextInt(tempArr.length);
            int temp = tempArr[i];
            tempArr[i] = tempArr[index];
            tempArr[index]=temp;
        }

        for (int i =0;i<tempArr.length;i++){
            if (tempArr[i]==0){
                x=i/4;
                y=i%4;
            }
            date[i / 4][i % 4] = tempArr[i];

        }

    }

    private void initImage() {
        //清空已经存在的照片
        this.getContentPane().removeAll();

        //显示胜利的图片
        if (Win()){
            JLabel Win = new JLabel(new ImageIcon("image\\win.jpeg"));
            Win.setBounds(120,65,320,320);
            this.getContentPane().add(Win);


        }

        //步数记录
        JLabel stepcount = new JLabel("步数:"+step);
        stepcount.setBounds(50,30,100,20);
        this.getContentPane().add(stepcount);


        for (int i=0;i<4;i++){
            for (int j=0;j<4;j++){
                //创建一个图片ImageIcon的对象
                int nub = date[i][j];
                ImageIcon icon = new ImageIcon(path+nub+".jpg");
                //创建一个JLabe1的对象
                JLabel jLabel1 = new JLabel(icon);
                //指定位置
                jLabel1.setBounds(105*j+75,105*i+55,105,105);
                //边框
                jLabel1.setBorder(new BevelBorder(0));
                //把管理容器添加到界面中
//        this.add(jLabel);
                this.getContentPane().add(jLabel1);

            }

        }
        JLabel background = new JLabel(new ImageIcon("image\\banjing(1).jpg"));
        background.setBounds(0,0,580,550);
        this.getContentPane().add(background);
        this.getContentPane().repaint();

    }

    private void initJmenuBar() {
        //菜单
        JMenuBar jMenuBar = new JMenuBar();

        JMenu FunctionJmenu = new JMenu("功能");
        JMenu AboutJmenu = new JMenu("关于我们");

        //加入选项
        FunctionJmenu.add(replay);
        FunctionJmenu.add(reLoginItem);
        FunctionJmenu.add(closeTtem);
        AboutJmenu.add(accountItem);
        //给条目绑定事件
        replay.addActionListener(this);
        reLoginItem.addActionListener(this);
        closeTtem.addActionListener(this);
        accountItem.addActionListener(this);

        //加入菜单
        jMenuBar.add(FunctionJmenu);
        jMenuBar.add(AboutJmenu);
        //设置菜单
        this.setJMenuBar(jMenuBar);
    }

    private void initJframe() {
        this.setSize(580,550);
        this.setTitle("拼图");
        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(3);
        this.setLayout(null);
        this.addKeyListener(this);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        if (code==65){
            this.getContentPane().removeAll();
            JLabel alltupian = new JLabel(new ImageIcon(path+"你的路径.jpg"));
            alltupian.setBounds(65,55,420,420);
            this.getContentPane().add(alltupian);
            JLabel background = new JLabel(new ImageIcon("image\\banjing(1).jpg"));
            background.setBounds(0,0,580,550);
            this.getContentPane().add(background);
            this.getContentPane().repaint();

        }

    }
    private boolean[] keysPressed = new boolean[3]; // 用于跟踪 g, s, y 是否按顺序被按下
    private int keyIndex = 0; // 当前应按下的键的索引
    @Override
    public void keyReleased(KeyEvent e) {
        if (Win()){
            return;
        }

        int code = e.getKeyCode();
        System.out.println(code);




        if (code==37){
            System.out.println("向左移动");
            if (y==3){
                return;
            }
            date[x][y]=date[x][y+1];
            date[x][y+1]=0;
            y++;
            step++;
            initImage();

        }else if (code==38){
            System.out.println("向上移动");
            if (x==3){
                return;
            }
            date[x][y]=date[x+1][y];
            date[x+1][y]=0;
            x++;
            step++;
            initImage();
        } else if (code==39) {
            System.out.println("向右移动");
            if (y==0){
                return;
            }
            date[x][y]=date[x][y-1];
            date[x][y-1]=0;
            y--;
            step++;
            initImage();

        } else if (code==40) {
            System.out.println("向下移动");
            if (x==0){
                return;
            }
            date[x][y]=date[x-1][y];
            date[x-1][y]=0;
            x--;
            step++;
            initImage();

        }else if (code==65){
            initImage();

        } else if (code == KeyEvent.VK_G && keyIndex == 0) { //特殊快捷键gsy,检查是否按下了 g, s, 或 y 键,并按正确的顺序
            keysPressed[0] = true;
            keyIndex++;
        } else if (code == KeyEvent.VK_S && keyIndex == 1 && keysPressed[0]) {
            keysPressed[1] = true;
            keyIndex++;
        } else if (code == KeyEvent.VK_Y && keyIndex == 2 && keysPressed[0] && keysPressed[1]) {
            keysPressed[2] = true;
            // 所有键都按顺序被按下,执行操作
            date = new int[][]{
                    {1, 2, 3, 4},
                    {5, 6, 7, 8},
                    {9, 10, 11, 12},
                    {13, 14, 15, 0}
            };
            initImage();
        } else {
            // 如果按下的是其他键,或者顺序不对,重置
            resetKeysPressed();
        }


        }

    private void resetKeysPressed() {
        for (int i = 0; i < keysPressed.length; i++) {
            keysPressed[i] = false;
        }
        keyIndex = 0;
    }
    public Boolean Win(){
        for (int i = 0; i < date.length; i++) {
            for (int j = 0; j < date[i].length; j++) {
                if (date[i][j]!=win[i][j]){

                    return false;
                }

            }
            
        }
        return true;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object obj = e.getSource();
        if (obj==replay){
            step=0;
            initData();
            initImage();

        } else if (obj==reLoginItem) {
            this.setVisible(false);
            new LoginJframe();



        } else if (obj==closeTtem) {
            System.exit(0);


        } else if (obj==accountItem) {
            JDialog jDialog = new JDialog();
            JLabel jLabel = new JLabel(new ImageIcon(""));
            jLabel.setBounds(0,0,258,258);
            jDialog.getContentPane().add(jLabel);
            jDialog.setSize(344,344);
            jDialog.setAlwaysOnTop(true);
            jDialog.setLocationRelativeTo(null);
            jDialog.setModal(true);
            jDialog.setVisible(true);

        }

    }
}

 2.cutPic.class类

分割照片

package test;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 *
 * @return 分割后文件夹路径
 */
import java.awt.Graphics;
import java.io.IOException;
public class cutPic {
    public static String cutPic(String path) throws IOException {
        File file = new File(path);
        if (!file.exists() || !file.isFile()) {
            throw new RuntimeException("文件不存在或不是一个文件:" + path);
        }
        BufferedImage image = ImageIO.read(file);
        int totalWidth = image.getWidth();
        int totalHeight = image.getHeight();
        int width = 105; // 固定裁剪宽度
        int height = 105; // 固定裁剪高度
        int rows = 4; // 裁剪行数
        int cols = 4; // 裁剪列数

        // 确保原始图片尺寸足够裁剪成4x4的网格
        if (totalWidth < width * cols || totalHeight < height * rows) {
            throw new RuntimeException("原始图片尺寸不足以裁剪成4x4网格");
        }

        File dirFile = new File(file.getParent(), file.getName().substring(0, file.getName().lastIndexOf(".")));
        if (!dirFile.exists()) {
            dirFile.mkdir();
        }
        int fileCounter = 1; // 初始化文件计数器
        for (int y = 0; y < totalHeight; y += height) {
            for (int x = 0; x < totalWidth; x += width) {
                // 创建目标文件,文件名为递增的数字
                File targetFile = new File(dirFile, String.format("%d.jpg", fileCounter));
                BufferedImage targetImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                Graphics g = targetImage.getGraphics();

                // 计算当前裁剪区域的实际宽度和高度
                int currentWidth = Math.min(width, totalWidth - x);
                int currentHeight = Math.min(height, totalHeight - y);

                // 绘制当前裁剪区域到目标图片
                g.drawImage(image.getSubimage(x, y, currentWidth, currentHeight), 0, 0, currentWidth, currentHeight, null);
                g.dispose();
                // 保存目标图片
                ImageIO.write(targetImage, "JPG", targetFile);
                fileCounter++; // 增加文件计数器
                // 如果已经裁剪了4x4网格,则退出循环
                if (fileCounter > rows * cols) {
                    break;
                }
            }
            // 如果已经裁剪了4x4网格,则退出循环
            if (fileCounter > rows * cols) {
                break;
            }
        }
        return dirFile.getPath();
    }
}

 3.App.class类

启动类

import test.gamejframe;

public class App {
    public static void main(String[] args) throws Exception {
//        cutPic.cutPic("D:\\lianxi\\untitled\\image\\小羊(1).jpg");

//        new LoginJframe();
        new gamejframe();
//        new RegisterJframe();
    }
}

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

浪仙545

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

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

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

打赏作者

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

抵扣说明:

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

余额充值