Java 编写的拼图游戏的实现
时间: 2024-03-05 15:49:21 浏览: 67
好的,下面是一个简单的 Java 编写的拼图游戏的实现,它使用了 Java Swing GUI 库来创建用户界面。
首先,我们需要创建一个继承自 JFrame 的主窗口类 PuzzleGameFrame:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PuzzleGameFrame extends JFrame {
private PuzzleGamePanel puzzleGamePanel;
public PuzzleGameFrame() {
super("Puzzle Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
puzzleGamePanel = new PuzzleGamePanel();
getContentPane().add(puzzleGamePanel, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new PuzzleGameFrame();
}
}
```
在主窗口类中,我们创建了一个 PuzzleGamePanel 对象并将其添加到窗口的中心位置。
接下来,我们需要创建一个继承自 JPanel 的面板类 PuzzleGamePanel:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PuzzleGamePanel extends JPanel {
private final int ROWS = 3;
private final int COLUMNS = 3;
private JButton[][] puzzleButtons;
private ImageIcon[][] puzzleImages;
private ImageIcon blankImage;
private int blankRow;
private int blankColumn;
public PuzzleGamePanel() {
setLayout(new GridLayout(ROWS, COLUMNS));
puzzleButtons = new JButton[ROWS][COLUMNS];
puzzleImages = new ImageIcon[ROWS][COLUMNS];
for (int row = 0; row < ROWS; row++) {
for (int column = 0; column < COLUMNS; column++) {
int index = row * COLUMNS + column;
puzzleImages[row][column] = new ImageIcon("image" + index + ".jpg");
puzzleButtons[row][column] = new JButton(puzzleImages[row][column]);
puzzleButtons[row][column].addActionListener(new PuzzleButtonListener(row, column));
add(puzzleButtons[row][column]);
}
}
blankImage = new ImageIcon("imageblank.jpg");
blankRow = ROWS - 1;
blankColumn = COLUMNS - 1;
puzzleButtons[blankRow][blankColumn].setIcon(blankImage);
}
private void swap(int row1, int column1, int row2, int column2) {
ImageIcon temp = puzzleImages[row1][column1];
puzzleImages[row1][column1] = puzzleImages[row2][column2];
puzzleImages[row2][column2] = temp;
puzzleButtons[row1][column1].setIcon(puzzleImages[row1][column1]);
puzzleButtons[row2][column2].setIcon(puzzleImages[row2][column2]);
}
private boolean isAdjacent(int row1, int column1, int row2, int column2) {
if (row1 == row2 && Math.abs(column1 - column2) == 1) {
return true;
}
if (column1 == column2 && Math.abs(row1 - row2) == 1) {
return true;
}
return false;
}
private boolean isSolved() {
int index = 0;
for (int row = 0; row < ROWS; row++) {
for (int column = 0; column < COLUMNS; column++) {
if (index != ROWS * COLUMNS - 1 && puzzleImages[row][column] != puzzleImages[index / COLUMNS][index % COLUMNS]) {
return false;
}
index++;
}
}
return true;
}
private class PuzzleButtonListener implements ActionListener {
private int row;
private int column;
public PuzzleButtonListener(int row, int column) {
this.row = row;
this.column = column;
}
public void actionPerformed(ActionEvent event) {
if (isAdjacent(row, column, blankRow, blankColumn)) {
swap(row, column, blankRow, blankColumn);
blankRow = row;
blankColumn = column;
if (isSolved()) {
JOptionPane.showMessageDialog(null, "Congratulations, you solved the puzzle!");
}
}
}
}
}
```
在面板类中,我们创建了一个 3x3 的拼图游戏,并为每个按钮添加了一个 ActionListener 监听器。当用户点击一个按钮时,程序会检查该按钮是否与空白按钮相邻,如果是,则交换它们的位置。当所有的按钮都被正确地交换到其正确的位置时,程序会弹出一个消息框,告知用户已经完成了拼图。
最后,我们需要将图像文件 image0.jpg 到 image8.jpg 和 imageblank.jpg 放置在程序的根目录下。
这是一个简单的 Java 编写的拼图游戏的实现。您可以根据自己的需要进行修改和定制。