张世豪
2025-12-01 d23f280e37080cb9b5934350cc0fafb2c68421d5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package zhuye;
 
import ui.UIConfig;
 
import javax.swing.*;
import java.awt.*;
 
public class LegendDialog extends JDialog {
    private final Color THEME_COLOR;
    
    public LegendDialog(Component parent, Color themeColor) {
        super(parent != null ? (JFrame) SwingUtilities.getWindowAncestor(parent) : null, 
              "图例", true);
    this.THEME_COLOR = themeColor;
    // 使用统一对话框尺寸(6.5 寸竖屏)
    initializeDialog(UIConfig.DIALOG_WIDTH, UIConfig.DIALOG_HEIGHT);
        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));
        
        // 标题 - 修改为"图例"
        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));
        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(contentPanel, BorderLayout.CENTER);
        
        // 底部按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        buttonPanel.setBackground(Color.WHITE);
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
        
        // 美化关闭按钮
        JButton closeButton = new JButton("关闭") {
            @Override
            protected void paintComponent(Graphics g) {
                Graphics2D g2 = (Graphics2D) g.create();
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                
                // 绘制圆角矩形背景
                g2.setColor(THEME_COLOR);
                g2.fillRoundRect(0, 0, getWidth(), getHeight(), 15, 15);
                
                // 绘制边框
                g2.setColor(THEME_COLOR.darker());
                g2.setStroke(new BasicStroke(1.5f));
                g2.drawRoundRect(0, 0, getWidth()-1, getHeight()-1, 15, 15);
                
                // 绘制文字
                g2.setColor(Color.WHITE);
                g2.setFont(getFont());
                FontMetrics fm = g2.getFontMetrics();
                int x = (getWidth() - fm.stringWidth(getText())) / 2;
                int y = (getHeight() - fm.getHeight()) / 2 + fm.getAscent();
                g2.drawString(getText(), x, y);
                
                g2.dispose();
            }
        };
        
        closeButton.setFont(new Font("微软雅黑", Font.BOLD, 12));
        closeButton.setForeground(Color.WHITE);
        closeButton.setBackground(THEME_COLOR);
        closeButton.setFocusPainted(false);
        closeButton.setBorder(BorderFactory.createEmptyBorder(5, 15, 5, 15));
        closeButton.setContentAreaFilled(false);
        closeButton.setPreferredSize(new Dimension(70, 28));
        closeButton.addActionListener(e -> dispose());
        
        // 添加鼠标悬停效果
        closeButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                closeButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                closeButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            }
        });
        
        buttonPanel.add(closeButton);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
        
        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;
    }
}