| | |
| | | import javax.swing.*; |
| | | import javax.swing.SwingUtilities; |
| | | import java.awt.*; |
| | | import java.awt.datatransfer.Clipboard; |
| | | import java.awt.datatransfer.StringSelection; |
| | | import java.awt.event.ActionEvent; |
| | | import java.awt.event.ActionListener; |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.ArrayList; |
| | |
| | | import lujing.Lunjingguihua; |
| | | import lujing.ObstaclePathPlanner; |
| | | import org.locationtech.jts.geom.Coordinate; |
| | | import gecaoji.Device; |
| | | |
| | | /** |
| | | * 生成割草路径页面 |
| | |
| | | widthField = createInfoTextField(widthValue != null ? widthValue : "", true); |
| | | contentPanel.add(createTextFieldSection("割草宽度 (厘米)", widthField)); |
| | | |
| | | // 割草安全距离(只读显示) |
| | | String displaySafetyDistance = "未设置"; |
| | | Device device = Device.getActiveDevice(); |
| | | if (device != null) { |
| | | String safetyDistanceValue = device.getMowingSafetyDistance(); |
| | | if (safetyDistanceValue != null && !"-1".equals(safetyDistanceValue) && !safetyDistanceValue.trim().isEmpty()) { |
| | | try { |
| | | double distanceMeters = Double.parseDouble(safetyDistanceValue.trim()); |
| | | // 如果值大于100,认为是厘米,需要转换为米 |
| | | if (distanceMeters > 100) { |
| | | distanceMeters = distanceMeters / 100.0; |
| | | } |
| | | displaySafetyDistance = String.format("%.2f米", distanceMeters); |
| | | } catch (NumberFormatException e) { |
| | | displaySafetyDistance = "未设置"; |
| | | } |
| | | } |
| | | } |
| | | contentPanel.add(createInfoValueSection("割草安全距离", displaySafetyDistance)); |
| | | |
| | | // 割草模式(只读显示) |
| | | contentPanel.add(createInfoValueSection("割草模式", formatMowingPatternForDialog(modeValue))); |
| | | |
| | |
| | | section.setBackground(BACKGROUND_COLOR); |
| | | section.setAlignmentX(Component.LEFT_ALIGNMENT); |
| | | |
| | | // 创建标题面板,包含标题和复制图标 |
| | | JPanel titlePanel = new JPanel(new BorderLayout()); |
| | | titlePanel.setBackground(BACKGROUND_COLOR); |
| | | titlePanel.setOpaque(false); |
| | | |
| | | JLabel titleLabel = new JLabel(title); |
| | | titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 14)); |
| | | titleLabel.setForeground(TEXT_COLOR); |
| | | section.add(titleLabel, BorderLayout.NORTH); |
| | | titlePanel.add(titleLabel, BorderLayout.WEST); |
| | | |
| | | // 创建复制按钮 |
| | | JButton copyButton = createCopyButton(title, () -> { |
| | | String text = textArea.getText(); |
| | | if (text == null || text.trim().isEmpty()) { |
| | | JOptionPane.showMessageDialog(this, title + " 未设置", "提示", JOptionPane.INFORMATION_MESSAGE); |
| | | return; |
| | | } |
| | | copyToClipboard(text, title); |
| | | }); |
| | | titlePanel.add(copyButton, BorderLayout.EAST); |
| | | |
| | | section.add(titlePanel, BorderLayout.NORTH); |
| | | |
| | | JScrollPane scrollPane = new JScrollPane(textArea); |
| | | scrollPane.setBorder(BorderFactory.createLineBorder(BORDER_COLOR)); |
| | |
| | | section.setBackground(BACKGROUND_COLOR); |
| | | section.setAlignmentX(Component.LEFT_ALIGNMENT); |
| | | |
| | | // 创建标题面板,包含标题和复制图标 |
| | | JPanel titlePanel = new JPanel(new BorderLayout()); |
| | | titlePanel.setBackground(BACKGROUND_COLOR); |
| | | titlePanel.setOpaque(false); |
| | | |
| | | JLabel titleLabel = new JLabel(title); |
| | | titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 14)); |
| | | titleLabel.setForeground(TEXT_COLOR); |
| | | section.add(titleLabel, BorderLayout.NORTH); |
| | | titlePanel.add(titleLabel, BorderLayout.WEST); |
| | | |
| | | // 创建复制按钮 |
| | | JButton copyButton = createCopyButton(title, () -> { |
| | | String text = textField.getText(); |
| | | if (text == null || text.trim().isEmpty()) { |
| | | JOptionPane.showMessageDialog(this, title + " 未设置", "提示", JOptionPane.INFORMATION_MESSAGE); |
| | | return; |
| | | } |
| | | copyToClipboard(text, title); |
| | | }); |
| | | titlePanel.add(copyButton, BorderLayout.EAST); |
| | | |
| | | section.add(titlePanel, BorderLayout.NORTH); |
| | | |
| | | JPanel fieldWrapper = new JPanel(new BorderLayout()); |
| | | fieldWrapper.setBackground(textField.isEditable() ? WHITE : new Color(245, 245, 245)); |
| | |
| | | } |
| | | return "parallel"; |
| | | } |
| | | |
| | | /** |
| | | * 创建复制按钮 |
| | | */ |
| | | private JButton createCopyButton(String title, Runnable copyAction) { |
| | | JButton copyButton = new JButton(); |
| | | Font titleFont = new Font("微软雅黑", Font.BOLD, 14); |
| | | FontMetrics metrics = getFontMetrics(titleFont); |
| | | int iconSize = metrics.getHeight(); // 使用标题字体高度作为图标大小 |
| | | |
| | | // 加载复制图标 |
| | | ImageIcon copyIcon = null; |
| | | ImageIcon successIcon = null; |
| | | try { |
| | | ImageIcon originalCopyIcon = new ImageIcon("image/fuzhi.png"); |
| | | Image scaledCopyImage = originalCopyIcon.getImage().getScaledInstance(iconSize, iconSize, Image.SCALE_SMOOTH); |
| | | copyIcon = new ImageIcon(scaledCopyImage); |
| | | |
| | | // 加载成功图标 |
| | | ImageIcon originalSuccessIcon = new ImageIcon("image/fuzhisucc.png"); |
| | | Image scaledSuccessImage = originalSuccessIcon.getImage().getScaledInstance(iconSize, iconSize, Image.SCALE_SMOOTH); |
| | | successIcon = new ImageIcon(scaledSuccessImage); |
| | | } catch (Exception e) { |
| | | // 如果图片加载失败,使用文本 |
| | | copyButton.setText("复制"); |
| | | copyButton.setFont(new Font("微软雅黑", Font.PLAIN, 12)); |
| | | System.err.println("无法加载复制图标: " + e.getMessage()); |
| | | } |
| | | |
| | | final ImageIcon finalCopyIcon = copyIcon; |
| | | final ImageIcon finalSuccessIcon = successIcon; |
| | | |
| | | copyButton.setIcon(finalCopyIcon); |
| | | copyButton.setContentAreaFilled(false); |
| | | copyButton.setBorder(null); |
| | | copyButton.setFocusPainted(false); |
| | | copyButton.setCursor(new Cursor(Cursor.HAND_CURSOR)); |
| | | copyButton.setToolTipText("复制" + title); |
| | | |
| | | // 添加点击事件 |
| | | copyButton.addActionListener(e -> { |
| | | copyAction.run(); |
| | | // 复制成功后切换图标 |
| | | if (finalSuccessIcon != null) { |
| | | copyButton.setIcon(finalSuccessIcon); |
| | | // 1秒后恢复原图标 |
| | | Timer timer = new Timer(1000, evt -> { |
| | | copyButton.setIcon(finalCopyIcon); |
| | | }); |
| | | timer.setRepeats(false); |
| | | timer.start(); |
| | | } |
| | | }); |
| | | |
| | | return copyButton; |
| | | } |
| | | |
| | | /** |
| | | * 复制文本到剪贴板 |
| | | */ |
| | | private void copyToClipboard(String text, String title) { |
| | | if (text == null || text.trim().isEmpty()) { |
| | | JOptionPane.showMessageDialog(this, title + " 未设置", "提示", JOptionPane.INFORMATION_MESSAGE); |
| | | return; |
| | | } |
| | | |
| | | try { |
| | | StringSelection selection = new StringSelection(text); |
| | | Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); |
| | | clipboard.setContents(selection, selection); |
| | | // 去掉成功提示弹窗 |
| | | } catch (Exception ex) { |
| | | JOptionPane.showMessageDialog(this, "复制失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |