当前位置: 首页 > news >正文

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<JButton> 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() {@Overridepublic 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() {@Overridepublic void actionPerformed(ActionEvent e) {movePiece(button);}});puzzlePieces.add(button);puzzlePanel.add(button);}}// 将 Image 转换为 BufferedImageprivate 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() {@Overridepublic void run() {ImagePuzzleGame puzzleGame = new ImagePuzzleGame();puzzleGame.setVisible(true);}});}
}

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


http://www.lryc.cn/news/237728.html

相关文章:

  • 终于有人把数据资产入表知识地图总结出来了,轻松看懂
  • 白鳝:聊聊IvorySQL的Oracle兼容技术细节与实现原理
  • vue和uni-app的递归组件排坑
  • 【考研】数据结构(更新到顺序表)
  • 汇编-指针
  • 常见Web安全
  • milvus数据库搜索
  • HEVC参考帧技术
  • QT小记:The QColor ctor taking ints is cheaper than the one taking string literals
  • 机器人走迷宫问题
  • 轻量封装WebGPU渲染系统示例<36>- 广告板(Billboard)(WGSL源码)
  • Java 多线程进阶
  • CentOS上搭建SVN并自动同步至web目录
  • .Net中Redis的基本使用
  • 使用cli批量下载GitHub仓库中所有的release
  • 深入分析TaskView源码之触摸相关
  • 键盘快捷键工具Keyboard Maestro mac中文版介绍
  • Dubbo开发系列
  • 周赛372(正难则反、枚举+贪心、异或位运算、离线+单调栈)
  • 存储区域网络(SAN)之FC-SAN和IP-SAN的比较
  • Leetcode_45:跳跃游戏 II
  • 给新手教师的成长建议
  • 新手教师如何迅速成长
  • 竞赛选题 深度学习验证码识别 - 机器视觉 python opencv
  • 提升工作效率,使用AnyTXT Searcher实现远程办公速查公司电脑文件——“cpolar内网穿透”
  • mybatis使用foreach标签实现union集合操作
  • 请问DasViewer是否支持与业务系统集成,将业务的动态的数据实时的展示到三维模型上?
  • [ruby on rails]rack-cors, rack-attack
  • 猫12分类:使用多线程爬取图片的Python程序
  • 《深度学习500问》外链笔记