parent
470af860b8
commit
27291b14c6
@ -0,0 +1,172 @@
|
|||||||
|
package cn.edu.caztc.sokobangame;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class ThemeSwitcher {
|
||||||
|
// 当前主题索引
|
||||||
|
private static int currentTheme = 1;
|
||||||
|
// 主题图片缓存
|
||||||
|
private static Map<Integer, ImageIcon[]> themeIcons = new HashMap<>();
|
||||||
|
|
||||||
|
// 主题名称
|
||||||
|
private static final String[] THEME_NAMES = {
|
||||||
|
"默认主题",
|
||||||
|
"chiikawa主题",
|
||||||
|
"MINECRAFT主题",
|
||||||
|
"粉色气泡主题"
|
||||||
|
};
|
||||||
|
|
||||||
|
// 初始化主题图片
|
||||||
|
static {
|
||||||
|
loadThemeIcons();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加载所有主题的图片
|
||||||
|
private static void loadThemeIcons() {
|
||||||
|
for (int theme = 1; theme <= THEME_NAMES.length; theme++) {
|
||||||
|
ImageIcon[] icons = new ImageIcon[7];
|
||||||
|
String suffix = theme == 1 ? "" : "(" + (theme-1) + ")";
|
||||||
|
|
||||||
|
// 修改图片路径为images文件夹
|
||||||
|
icons[0] = new ImageIcon("images/墙" + suffix + ".png");
|
||||||
|
icons[1] = new ImageIcon("images/地板" + suffix + ".png");
|
||||||
|
icons[2] = new ImageIcon("images/空箱子" + suffix + ".png");
|
||||||
|
icons[3] = new ImageIcon("images/箱子" + suffix + ".png");
|
||||||
|
icons[4] = new ImageIcon("images/箱子点" + suffix + ".png");
|
||||||
|
icons[5] = new ImageIcon("images/player" + suffix + ".png");
|
||||||
|
icons[6] = new ImageIcon("images/player2" + suffix + ".png");
|
||||||
|
|
||||||
|
themeIcons.put(theme, icons);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建切换主题菜单项
|
||||||
|
public static JMenuItem createThemeMenuItem() {
|
||||||
|
JMenuItem themeItem = new JMenuItem("切换主题");
|
||||||
|
themeItem.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
showThemeDialog();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return themeItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 显示主题选择对话框
|
||||||
|
private static void showThemeDialog() {
|
||||||
|
JDialog dialog = new JDialog();
|
||||||
|
dialog.setTitle("切换主题");
|
||||||
|
dialog.setSize(600, 700);
|
||||||
|
dialog.setLayout(new BorderLayout());
|
||||||
|
dialog.setLocationRelativeTo(null);
|
||||||
|
|
||||||
|
JPanel mainPanel = new JPanel(new GridLayout(THEME_NAMES.length, 1, 10, 10));
|
||||||
|
mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
|
||||||
|
|
||||||
|
for (int i = 0; i < THEME_NAMES.length; i++) {
|
||||||
|
JPanel themePanel = createThemePanel(i + 1, THEME_NAMES[i]);
|
||||||
|
mainPanel.add(themePanel);
|
||||||
|
}
|
||||||
|
|
||||||
|
JScrollPane scrollPane = new JScrollPane(mainPanel);
|
||||||
|
dialog.add(scrollPane, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
JButton closeButton = new JButton("关闭");
|
||||||
|
closeButton.addActionListener(e -> dialog.dispose());
|
||||||
|
JPanel buttonPanel = new JPanel();
|
||||||
|
buttonPanel.add(closeButton);
|
||||||
|
dialog.add(buttonPanel, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
dialog.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建单个主题展示面板
|
||||||
|
private static JPanel createThemePanel(int themeIndex, String themeName) {
|
||||||
|
JPanel panel = new JPanel(new BorderLayout());
|
||||||
|
panel.setBorder(BorderFactory.createTitledBorder(themeName));
|
||||||
|
|
||||||
|
JLabel titleLabel = new JLabel(themeName, JLabel.CENTER);
|
||||||
|
titleLabel.setFont(new Font("宋体", Font.BOLD, 16));
|
||||||
|
panel.add(titleLabel, BorderLayout.NORTH);
|
||||||
|
|
||||||
|
JPanel iconsPanel = new JPanel(new GridLayout(1, 7, 5, 5));
|
||||||
|
ImageIcon[] icons = themeIcons.get(themeIndex);
|
||||||
|
|
||||||
|
// 添加素材图片
|
||||||
|
String[] iconNames = {"墙", "地板", "空箱子", "箱子", "箱子点", "玩家一", "玩家二"};
|
||||||
|
for (int i = 0; i < 7; i++) {
|
||||||
|
JPanel iconPanel = new JPanel(new BorderLayout());
|
||||||
|
iconPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
||||||
|
|
||||||
|
// 缩放图片以适应显示
|
||||||
|
ImageIcon scaledIcon = scaleIcon(icons[i], 50, 50);
|
||||||
|
JLabel iconLabel = new JLabel(scaledIcon);
|
||||||
|
iconLabel.setHorizontalAlignment(JLabel.CENTER);
|
||||||
|
iconPanel.add(iconLabel, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
JLabel nameLabel = new JLabel(iconNames[i], JLabel.CENTER);
|
||||||
|
iconPanel.add(nameLabel, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
iconsPanel.add(iconPanel);
|
||||||
|
}
|
||||||
|
|
||||||
|
JButton selectButton = new JButton("选择此主题");
|
||||||
|
selectButton.addActionListener(e -> {
|
||||||
|
setCurrentTheme(themeIndex);
|
||||||
|
JOptionPane.showMessageDialog(null, "已切换至" + themeName, "提示", JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
});
|
||||||
|
|
||||||
|
JPanel centerPanel = new JPanel(new BorderLayout());
|
||||||
|
centerPanel.add(iconsPanel, BorderLayout.CENTER);
|
||||||
|
centerPanel.add(selectButton, BorderLayout.SOUTH);
|
||||||
|
panel.add(centerPanel, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
return panel;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 缩放图标
|
||||||
|
private static ImageIcon scaleIcon(ImageIcon icon, int width, int height) {
|
||||||
|
if (icon == null) return null;
|
||||||
|
Image image = icon.getImage();
|
||||||
|
Image scaledImage = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
|
||||||
|
return new ImageIcon(scaledImage);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置当前主题
|
||||||
|
public static void setCurrentTheme(int theme) {
|
||||||
|
currentTheme = theme;
|
||||||
|
updateGameTheme();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新游戏主题
|
||||||
|
private static void updateGameTheme() {
|
||||||
|
ImageIcon[] currentIcons = themeIcons.get(currentTheme);
|
||||||
|
|
||||||
|
// 更新游戏中的图标
|
||||||
|
if (MainGame.getInstance() != null) {
|
||||||
|
MainGame.getInstance().updateTheme(currentIcons);
|
||||||
|
}
|
||||||
|
if (DualGame.getInstance() != null) {
|
||||||
|
DualGame.getInstance().updateTheme(currentIcons);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取当前主题的图标
|
||||||
|
public static ImageIcon getThemeIcon(int index) {
|
||||||
|
ImageIcon[] icons = themeIcons.get(currentTheme);
|
||||||
|
if (icons != null && index >= 0 && index < icons.length) {
|
||||||
|
return icons[index];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加方法获取当前主题的所有图标
|
||||||
|
public static ImageIcon[] getCurrentThemeIcons() {
|
||||||
|
return themeIcons.get(currentTheme);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue