Java 拼图游戏的代码
时间: 2023-07-09 09:40:32 浏览: 202
Java拼图游戏代码
以下是一个简单的 Java 拼图游戏的代码示例,包括拼图分割、拖动、拼图完成判断等功能:
```
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class PuzzleGame extends JFrame implements ActionListener, MouseListener, MouseMotionListener {
private static final long serialVersionUID = 1L;
private JPanel panel;
private JLabel[] labels;
private int emptyIndex;
private int rows = 3;
private int cols = 3;
public PuzzleGame() {
super("Puzzle Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setResizable(false);
setSize(500, 500);
setLocationRelativeTo(null);
panel = new JPanel(new GridLayout(rows, cols));
add(panel, BorderLayout.CENTER);
ImageIcon imageIcon = new ImageIcon("puzzle.jpg");
Image image = imageIcon.getImage();
int width = image.getWidth(null) / cols;
int height = image.getHeight(null) / rows;
labels = new JLabel[rows * cols];
int index = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int x = j * width;
int y = i * height;
Image subImage = image.getScaledInstance(width, height, Image.SCALE_DEFAULT);
subImage = subImage.getSubimage(x, y, width, height);
ImageIcon subIcon = new ImageIcon(subImage);
labels[index] = new JLabel(subIcon);
labels[index].setBorder(BorderFactory.createLineBorder(Color.BLACK));
labels[index].addMouseListener(this);
labels[index].addMouseMotionListener(this);
panel.add(labels[index]);
index++;
}
}
// 设置空图块
emptyIndex = rows * cols - 1;
labels[emptyIndex].setIcon(null);
shuffle();
setVisible(true);
}
private void shuffle() {
Random random = new Random();
for (int i = 0; i < 1000; i++) {
int index = random.nextInt(rows * cols);
if (index != emptyIndex) {
swap(index);
}
}
}
private void swap(int index) {
int x1 = index % cols;
int y1 = index / cols;
int x2 = emptyIndex % cols;
int y2 = emptyIndex / cols;
if ((x1 == x2 && Math.abs(y1 - y2) == 1) || (y1 == y2 && Math.abs(x1 - x2) == 1)) {
JLabel label1 = labels[index];
JLabel label2 = labels[emptyIndex];
label2.setIcon(label1.getIcon());
label1.setIcon(null);
emptyIndex = index;
}
}
private boolean isCompleted() {
for (int i = 0; i < rows * cols - 1; i++) {
ImageIcon icon1 = (ImageIcon)labels[i].getIcon();
ImageIcon icon2 = new ImageIcon("puzzle.jpg");
Image image1 = icon1.getImage();
Image image2 = icon2.getImage();
if (!image1.equals(image2)) {
return false;
}
}
return true;
}
@Override
public void actionPerformed(ActionEvent e) {
if (isCompleted()) {
JOptionPane.showMessageDialog(this, "Congratulations! You have completed the puzzle!");
}
}
@Override
public void mousePressed(MouseEvent e) {
JLabel label = (JLabel)e.getSource();
int index = Arrays.asList(labels).indexOf(label);
swap(index);
}
@Override
public void mouseDragged(MouseEvent e) {
// 拖动图块
JLabel label = (JLabel)e.getSource();
label.setLocation(label.getX() + e.getX() - label.getWidth() / 2, label.getY() + e.getY() - label.getHeight() / 2);
}
@Override
public void mouseReleased(MouseEvent e) {
JLabel label = (JLabel)e.getSource();
int index = Arrays.asList(labels).indexOf(label);
Point point = label.getLocation();
Rectangle bounds = panel.getBounds();
if (bounds.contains(point)) {
int x = point.x / label.getWidth();
int y = point.y / label.getHeight();
int newIndex = y * cols + x;
if (newIndex != emptyIndex) {
swap(index);
}
} else {
label.setLocation(index % cols * label.getWidth(), index / cols * label.getHeight());
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
}
public static void main(String[] args) {
new PuzzleGame();
}
}
```
在这个示例中,我们使用了一个 JPanel 来展示拼图,并将拼图图片分割成小块,使用 JLabel 来展示每个小块。我们使用鼠标事件监听器来实现拖动小块来移动拼图,使用 swap() 方法来交换空图块和鼠标点击的图块。使用 shuffle() 方法来随机打乱拼图,使用 isCompleted() 方法来判断拼图是否完成。最后,我们将 PuzzleGame 实例化并显示出来。
阅读全文