826220679@qq.com
2 天以前 48ee74129bb09a817a0bbbabe860c4007b74c66b
src/zhuye/LegendDialog.java
@@ -8,10 +8,11 @@
public class LegendDialog extends JDialog {
    public LegendDialog(Component parent, Color ignoredThemeColor) {
        super(parent != null ? (JFrame) SwingUtilities.getWindowAncestor(parent) : null,
              "图例", true);
    // 使用统一宽度,高度缩减为一半
    initializeDialog(UIConfig.DIALOG_WIDTH, UIConfig.DIALOG_HEIGHT / 2);
      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); // 居中显示
@@ -31,13 +32,6 @@
        mainPanel.setBackground(Color.WHITE);
        mainPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 10, 15));
        
        // 标题 - 修改为"图例"
        JLabel titleLabel = new JLabel("图例", JLabel.CENTER);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        titleLabel.setForeground(new Color(60, 60, 60));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
        mainPanel.add(titleLabel, BorderLayout.NORTH);
        // 图例内容面板 - 直接添加,不使用滚动条
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
@@ -54,6 +48,7 @@
            {"路径方向", "255,0,0", "arrow"},
            {"障碍物区域", "255,0,0", "obstacle_fill"},
            {"割草机位置", "0,150,0", "mow"},
            {"往返路径", "0,0,0", "railway"},
            {"割草轨迹", "100,150,200", "trail"}
        };
        
@@ -67,7 +62,7 @@
            contentPanel.remove(contentPanel.getComponentCount() - 1);
        }
        
        // 直接添加内容面板,不使用滚动条
        // 添加图例内容面板
        mainPanel.add(contentPanel, BorderLayout.CENTER);
        
        getContentPane().add(mainPanel);
@@ -126,11 +121,30 @@
                        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);
                        ImageIcon icon = loadIcon("image/gecaoji.png", size + 8, size + 8);
                        if (icon != null) {
                            icon.paintIcon(this, g2d, x - 4, y - 4);
                        } else {
                            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 "railway":
                        // 1. 绘制底层黑色实线
                        g2d.setColor(Color.BLACK);
                        g2d.setStroke(new BasicStroke(3.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
                        g2d.drawLine(x, y + size/2, x + size, y + size/2);
                        // 2. 绘制顶层白色虚线
                        float dashLen = 3.0f * 2.0f;
                        float dashSpace = 3.0f * 2.0f;
                        float[] dashPattern = {dashLen, dashSpace};
                        g2d.setColor(Color.WHITE);
                        g2d.setStroke(new BasicStroke(1.2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 10.0f, dashPattern, 0.0f));
                        g2d.drawLine(x, y + size/2, x + size, y + size/2);
                        break;
                    case "trail":
                        g2d.setColor(itemColor);
@@ -160,4 +174,37 @@
        
        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;
    }
}