Java实现拼图游戏
拼图游戏是一种经典的娱乐方式,其目标是将碎片拼接在一起以还原原始图像。在本文中,我将向您展示如何使用Java编写一个简单的拼图游戏。我们将使用Swing库来创建用户界面,并使用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.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import javax.imageio.ImageIO;
public class PuzzleGame extends JPanel {
private static final int BOARD_SIZE = 3;
private static final int TILE_SIZE = 200;
private static final int IMAGE_SIZE = BOARD_SIZE * TILE_SIZE;
private static final String IMAGE_PATH = "image.jpg";
private ArrayList<ImageTile> tiles;
private BufferedImage image;
public PuzzleGame() {
setPreferredSize(new Dimension(IMAGE_SIZE, IMAGE_SIZE));
setLayout(null);
tiles = new ArrayList<>();
loadAndSplitImage();
createTiles();
Collections.shuffle(tiles);
positionTiles();
addTilesToPanel();
addMouseListener(new PuzzleMouseListener());
}
private void loadAndSplitImage() {
try {
image = ImageIO.read(new File(IMAGE_PATH));
} catch (IOException e) {
e.printStackTrace();
}
}
private void createTiles() {
int index = 0;
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
int x = col * TILE_SIZE;
int y = row * TILE_SIZE;
BufferedImage tileImage = image.getSubimage(x, y, TILE_SIZE, TILE_SIZE);
ImageTile tile = new ImageTile(tileImage, index++);
tiles.add(tile);
}
}
}
private void positionTiles() {
int x = 0, y = 0;
for (ImageTile tile : tiles) {
tile.setPosition(x, y);
x += TILE_SIZE;
if (x >= IMAGE_SIZE) {
x = 0;
y += TILE_SIZE;
}
}
}
private void addTilesToPanel() {
for (ImageTile tile : tiles) {
add(tile);
}
}
private class PuzzleMouseListener extends MouseAdapter {
private ImageTile selectedTile;
@Override
public void mousePressed(MouseEvent e) {
selectedTile = getTileAt(e.getX(), e.getY());
}
@Override
public void mouseReleased(MouseEvent e) {
if (selectedTile != null) {
ImageTile targetTile = getTileAt(e.getX(), e.getY());
if (targetTile != null && selectedTile != targetTile) {
swapTiles(selectedTile, targetTile);
}
selectedTile.resetPosition();
selectedTile = null;
repaint();
}
}
private ImageTile getTileAt(int x, int y) {
for (ImageTile tile : tiles) {
if (tile.contains(x, y)) {
return tile;
}
}
return null;
}
private void swapTiles(ImageTile tile1, ImageTile tile2) {
int tempX = tile1.getX();
int tempY = tile1.getY();
tile1.setPosition(tile2.getX(), tile2.getY());
tile2.setPosition(tempX, tempY);
Collections.swap(tiles, tile1.getIndex(), tile2.getIndex());
}
}
private static class ImageTile extends JLabel {
private int index;
public ImageTile(BufferedImage image, int index) {
setIcon(new ImageIcon(image));
setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
this.index = index;
}
public int getIndex() {
return index;
}
public void setPosition(int x, int y) {
setLocation(x, y);
}
public void resetPosition() {
int x = index % BOARD_SIZE * TILE_SIZE;
int y = index / BOARD_SIZE * TILE_SIZE;
setLocation(x, y);
}
public boolean contains(int x, int y) {
return getBounds().contains(x, y);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Puzzle Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new PuzzleGame());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
在上面的代码中,我们创建了一个名为PuzzleGame的类,它继承自JPanel,作为拼图游戏的主要组件。在构造函数中,我们设置了面板的首选大小,并使用null布局。然后,我们加载并拆分图像,创建拼图碎片,并对它们进行随机排序和定位。最后,我们将拼图碎片添加到面板中,并为面板添加了一个鼠标监听器,以处理拼图碎片之间的交换。
我们还创建了一个名为ImageTile的嵌套类,它继承自JLabel,用于表示每个拼图碎片。它包含了拼图碎片的索引、位置设置和重置、包含坐标的方法。
最后,在main方法中,我们创建了一个JFrame并将PuzzleGame面板添加到其中,然后显示窗口。
您只需将上述代码保存为PuzzleGame.java文件,然后使用Java编译器进行编译。运行后,将显示一个窗口,其中包含随机排列的拼图碎片。您可以使用鼠标拖动碎片并与其他碎片交换位置,直到还原原始图像。
希望这个简单的拼图游戏实现能够满足您的需求!如果您有任何进一步的问题,请随时提问。
396

被折叠的 条评论
为什么被折叠?



