From b315a6943e6c0d6bdf0d5f7565c570d719154d6c Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期三, 17 十二月 2025 14:56:58 +0800
Subject: [PATCH] 新增了障碍物管理页面

---
 src/zhuye/MapRenderer.java |  209 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 201 insertions(+), 8 deletions(-)

diff --git a/src/zhuye/MapRenderer.java b/src/zhuye/MapRenderer.java
index c2c3392..3c79352 100644
--- a/src/zhuye/MapRenderer.java
+++ b/src/zhuye/MapRenderer.java
@@ -18,6 +18,7 @@
 import java.util.List;
 import java.util.Locale;
 import java.util.Set;
+import set.Setsys;
 import gecaoji.Device;
 import gecaoji.Gecaoji;
 import gecaoji.GecaojiMeg;
@@ -35,13 +36,15 @@
  */
 public class MapRenderer {
     // 瑙嗗浘鍙樻崲鍙傛暟
-    private double scale = 1.0;
+    private static final double DEFAULT_SCALE = 20.0; // 榛樿缂╂斁姣斾緥
+    private double scale = DEFAULT_SCALE;
     private double translateX = 0.0;
     private double translateY = 0.0;
     private Point lastDragPoint;
     private static final double MIN_SCALE = 0.05d;
     private static final double MAX_SCALE = 50.0d;
     private static final double SCALE_EPSILON = 1e-6d;
+    private static final String MAP_SCALE_PROPERTY = "mapScale"; // 灞炴�ф枃浠朵腑鐨勯敭鍚�
     
     // 涓婚棰滆壊
     private final Color THEME_COLOR = new Color(46, 139, 87);
@@ -49,6 +52,7 @@
     private static final Color GRASS_FILL_COLOR = new Color(144, 238, 144, 120);
     private static final Color GRASS_BORDER_COLOR = new Color(60, 179, 113);
     private static final Color BOUNDARY_POINT_COLOR = new Color(128, 0, 128);
+    private static final Color OBSTACLE_POINT_COLOR = new Color(255, 140, 0); // 姗欒壊锛岀敤浜庡尯鍒嗛殰纰嶇墿鐐�
     private static final Color CIRCLE_SAMPLE_COLOR = new Color(220, 20, 60, 230);
     private static final double CIRCLE_SAMPLE_SIZE = 0.54d;
     private static final double BOUNDARY_POINT_MERGE_THRESHOLD = 0.05;
@@ -68,6 +72,7 @@
     private String currentObstacleLandNumber;
     private String boundaryName;
     private boolean boundaryPointsVisible;
+    private boolean obstaclePointsVisible;
     private double boundaryPointSizeScale = 1.0d;
     private boolean previewSizingEnabled;
     private String currentBoundaryLandNumber;
@@ -111,6 +116,64 @@
         this.mowerUpdateTimer = createMowerTimer();
         this.mowerInfoManager = new GecaojiMeg(visualizationPanel, mower);
         setupMouseListeners();
+        // 浠庨厤缃枃浠惰鍙栦笂娆′繚瀛樼殑缂╂斁姣斾緥鍜岃鍥句腑蹇冨潗鏍�
+        loadViewSettingsFromProperties();
+    }
+    
+    /**
+     * 浠庨厤缃枃浠惰鍙栫缉鏀炬瘮渚嬪拰瑙嗗浘涓績鍧愭爣
+     */
+    private void loadViewSettingsFromProperties() {
+        // 鍔犺浇缂╂斁姣斾緥
+        String scaleValue = Setsys.getPropertyValue(MAP_SCALE_PROPERTY);
+        if (scaleValue != null && !scaleValue.trim().isEmpty()) {
+            try {
+                double savedScale = Double.parseDouble(scaleValue.trim());
+                // 楠岃瘉缂╂斁姣斾緥鏄惁鍦ㄦ湁鏁堣寖鍥村唴
+                if (savedScale >= MIN_SCALE && savedScale <= MAX_SCALE) {
+                    scale = savedScale;
+                } else {
+                    scale = DEFAULT_SCALE;
+                }
+            } catch (NumberFormatException e) {
+                // 濡傛灉瑙f瀽澶辫触锛屼娇鐢ㄩ粯璁ゅ��
+                scale = DEFAULT_SCALE;
+            }
+        } else {
+            // 濡傛灉娌℃湁淇濆瓨鐨勫�硷紝浣跨敤榛樿鍊�
+            scale = DEFAULT_SCALE;
+        }
+        
+        // 鍔犺浇瑙嗗浘涓績鍧愭爣
+        String viewCenterXValue = Setsys.getPropertyValue("viewCenterX");
+        String viewCenterYValue = Setsys.getPropertyValue("viewCenterY");
+        if (viewCenterXValue != null && !viewCenterXValue.trim().isEmpty()) {
+            try {
+                translateX = Double.parseDouble(viewCenterXValue.trim());
+            } catch (NumberFormatException e) {
+                translateX = 0.0;
+            }
+        } else {
+            translateX = 0.0;
+        }
+        if (viewCenterYValue != null && !viewCenterYValue.trim().isEmpty()) {
+            try {
+                translateY = Double.parseDouble(viewCenterYValue.trim());
+            } catch (NumberFormatException e) {
+                translateY = 0.0;
+            }
+        } else {
+            translateY = 0.0;
+        }
+    }
+    
+    /**
+     * 淇濆瓨缂╂斁姣斾緥鍒伴厤缃枃浠�
+     */
+    private void saveScaleToProperties() {
+        Setsys setsys = new Setsys();
+        // 淇濈暀2浣嶅皬鏁�
+        setsys.updateProperty(MAP_SCALE_PROPERTY, String.format("%.2f", scale));
     }
     
     /**
@@ -218,6 +281,8 @@
         translateX += (newWorldX - worldX);
         translateY += (newWorldY - worldY);
 
+        // 淇濆瓨缂╂斁姣斾緥鍒伴厤缃枃浠�
+        saveScaleToProperties();
         visualizationPanel.repaint();
     }
 
@@ -253,9 +318,11 @@
      * 閲嶇疆瑙嗗浘
      */
     public void resetView() {
-        scale = 1.0;
+        scale = DEFAULT_SCALE;
         translateX = 0.0;
         translateY = 0.0;
+        // 淇濆瓨缂╂斁姣斾緥鍒伴厤缃枃浠�
+        saveScaleToProperties();
         visualizationPanel.repaint();
     }
     
@@ -281,14 +348,11 @@
         boolean hasPlannedPath = currentPlannedPath != null && currentPlannedPath.size() >= 2;
         boolean hasObstacles = currentObstacles != null && !currentObstacles.isEmpty();
 
+        // 缁樺埗鍦板潡杈圭晫锛堝簳灞傦級
         if (hasBoundary) {
             drawCurrentBoundary(g2d);
         }
 
-        if (hasObstacles) {
-            Obstacledraw.drawObstacles(g2d, currentObstacles, scale, selectedObstacleName);
-        }
-
         yulanzhangaiwu.renderPreview(g2d, scale);
 
         if (!circleSampleMarkers.isEmpty()) {
@@ -301,10 +365,16 @@
 
     adddikuaiyulan.drawPreview(g2d, handheldBoundaryPreview, scale, handheldBoundaryPreviewActive, boundaryPreviewMarkerScale);
 
+        // 缁樺埗瀵艰埅璺緞锛堜腑灞傦級
         if (hasPlannedPath) {
             drawCurrentPlannedPath(g2d);
         }
 
+        // 缁樺埗闅滅鐗╋紙椤跺眰锛屾樉绀哄湪鍦板潡鍜屽鑸矾寰勪笂鏂癸級
+        if (hasObstacles) {
+            Obstacledraw.drawObstacles(g2d, currentObstacles, scale, selectedObstacleName);
+        }
+
         if (boundaryPointsVisible && hasBoundary) {
             double markerScale = boundaryPointSizeScale * (previewSizingEnabled ? PREVIEW_BOUNDARY_MARKER_SCALE : 1.0d);
             pointandnumber.drawBoundaryPoints(
@@ -317,6 +387,22 @@
             );
         }
 
+        // 缁樺埗闅滅鐗╁潗鏍囩偣
+        if (obstaclePointsVisible && hasObstacles) {
+            List<Point2D.Double> obstaclePoints = Obstacledraw.getObstaclePoints(currentObstacles);
+            if (obstaclePoints != null && !obstaclePoints.isEmpty()) {
+                double markerScale = boundaryPointSizeScale * (previewSizingEnabled ? PREVIEW_BOUNDARY_MARKER_SCALE : 1.0d);
+                pointandnumber.drawBoundaryPoints(
+                    g2d,
+                    obstaclePoints,
+                    scale,
+                    BOUNDARY_POINT_MERGE_THRESHOLD,
+                    OBSTACLE_POINT_COLOR,
+                    markerScale
+                );
+            }
+        }
+
         if (shouldRenderIdleTrail()) {
             tuowei.draw(g2d, idleMowerTrail, scale);
         }
@@ -482,7 +568,8 @@
         if (device == null) {
             return;
         }
-        if (!isHighPrecisionFix(device.getPositioningStatus())) {
+        // 浣跨敤鏇村鏉剧殑瀹氫綅鐘舵�佸垽鏂紝鍏佽鐘舵��1鍜�4鏄剧ず鎷栧熬
+        if (!isValidFixForTrail(device.getPositioningStatus())) {
             return;
         }
 
@@ -504,6 +591,56 @@
         idleMowerTrail.addLast(new tuowei.TrailSample(now, new Point2D.Double(position.x, position.y)));
         pruneIdleMowerTrail(now);
     }
+    
+    /**
+     * 寮哄埗鏇存柊鎷栧熬锛堢敤浜庢敹鍒�$GNGGA鏁版嵁鏃剁珛鍗虫洿鏂帮級
+     * 杩欎釜鏂规硶浼氬埛鏂癿ower浣嶇疆骞剁珛鍗虫坊鍔犲埌鎷栧熬
+     */
+    public void forceUpdateIdleMowerTrail() {
+        long now = System.currentTimeMillis();
+        pruneIdleMowerTrail(now);
+
+        if (idleTrailSuppressed || realtimeTrackRecording) {
+            if (!idleMowerTrail.isEmpty()) {
+                clearIdleMowerTrail();
+            }
+            return;
+        }
+
+        Device device = Device.getGecaoji();
+        if (device == null) {
+            return;
+        }
+        // 浣跨敤鏇村鏉剧殑瀹氫綅鐘舵�佸垽鏂紝鍏佽鐘舵��1鍜�4鏄剧ず鎷栧熬
+        if (!isValidFixForTrail(device.getPositioningStatus())) {
+            return;
+        }
+
+        // 鍒锋柊mower浣嶇疆锛屼娇鐢ㄦ渶鏂扮殑Device鏁版嵁
+        mower.refreshFromDevice();
+        Point2D.Double position = mower.getPosition();
+        if (position == null || !Double.isFinite(position.x) || !Double.isFinite(position.y)) {
+            return;
+        }
+
+        tuowei.TrailSample lastSample = idleMowerTrail.peekLast();
+        if (lastSample != null) {
+            Point2D.Double lastPoint = lastSample.getPoint();
+            double dx = position.x - lastPoint.x;
+            double dy = position.y - lastPoint.y;
+            if (Math.hypot(dx, dy) < IDLE_TRAIL_SAMPLE_DISTANCE_METERS) {
+                return;
+            }
+        }
+
+        idleMowerTrail.addLast(new tuowei.TrailSample(now, new Point2D.Double(position.x, position.y)));
+        pruneIdleMowerTrail(now);
+        
+        // 绔嬪嵆閲嶇粯锛岀‘淇濇嫋灏惧強鏃舵樉绀�
+        if (visualizationPanel != null) {
+            visualizationPanel.repaint();
+        }
+    }
 
     private void pruneIdleMowerTrail(long now) {
         if (idleMowerTrail.isEmpty()) {
@@ -1233,6 +1370,32 @@
             return false;
         }
     }
+    
+    /**
+     * 鍒ゆ柇瀹氫綅鐘舵�佹槸鍚︽湁鏁堬紝鍙敤浜庢樉绀烘嫋灏�
+     * 鎺ュ彈鐘舵��1锛堝崟鐐瑰畾浣嶏級銆�2锛堢爜宸垎锛夈��3锛堟棤鏁圥PS锛夈��4锛堝浐瀹氳В锛夈��5锛堟诞鐐硅В锛�
+     */
+    private boolean isValidFixForTrail(String fixQuality) {
+        if (fixQuality == null) {
+            return false;
+        }
+        String trimmed = fixQuality.trim();
+        if (trimmed.isEmpty()) {
+            return false;
+        }
+        // 鎺ュ彈鐘舵��1,2,3,4,5锛堝彧瑕佷笉鏄�0鎴栨棤鏁堢姸鎬侊級
+        if ("1".equals(trimmed) || "2".equals(trimmed) || "3".equals(trimmed) || 
+            "4".equals(trimmed) || "5".equals(trimmed)) {
+            return true;
+        }
+        try {
+            double value = Double.parseDouble(trimmed);
+            // 鎺ュ彈1.0, 2.0, 3.0, 4.0, 5.0锛堝彧瑕佷笉鏄�0锛�
+            return value >= 1.0 && value <= 5.0;
+        } catch (NumberFormatException ex) {
+            return false;
+        }
+    }
 
     private boolean isPointInsideActiveBoundary(Point2D.Double point) {
         if (point == null || !Double.isFinite(point.x) || !Double.isFinite(point.y)) {
@@ -1339,7 +1502,15 @@
      * 璁剧疆瑙嗗浘鍙樻崲鍙傛暟锛堢敤浜庣▼搴忓寲鎺у埗锛�
      */
     public void setViewTransform(double scale, double translateX, double translateY) {
-        this.scale = scale;
+        // 闄愬埗缂╂斁鑼冨洿
+        scale = Math.max(MIN_SCALE, Math.min(scale, MAX_SCALE));
+        // 濡傛灉缂╂斁姣斾緥鏀瑰彉浜嗭紝淇濆瓨鍒伴厤缃枃浠�
+        if (Math.abs(this.scale - scale) > SCALE_EPSILON) {
+            this.scale = scale;
+            saveScaleToProperties();
+        } else {
+            this.scale = scale;
+        }
         this.translateX = translateX;
         this.translateY = translateY;
         visualizationPanel.repaint();
@@ -1593,6 +1764,7 @@
         obstacleBounds = null;
         selectedObstacleName = null;
         currentObstacleLandNumber = null;
+        obstaclePointsVisible = false;
     }
 
     private List<Obstacledge.Obstacle> parseObstacles(String obstaclesData, String landNumber) {
@@ -1880,6 +2052,11 @@
         visualizationPanel.repaint();
     }
 
+    public void setObstaclePointsVisible(boolean visible) {
+        this.obstaclePointsVisible = visible;
+        visualizationPanel.repaint();
+    }
+
     public void setBoundaryPointSizeScale(double sizeScale) {
         double normalized = (Double.isFinite(sizeScale) && sizeScale > 0.0d) ? sizeScale : 1.0d;
         if (Math.abs(boundaryPointSizeScale - normalized) < 1e-6) {
@@ -2081,4 +2258,20 @@
         mowerInfoManager.dispose();
     }
 
+    /**
+     * 鑾峰彇褰撳墠杈圭晫鐐瑰垪琛�
+     * @return 褰撳墠杈圭晫鐐瑰垪琛紝濡傛灉娌℃湁杈圭晫鍒欒繑鍥瀗ull
+     */
+    public List<Point2D.Double> getCurrentBoundary() {
+        return currentBoundary;
+    }
+
+    /**
+     * 鑾峰彇鍓茶崏鏈哄疄渚�
+     * @return 鍓茶崏鏈哄疄渚�
+     */
+    public Gecaoji getMower() {
+        return mower;
+    }
+
 }
\ No newline at end of file

--
Gitblit v1.10.0