| | |
| | | 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 lujing.Qufenxingzhuang; |
| | | import lujing.AoxinglujingNoObstacle; |
| | | import lujing.YixinglujingNoObstacle; |
| | | import publicway.Fuzhibutton; |
| | | import lujing.AoxinglujingHaveObstacel; |
| | | import lujing.YixinglujingHaveObstacel; |
| | | import org.locationtech.jts.geom.Coordinate; |
| | |
| | | |
| | | // 地块边界 |
| | | boundaryArea = createInfoTextArea(boundaryValue != null ? boundaryValue : "", true, 6); |
| | | contentPanel.add(createTextAreaSection("地块边界", boundaryArea)); |
| | | String boundaryTitle = "地块边界"; |
| | | if (boundaryValue != null && !boundaryValue.trim().isEmpty() && !"-1".equals(boundaryValue.trim())) { |
| | | int boundaryCount = boundaryValue.split(";").length; |
| | | boundaryTitle = "地块边界 (" + boundaryCount + "点)"; |
| | | } |
| | | contentPanel.add(createTextAreaSection(boundaryTitle, boundaryArea)); |
| | | |
| | | // 障碍物坐标 |
| | | obstacleArea = createInfoTextArea(obstacleValue != null ? obstacleValue : "", true, 6); |
| | | contentPanel.add(createTextAreaSection("障碍物坐标", obstacleArea)); |
| | | String obstacleTitle = "障碍物坐标"; |
| | | if (obstacleValue != null && !obstacleValue.trim().isEmpty() && !"-1".equals(obstacleValue.trim())) { |
| | | // 障碍物坐标格式可能是空格分隔的多个障碍物,每个障碍物用分号分隔坐标点 |
| | | // 计算所有障碍物的总坐标点数 |
| | | String[] obstacles = obstacleValue.trim().split("\\s+"); |
| | | int totalObstaclePoints = 0; |
| | | for (String obstacle : obstacles) { |
| | | if (obstacle != null && !obstacle.trim().isEmpty()) { |
| | | totalObstaclePoints += obstacle.split(";").length; |
| | | } |
| | | } |
| | | if (totalObstaclePoints > 0) { |
| | | obstacleTitle = "障碍物坐标 (" + totalObstaclePoints + "点)"; |
| | | } |
| | | } |
| | | contentPanel.add(createTextAreaSection(obstacleTitle, obstacleArea)); |
| | | |
| | | // 割草宽度 |
| | | widthField = createInfoTextField(widthValue != null ? widthValue : "", true); |
| | | contentPanel.add(createTextFieldSection("割草宽度 (厘米)", widthField)); |
| | | contentPanel.add(createTextFieldSection("割草宽度 (cm)", widthField)); |
| | | |
| | | // 割草安全距离(只读显示) |
| | | // 优先从Dikuai对象获取,如果Dikuai中没有,再从Device获取 |
| | | 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 = "未设置"; |
| | | String safetyDistanceValue = null; |
| | | |
| | | // 首先尝试从Dikuai对象获取 |
| | | if (dikuai != null) { |
| | | safetyDistanceValue = dikuai.getMowingSafetyDistance(); |
| | | } |
| | | |
| | | // 如果Dikuai中没有,从Device获取 |
| | | if ((safetyDistanceValue == null || "-1".equals(safetyDistanceValue) || safetyDistanceValue.trim().isEmpty())) { |
| | | Device device = Device.getActiveDevice(); |
| | | if (device != null) { |
| | | 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(Locale.US, "%.2fm", distanceMeters); |
| | | } catch (NumberFormatException e) { |
| | | displaySafetyDistance = "未设置"; |
| | | } |
| | | } |
| | | contentPanel.add(createInfoValueSection("割草安全距离", displaySafetyDistance)); |
| | |
| | | String existingPath = prepareCoordinateForEditor(dikuai.getPlannedPath()); |
| | | String pathSeed = initialGeneratedPath != null ? initialGeneratedPath : existingPath; |
| | | pathArea = createInfoTextArea(pathSeed != null ? pathSeed : "", true, 10); |
| | | contentPanel.add(createTextAreaSection("割草路径坐标", pathArea)); |
| | | String pathTitle = "割草路径坐标"; |
| | | if (pathSeed != null && !pathSeed.trim().isEmpty() && !"-1".equals(pathSeed.trim())) { |
| | | int pathCount = pathSeed.split(";").length; |
| | | pathTitle = "割草路径坐标 (" + pathCount + "点)"; |
| | | } |
| | | contentPanel.add(createTextAreaSection(pathTitle, pathArea)); |
| | | |
| | | JScrollPane dialogScrollPane = new JScrollPane(contentPanel); |
| | | dialogScrollPane.setBorder(BorderFactory.createEmptyBorder()); |
| | |
| | | String rawWidthInput = widthField.getText() != null ? widthField.getText().trim() : ""; |
| | | String widthSanitized = sanitizeWidthString(widthField.getText()); |
| | | if (widthSanitized == null) { |
| | | String message = rawWidthInput.isEmpty() ? "请先设置割草宽度(厘米)" : "割草宽度格式不正确"; |
| | | String message = rawWidthInput.isEmpty() ? "请先设置割草宽度(cm)" : "割草宽度格式不正确"; |
| | | JOptionPane.showMessageDialog(this, message, "提示", JOptionPane.WARNING_MESSAGE); |
| | | return; |
| | | } |
| | |
| | | String widthStr = sanitizeWidthString(widthCmInput); |
| | | if (widthStr == null) { |
| | | if (showMessages) { |
| | | String message = rawWidth.isEmpty() ? "请先设置割草宽度(厘米)" : "割草宽度格式不正确"; |
| | | String message = rawWidth.isEmpty() ? "请先设置割草宽度(cm)" : "割草宽度格式不正确"; |
| | | JOptionPane.showMessageDialog(parentComponent, message, "提示", JOptionPane.WARNING_MESSAGE); |
| | | } |
| | | return null; |
| | |
| | | // 异形地块,无障碍物 -> 调用 YixinglujingNoObstacle |
| | | // 注意:如果该类还没有实现,这里会抛出异常或返回null |
| | | try { |
| | | // 假设 YixinglujingNoObstacle 有类似的方法签名 |
| | | // 如果类还没有实现,可能需要使用原来的方法作为后备 |
| | | generated = YixinglujingNoObstacle.planPath(boundary, plannerWidth, safetyMarginStr); |
| | | // 调用 YixinglujingNoObstacle.planPath 获取路径段列表 |
| | | List<YixinglujingNoObstacle.PathSegment> segments = |
| | | YixinglujingNoObstacle.planPath(boundary, plannerWidth, safetyMarginStr); |
| | | // 格式化路径段列表为字符串 |
| | | generated = formatYixingPathSegments(segments); |
| | | } catch (Exception e) { |
| | | // 如果类还没有实现,使用原来的方法作为后备 |
| | | if (showMessages) { |
| | |
| | | |
| | | /** |
| | | * 获取安全距离字符串(米) |
| | | * 优先从Dikuai对象获取,如果Dikuai中没有,再从Device获取 |
| | | */ |
| | | private String getSafetyDistanceString() { |
| | | 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; |
| | | } |
| | | return BigDecimal.valueOf(distanceMeters) |
| | | .setScale(3, RoundingMode.HALF_UP) |
| | | .stripTrailingZeros() |
| | | .toPlainString(); |
| | | } catch (NumberFormatException e) { |
| | | // 解析失败,返回null,使用默认值 |
| | | String safetyDistanceValue = null; |
| | | |
| | | // 首先尝试从Dikuai对象获取 |
| | | if (dikuai != null) { |
| | | safetyDistanceValue = dikuai.getMowingSafetyDistance(); |
| | | } |
| | | |
| | | // 如果Dikuai中没有,从Device获取 |
| | | if ((safetyDistanceValue == null || "-1".equals(safetyDistanceValue) || safetyDistanceValue.trim().isEmpty())) { |
| | | Device device = Device.getActiveDevice(); |
| | | if (device != null) { |
| | | 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; |
| | | } |
| | | return BigDecimal.valueOf(distanceMeters) |
| | | .setScale(3, RoundingMode.HALF_UP) |
| | | .stripTrailingZeros() |
| | | .toPlainString(); |
| | | } catch (NumberFormatException e) { |
| | | // 解析失败,返回null,使用默认值 |
| | | } |
| | | } |
| | | return null; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 格式化 YixinglujingNoObstacle.PathSegment 列表为坐标字符串 |
| | | */ |
| | | private String formatYixingPathSegments(List<YixinglujingNoObstacle.PathSegment> segments) { |
| | | if (segments == null || segments.isEmpty()) { |
| | | return ""; |
| | | } |
| | | StringBuilder sb = new StringBuilder(); |
| | | YixinglujingNoObstacle.Point last = null; |
| | | for (YixinglujingNoObstacle.PathSegment segment : segments) { |
| | | // 只添加割草工作段,跳过过渡段 |
| | | if (segment.isMowing) { |
| | | // 如果起点与上一个终点不同,添加起点 |
| | | if (last == null || !equalsYixingPoint(last, segment.start)) { |
| | | appendYixingPoint(sb, segment.start); |
| | | } |
| | | // 添加终点 |
| | | appendYixingPoint(sb, segment.end); |
| | | last = segment.end; |
| | | } |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 比较两个点是否相同(使用小的容差) |
| | | */ |
| | | private boolean equals2D(AoxinglujingNoObstacle.Point p1, AoxinglujingNoObstacle.Point p2) { |
| | |
| | | sb.append(String.format(Locale.US, "%.6f,%.6f", point.x, point.y)); |
| | | } |
| | | |
| | | /** |
| | | * 比较两个 YixinglujingNoObstacle.Point 是否相同(使用小的容差) |
| | | */ |
| | | private boolean equalsYixingPoint(YixinglujingNoObstacle.Point p1, YixinglujingNoObstacle.Point p2) { |
| | | if (p1 == null || p2 == null) { |
| | | return p1 == p2; |
| | | } |
| | | double tolerance = 1e-6; |
| | | return Math.abs(p1.x - p2.x) < tolerance && Math.abs(p1.y - p2.y) < tolerance; |
| | | } |
| | | |
| | | /** |
| | | * 添加 YixinglujingNoObstacle.Point 到字符串构建器 |
| | | */ |
| | | private void appendYixingPoint(StringBuilder sb, YixinglujingNoObstacle.Point point) { |
| | | if (sb.length() > 0) { |
| | | sb.append(";"); |
| | | } |
| | | sb.append(String.format(Locale.US, "%.6f,%.6f", point.x, point.y)); |
| | | } |
| | | |
| | | // ========== UI辅助方法 ========== |
| | | |
| | | private JTextArea createInfoTextArea(String text, boolean editable, int rows) { |
| | |
| | | titleLabel.setForeground(TEXT_COLOR); |
| | | 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); |
| | | }); |
| | | // 创建复制按钮(使用 Fuzhibutton) |
| | | JButton copyButton = Fuzhibutton.createCopyButton( |
| | | () -> { |
| | | String text = textArea.getText(); |
| | | if (text == null || text.trim().isEmpty() || "-1".equals(text.trim())) { |
| | | return null; // 返回null会触发"未设置"提示 |
| | | } |
| | | return text; |
| | | }, |
| | | "复制" + title, |
| | | new Color(230, 250, 240) |
| | | ); |
| | | titlePanel.add(copyButton, BorderLayout.EAST); |
| | | |
| | | section.add(titlePanel, BorderLayout.NORTH); |
| | |
| | | titleLabel.setForeground(TEXT_COLOR); |
| | | 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); |
| | | }); |
| | | // 创建复制按钮(使用 Fuzhibutton) |
| | | JButton copyButton = Fuzhibutton.createCopyButton( |
| | | () -> { |
| | | String text = textField.getText(); |
| | | if (text == null || text.trim().isEmpty() || "-1".equals(text.trim())) { |
| | | return null; // 返回null会触发"未设置"提示 |
| | | } |
| | | return text; |
| | | }, |
| | | "复制" + title, |
| | | new Color(230, 250, 240) |
| | | ); |
| | | titlePanel.add(copyButton, BorderLayout.EAST); |
| | | |
| | | section.add(titlePanel, BorderLayout.NORTH); |
| | |
| | | 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); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |