Java拼图小游戏

Java拼图小游戏

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ImagePuzzleGame extends JFrame {

    private JPanel puzzlePanel;
    private List puzzlePieces;
    private JButton emptyButton;

    public ImagePuzzleGame() {
        initUI();
    }

    private void initUI() {
        setTitle("Image Puzzle Game");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 300);
        setLocationRelativeTo(null);

        puzzlePieces = new ArrayList<>();
        puzzlePanel = new JPanel(new GridLayout(3, 3, 2, 2));

        createImagePuzzlePieces();
        addEmptyButton();

        add(puzzlePanel, BorderLayout.CENTER);

        JButton shuffleButton = new JButton("Shuffle");
        shuffleButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                shufflePuzzle();
            }
        });

        add(shuffleButton, BorderLayout.SOUTH);
    }

    // 创建带有图像的拼图方块
    private void createImagePuzzlePieces() {
        // 加载图像( "src/twicon-kagari.jpg" 为实际图像文件路径)
        ImageIcon icon = new ImageIcon("src/twicon-kagari.jpg");
        BufferedImage img = toBufferedImage(icon.getImage());

        for (int i = 1; i <= 8; i++) {
            JButton button = new JButton(new ImageIcon(getSubImage(img, i)));
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    movePiece(button);
                }
            });
            puzzlePieces.add(button);
            puzzlePanel.add(button);
        }
    }

    // 将 Image 转换为 BufferedImage
    private BufferedImage toBufferedImage(Image img) {
        if (img instanceof BufferedImage) {
            return (BufferedImage) img;
        }

        BufferedImage bufferedImage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        Graphics g = bufferedImage.getGraphics();
        g.drawImage(img, 0, 0, null);
        g.dispose();

        return bufferedImage;
    }

    // 获取图像的子图像
    private Image getSubImage(BufferedImage img, int index) {
        int width = img.getWidth() / 3;
        int height = img.getHeight() / 3;

        int row = (index - 1) / 3;
        int col = (index - 1) % 3;

        return img.getSubimage(col * width, row * height, width, height).getScaledInstance(width, height, Image.SCALE_SMOOTH);
    }

    // 添加一个空的拼图方块
    private void addEmptyButton() {
        emptyButton = new JButton("");
        emptyButton.setEnabled(false);
        puzzlePieces.add(emptyButton);
        puzzlePanel.add(emptyButton);
    }

    // 洗牌拼图
    private void shufflePuzzle() {
        Collections.shuffle(puzzlePieces);
        updatePuzzlePanel();
    }

    // 更新拼图面板
    private void updatePuzzlePanel() {
        puzzlePanel.removeAll();
        for (JButton piece : puzzlePieces) {
            puzzlePanel.add(piece);
        }
        revalidate();
        repaint();
    }

    // 移动拼图方块
    private void movePiece(JButton clickedButton) {
        int emptyIndex = puzzlePieces.indexOf(emptyButton);
        int clickedIndex = puzzlePieces.indexOf(clickedButton);

        if (isAdjacent(emptyIndex, clickedIndex)) {
            Collections.swap(puzzlePieces, emptyIndex, clickedIndex);
            updatePuzzlePanel();
        }

        if (isPuzzleSolved()) {
            JOptionPane.showMessageDialog(this, "Congratulations! You solved the puzzle!");
        }
    }

    // 检查方块是否相邻
    private boolean isAdjacent(int index1, int index2) {
        int row1 = index1 / 3;
        int col1 = index1 % 3;
        int row2 = index2 / 3;
        int col2 = index2 % 3;

        return Math.abs(row1 - row2) + Math.abs(col1 - col2) == 1;
    }

    // 检查拼图是否已解决
    private boolean isPuzzleSolved() {
        for (int i = 0; i < puzzlePieces.size() - 1; i++) {
            // 检查每个方块是否与其应在的位置相符
            if (!puzzlePieces.get(i).getIcon().equals(new ImageIcon(getSubImage(toBufferedImage(((ImageIcon)puzzlePieces.get(i).getIcon()).getImage()), i + 1)))) {
                return false;
            }
        }
        return true;
    }

    // 主方法,启动应用
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                ImagePuzzleGame puzzleGame = new ImagePuzzleGame();
                puzzleGame.setVisible(true);
            }
        });
    }
}

() {
ImagePuzzleGame puzzleGame = new ImagePuzzleGame();
puzzleGame.setVisible(true);
}
});
}
}


你可能感兴趣的:(java)