diff --git a/src/ThemeSelectorDialog.java b/src/ThemeSelectorDialog.java new file mode 100644 index 0000000..57c7473 --- /dev/null +++ b/src/ThemeSelectorDialog.java @@ -0,0 +1,97 @@ +package cn.edu.caztc.sokobangame; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class ThemeSelectorDialog extends JDialog { + + public ThemeSelectorDialog(JFrame parent) { + super(parent, "选择主题", true); + setSize(600, 650); + setLocationRelativeTo(parent); + setLayout(new BorderLayout()); + + JPanel mainPanel = new JPanel(new GridLayout(MapConfig.THEMES.length, 1, 10, 10)); + mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); + + ButtonGroup group = new ButtonGroup(); + String[] iconNames = {"墙", "地板", "空箱子", "箱子", "箱子点", "玩家一", "玩家二"}; + + for (int i = 0; i < MapConfig.THEMES.length; i++) { + // 主主题面板 + JPanel themePanel = new JPanel(new BorderLayout()); + themePanel.setBorder(BorderFactory.createTitledBorder(MapConfig.THEMES[i])); + + + // 图标面板(包含7个图标) + JPanel iconsPanel = new JPanel(new GridLayout(1, 7, 5, 5)); + + for (int j = 0; j < 7; j++) { + // 单个图标的容器面板 + JPanel iconPanel = new JPanel(new BorderLayout()); + iconPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); + + // 缩放并添加图片 + ImageIcon icon = getThemeIcon(i, j); + ImageIcon scaledIcon = new ImageIcon(icon.getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH)); + JLabel iconLabel = new JLabel(scaledIcon, JLabel.CENTER); + iconPanel.add(iconLabel, BorderLayout.CENTER); + + // 添加图片名称标签 + JLabel nameLabel = new JLabel(iconNames[j], JLabel.CENTER); + iconPanel.add(nameLabel, BorderLayout.SOUTH); + + iconsPanel.add(iconPanel); + } + + // 添加单选按钮 + JRadioButton radioButton = new JRadioButton("选择此主题"); + radioButton.setSelected(i == MapConfig.ThemeManager.getCurrentTheme()); + radioButton.addActionListener(new ThemeSelectListener(i)); + group.add(radioButton); + + // 将图标面板和单选按钮放入容器 + JPanel contentPanel = new JPanel(new BorderLayout()); + contentPanel.add(iconsPanel, BorderLayout.CENTER); + contentPanel.add(radioButton, BorderLayout.SOUTH); + + themePanel.add(contentPanel, BorderLayout.CENTER); + mainPanel.add(themePanel); + } + + add(mainPanel, BorderLayout.CENTER); + + JButton closeButton = new JButton("关闭"); + closeButton.addActionListener(e -> dispose()); + add(closeButton, BorderLayout.SOUTH); + } + + private ImageIcon getThemeIcon(int themeIndex, int iconIndex) { + int savedTheme = MapConfig.ThemeManager.getCurrentTheme(); + MapConfig.ThemeManager.setCurrentTheme(themeIndex); + ImageIcon icon = MapConfig.getThemeIcon(iconIndex); + MapConfig.ThemeManager.setCurrentTheme(savedTheme); + return icon; + } + + private class ThemeSelectListener implements ActionListener { + private int themeIndex; + + public ThemeSelectListener(int themeIndex) { + this.themeIndex = themeIndex; + } + + @Override + public void actionPerformed(ActionEvent e) { + MapConfig.ThemeManager.setCurrentTheme(themeIndex); + // 刷新游戏界面 + if (SwingUtilities.getWindowAncestor((Component)e.getSource()) instanceof MainGame) { + ((MainGame)SwingUtilities.getWindowAncestor((Component)e.getSource())).refreshGame(); + } else if (SwingUtilities.getWindowAncestor((Component)e.getSource()) instanceof DualGame) { + ((DualGame)SwingUtilities.getWindowAncestor((Component)e.getSource())).refreshGameDisplay(); + } + } + } +} \ No newline at end of file