package zhuye; import ui.UIConfig; import javax.swing.*; import java.awt.*; public class LegendDialog extends JDialog { public LegendDialog(Component parent, Color ignoredThemeColor) { super(parent != null ? (JFrame) SwingUtilities.getWindowAncestor(parent) : null, "图例", true); int adjustedWidth = (int) Math.round(UIConfig.DIALOG_WIDTH * 0.8); int adjustedHeight = (int) Math.round(UIConfig.DIALOG_HEIGHT * 0.4); initializeDialog(adjustedWidth, adjustedHeight); initializeLegendContent(); if (parent == null) { setLocationRelativeTo(null); // 居中显示 } } private void initializeDialog(int width, int height) { setSize(width, height); setLocationRelativeTo(getParent()); setResizable(false); getContentPane().setBackground(Color.WHITE); } private void initializeLegendContent() { // 主面板使用BorderLayout JPanel mainPanel = new JPanel(new BorderLayout()); mainPanel.setBackground(Color.WHITE); mainPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 10, 15)); // 计算图例内容面板的宽度(用于设置图标尺寸) // 图例对话框宽度 = DIALOG_WIDTH * 0.8 // 主面板左右边框各15像素,图例内容面板左右内边距各10像素 int adjustedWidth = (int) Math.round(UIConfig.DIALOG_WIDTH * 0.8); int iconSize = adjustedWidth - 30 - 20; // 减去主面板左右边框(15*2)和图例内容面板左右内边距(10*2) // 创建割草机图标面板 JPanel iconPanel = new JPanel(new BorderLayout()); iconPanel.setBackground(Color.WHITE); iconPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0)); // 底部间距10像素 JLabel gecaojiLabel = new JLabel(); gecaojiLabel.setHorizontalAlignment(SwingConstants.CENTER); ImageIcon gecaojiIcon = loadIcon("image/gecaoji.png", iconSize, iconSize); if (gecaojiIcon != null) { gecaojiLabel.setIcon(gecaojiIcon); } else { // 如果图标加载失败,显示占位文本 gecaojiLabel.setText("割草机图标"); gecaojiLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12)); } iconPanel.add(gecaojiLabel, BorderLayout.CENTER); // 图例内容面板 - 直接添加,不使用滚动条 JPanel contentPanel = new JPanel(); contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS)); contentPanel.setBackground(Color.WHITE); contentPanel.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(new Color(220, 220, 220), 1), BorderFactory.createEmptyBorder(10, 10, 10, 10) )); String[][] legendItems = { {"草坪区域", "144,238,144", "fill"}, {"区域顶点", "128,0,128", "point"}, {"割草机路径", "255,0,0", "line"}, {"路径方向", "255,0,0", "arrow"}, {"障碍物区域", "255,0,0", "obstacle_fill"}, {"割草机位置", "0,150,0", "mow"}, {"割草轨迹", "100,150,200", "trail"} }; for (String[] item : legendItems) { contentPanel.add(createLegendItem(item[0], item[1], item[2])); contentPanel.add(Box.createRigidArea(new Dimension(0, 6))); // 缩小行间距 } // 移除最后一个间距组件,避免底部多余空白 if (contentPanel.getComponentCount() > 0) { contentPanel.remove(contentPanel.getComponentCount() - 1); } // 添加图标面板和图例内容面板 mainPanel.add(iconPanel, BorderLayout.NORTH); mainPanel.add(contentPanel, BorderLayout.CENTER); getContentPane().add(mainPanel); } private JPanel createLegendItem(String text, String color, String type) { JPanel itemPanel = new JPanel(new BorderLayout(8, 0)); // 减小组件间距 itemPanel.setBackground(Color.WHITE); itemPanel.setMaximumSize(new Dimension(160, 25)); // 减小高度 String[] rgb = color.split(","); Color itemColor = new Color( Integer.parseInt(rgb[0]), Integer.parseInt(rgb[1]), Integer.parseInt(rgb[2]) ); // 图标面板 JPanel iconPanel = new JPanel() { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int size = 16; // 缩小图标尺寸 int x = (getWidth() - size) / 2; int y = (getHeight() - size) / 2; switch (type) { case "fill": g2d.setColor(itemColor); g2d.fillRect(x, y, size, size); g2d.setColor(new Color(100, 100, 100)); g2d.setStroke(new BasicStroke(1)); g2d.drawRect(x, y, size, size); break; case "point": g2d.setColor(itemColor); g2d.fillOval(x, y, size, size); g2d.setColor(new Color(100, 100, 100)); g2d.setStroke(new BasicStroke(1)); g2d.drawOval(x, y, size, size); break; case "line": g2d.setColor(itemColor); g2d.setStroke(new BasicStroke(2)); // 减小线宽 g2d.drawLine(x, y + size/2, x + size, y + size/2); break; case "arrow": g2d.setColor(itemColor); g2d.setStroke(new BasicStroke(1.5f)); // 减小线宽 g2d.drawLine(x, y + size/2, x + size, y + size/2); int[] xPoints = {x + size, x + size - 4, x + size - 4}; // 缩小箭头 int[] yPoints = {y + size/2, y + size/2 - 3, y + size/2 + 3}; g2d.fillPolygon(xPoints, yPoints, 3); break; case "mow": g2d.setColor(itemColor); g2d.fillOval(x, y, size, size); g2d.setColor(new Color(100, 100, 100)); g2d.setStroke(new BasicStroke(1)); g2d.drawOval(x, y, size, size); break; case "trail": g2d.setColor(itemColor); g2d.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); // 减小线宽 g2d.drawLine(x, y + size/2, x + size, y + size/2); break; case "obstacle_fill": g2d.setColor(new Color(255, 0, 0, 77)); g2d.fillRect(x, y, size, size); g2d.setColor(Color.RED); g2d.setStroke(new BasicStroke(1.5f)); // 减小线宽 g2d.drawRect(x, y, size, size); break; } } }; iconPanel.setPreferredSize(new Dimension(35, 25)); // 缩小图标面板 iconPanel.setBackground(Color.WHITE); // 文本标签 JLabel textLabel = new JLabel(text); textLabel.setFont(new Font("微软雅黑", Font.PLAIN, 11)); // 缩小字体 textLabel.setForeground(new Color(80, 80, 80)); itemPanel.add(iconPanel, BorderLayout.WEST); itemPanel.add(textLabel, BorderLayout.CENTER); return itemPanel; } /** * 加载并缩放图标 * @param iconPath 图标路径 * @param width 目标宽度 * @param height 目标高度 * @return 缩放后的图标 */ private ImageIcon loadIcon(String iconPath, int width, int height) { try { java.net.URL imgURL = getClass().getClassLoader().getResource(iconPath); if (imgURL == null) { // 尝试从文件系统加载 java.io.File imgFile = new java.io.File(iconPath); if (imgFile.exists()) { ImageIcon originalIcon = new ImageIcon(imgFile.getAbsolutePath()); Image scaledImage = originalIcon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH); ImageIcon scaledIcon = new ImageIcon(scaledImage); scaledIcon.setDescription(iconPath); return scaledIcon; } } else { ImageIcon originalIcon = new ImageIcon(imgURL); Image scaledImage = originalIcon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH); ImageIcon scaledIcon = new ImageIcon(scaledImage); scaledIcon.setDescription(iconPath); return scaledIcon; } } catch (Exception e) { System.err.println("无法加载图标: " + iconPath + " - " + e.getMessage()); } return null; } }