From b272034a1fdbfe32b355fc6c264a4c45df107190 Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期二, 23 十二月 2025 14:55:03 +0800
Subject: [PATCH] 优化了新增地块功能

---
 src/lujing/MowingPathGenerationPage.java |  172 +++++++++++++++++++++++++--------------------------------
 1 files changed, 75 insertions(+), 97 deletions(-)

diff --git a/src/lujing/MowingPathGenerationPage.java b/src/lujing/MowingPathGenerationPage.java
index 73a8fde..de7c608 100644
--- a/src/lujing/MowingPathGenerationPage.java
+++ b/src/lujing/MowingPathGenerationPage.java
@@ -3,8 +3,6 @@
 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;
@@ -18,6 +16,7 @@
 import lujing.Qufenxingzhuang;
 import lujing.AoxinglujingNoObstacle;
 import lujing.YixinglujingNoObstacle;
+import publicway.Fuzhibutton;
 import lujing.AoxinglujingHaveObstacel;
 import lujing.YixinglujingHaveObstacel;
 import org.locationtech.jts.geom.Coordinate;
@@ -541,9 +540,11 @@
                     // 寮傚舰鍦板潡锛屾棤闅滅鐗� -> 璋冪敤 YixinglujingNoObstacle
                     // 娉ㄦ剰锛氬鏋滆绫昏繕娌℃湁瀹炵幇锛岃繖閲屼細鎶涘嚭寮傚父鎴栬繑鍥瀗ull
                     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) {
@@ -712,6 +713,30 @@
     }
     
     /**
+     * 鏍煎紡鍖� 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) {
@@ -732,6 +757,27 @@
         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) {
@@ -762,15 +808,18 @@
         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);
@@ -812,15 +861,18 @@
         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);
@@ -962,80 +1014,6 @@
         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);
-        }
-    }
 }
 
 

--
Gitblit v1.10.0