From 9d171a3c3a57ea54454d7e9d64dec213aa885a2c Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期三, 17 十二月 2025 15:26:31 +0800
Subject: [PATCH] 障碍物预览增加了序号

---
 src/zhuye/MapRenderer.java |  350 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 332 insertions(+), 18 deletions(-)

diff --git a/src/zhuye/MapRenderer.java b/src/zhuye/MapRenderer.java
index 3c79352..b83f3c8 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 java.io.File;
 import set.Setsys;
 import gecaoji.Device;
 import gecaoji.Gecaoji;
@@ -215,6 +216,11 @@
                 if (handleMowerClick(e.getPoint())) {
                     return;
                 }
+                // 浼樺厛澶勭悊闅滅鐗╄竟鐣岀偣鐐瑰嚮锛堝鏋滃彲瑙侊級
+                if (obstaclePointsVisible && handleObstaclePointClick(e.getPoint())) {
+                    return;
+                }
+                // 鐒跺悗澶勭悊鍦板潡杈圭晫鐐圭偣鍑�
                 if (boundaryPointsVisible) {
                     handleBoundaryPointClick(e.getPoint());
                 }
@@ -376,33 +382,27 @@
         }
 
         if (boundaryPointsVisible && hasBoundary) {
-            double markerScale = boundaryPointSizeScale * (previewSizingEnabled ? PREVIEW_BOUNDARY_MARKER_SCALE : 1.0d);
-            pointandnumber.drawBoundaryPoints(
-                g2d,
-                currentBoundary,
-                scale,
-                BOUNDARY_POINT_MERGE_THRESHOLD,
-                BOUNDARY_POINT_COLOR,
-                markerScale
-            );
-        }
-
-        // 缁樺埗闅滅鐗╁潗鏍囩偣
-        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);
+            // 棰勮妯″紡涓嬫樉绀哄簭鍙�
+            if (previewSizingEnabled) {
+                drawBoundaryPointsWithNumbers(g2d, currentBoundary, scale);
+            } else {
+                double markerScale = boundaryPointSizeScale;
                 pointandnumber.drawBoundaryPoints(
                     g2d,
-                    obstaclePoints,
+                    currentBoundary,
                     scale,
                     BOUNDARY_POINT_MERGE_THRESHOLD,
-                    OBSTACLE_POINT_COLOR,
+                    BOUNDARY_POINT_COLOR,
                     markerScale
                 );
             }
         }
 
+        // 缁樺埗闅滅鐗╁潗鏍囩偣锛堝甫搴忓彿锛�
+        if (obstaclePointsVisible && hasObstacles) {
+            drawObstaclePointsWithNumbers(g2d, currentObstacles, scale);
+        }
+
         if (shouldRenderIdleTrail()) {
             tuowei.draw(g2d, idleMowerTrail, scale);
         }
@@ -1184,6 +1184,139 @@
         }
     }
 
+    /**
+     * 澶勭悊闅滅鐗╄竟鐣岀偣鐐瑰嚮
+     * @param screenPoint 灞忓箷鍧愭爣鐐�
+     * @return 濡傛灉澶勭悊浜嗙偣鍑昏繑鍥瀟rue锛屽惁鍒欒繑鍥瀎alse
+     */
+    private boolean handleObstaclePointClick(Point screenPoint) {
+        if (currentObstacles == null || currentObstacles.isEmpty() || currentObstacleLandNumber == null) {
+            return false;
+        }
+
+        double threshold = computeSelectionThresholdPixels();
+        
+        // 閬嶅巻鎵�鏈夐殰纰嶇墿锛屾壘鍒拌鐐瑰嚮鐨勭偣
+        for (Obstacledge.Obstacle obstacle : currentObstacles) {
+            if (obstacle == null) {
+                continue;
+            }
+            
+            List<Obstacledge.XYCoordinate> xyCoords = obstacle.getXyCoordinates();
+            if (xyCoords == null || xyCoords.isEmpty()) {
+                continue;
+            }
+            
+            // 妫�鏌ユ瘡涓偣
+            for (int i = 0; i < xyCoords.size(); i++) {
+                Obstacledge.XYCoordinate coord = xyCoords.get(i);
+                Point2D.Double worldPoint = new Point2D.Double(coord.getX(), coord.getY());
+                Point2D.Double screenPosition = worldToScreen(worldPoint);
+                
+                double dx = screenPosition.x - screenPoint.x;
+                double dy = screenPosition.y - screenPoint.y;
+                if (Math.hypot(dx, dy) <= threshold) {
+                    // 鎵惧埌琚偣鍑荤殑鐐�
+                    String obstacleName = obstacle.getObstacleName();
+                    String pointLabel = (i + 1) + "";
+                    String message = "纭畾瑕佸垹闄ら殰纰嶇墿 \"" + obstacleName + "\" 鐨勭" + pointLabel + "鍙疯竟鐣岀偣鍚楋紵";
+                    
+                    int choice = JOptionPane.showConfirmDialog(
+                        visualizationPanel,
+                        message,
+                        "鍒犻櫎闅滅鐗╄竟鐣岀偣",
+                        JOptionPane.OK_CANCEL_OPTION,
+                        JOptionPane.WARNING_MESSAGE
+                    );
+                    
+                    if (choice == JOptionPane.OK_OPTION) {
+                        removeObstaclePoint(obstacle, i);
+                    }
+                    return true;
+                }
+            }
+        }
+        
+        return false;
+    }
+
+    /**
+     * 鍒犻櫎闅滅鐗╃殑鎸囧畾杈圭晫鐐�
+     */
+    private void removeObstaclePoint(Obstacledge.Obstacle obstacle, int pointIndex) {
+        if (obstacle == null || currentObstacleLandNumber == null) {
+            return;
+        }
+        
+        List<Obstacledge.XYCoordinate> xyCoords = obstacle.getXyCoordinates();
+        if (xyCoords == null || pointIndex < 0 || pointIndex >= xyCoords.size()) {
+            return;
+        }
+        
+        // 妫�鏌ュ垹闄ゅ悗鏄惁杩樻湁瓒冲鐨勭偣
+        Obstacledge.ObstacleShape shape = obstacle.getShape();
+        int minPoints = (shape == Obstacledge.ObstacleShape.CIRCLE) ? 2 : 3;
+        
+        if (xyCoords.size() <= minPoints) {
+            JOptionPane.showMessageDialog(
+                visualizationPanel,
+                "闅滅鐗╄嚦灏戦渶瑕�" + minPoints + "涓偣锛屾棤娉曞垹闄�",
+                "鎻愮ず",
+                JOptionPane.INFORMATION_MESSAGE
+            );
+            return;
+        }
+        
+        // 鍒涘缓鏂扮殑鍧愭爣鍒楄〃锛堢Щ闄ゆ寚瀹氱偣锛�
+        List<Obstacledge.XYCoordinate> updatedCoords = new ArrayList<>(xyCoords);
+        updatedCoords.remove(pointIndex);
+        
+        // 鏇存柊闅滅鐗╁潗鏍�
+        obstacle.setXyCoordinates(updatedCoords);
+        
+        // 淇濆瓨鍒伴厤缃枃浠�
+        try {
+            File configFile = new File("Obstacledge.properties");
+            Obstacledge.ConfigManager manager = new Obstacledge.ConfigManager();
+            if (configFile.exists()) {
+                manager.loadFromFile(configFile.getAbsolutePath());
+            }
+            
+            Obstacledge.Plot plot = manager.getPlotById(currentObstacleLandNumber.trim());
+            if (plot != null) {
+                // 绉婚櫎鏃ч殰纰嶇墿骞舵坊鍔犳洿鏂板悗鐨勯殰纰嶇墿
+                plot.removeObstacleByName(obstacle.getObstacleName());
+                plot.addObstacle(obstacle);
+                manager.saveToFile(configFile.getAbsolutePath());
+                
+                // 鏇存柊鍦板潡鏇存柊鏃堕棿
+                Dikuai.updateField(currentObstacleLandNumber.trim(), "updateTime", 
+                    new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()));
+                Dikuai.saveToProperties();
+                
+                // 閲嶆柊鍔犺浇闅滅鐗╂暟鎹互鍒锋柊鏄剧ず
+                List<Obstacledge.Obstacle> updatedObstacles = new ArrayList<>();
+                for (Obstacledge.Obstacle obs : currentObstacles) {
+                    if (obs.getObstacleName().equals(obstacle.getObstacleName())) {
+                        updatedObstacles.add(obstacle); // 浣跨敤鏇存柊鍚庣殑闅滅鐗�
+                    } else {
+                        updatedObstacles.add(obs); // 淇濇寔鍏朵粬闅滅鐗╀笉鍙�
+                    }
+                }
+                applyObstaclesToRenderer(updatedObstacles, currentObstacleLandNumber);
+                visualizationPanel.repaint();
+            }
+        } catch (Exception ex) {
+            ex.printStackTrace();
+            JOptionPane.showMessageDialog(
+                visualizationPanel,
+                "淇濆瓨澶辫触: " + ex.getMessage(),
+                "閿欒",
+                JOptionPane.ERROR_MESSAGE
+            );
+        }
+    }
+
     private void handleBoundaryPointClick(Point screenPoint) {
         if (currentBoundary == null || currentBoundaryLandNumber == null) {
             return;
@@ -1451,6 +1584,187 @@
     /**
      * 缁樺埗瑙嗗浘淇℃伅
      */
+    /**
+     * 缁樺埗闅滅鐗╁潗鏍囩偣锛堝甫搴忓彿锛�
+     * 搴忓彿鏄剧ず鍦ㄧ偣涓績锛屽瓧浣撳ぇ灏忎笌闅滅鐗╁悕绉颁竴鑷达紙11鍙凤級锛屼笉闅忕缉鏀惧彉鍖�
+     */
+    private void drawObstaclePointsWithNumbers(Graphics2D g2d, List<Obstacledge.Obstacle> obstacles, double scale) {
+        if (obstacles == null || obstacles.isEmpty()) {
+            return;
+        }
+        
+        // 淇濆瓨鍘熷鍙樻崲
+        AffineTransform originalTransform = g2d.getTransform();
+        
+        // 璁剧疆鐐圭殑澶у皬锛堥殢缂╂斁鍙樺寲锛�
+        double scaleFactor = Math.max(0.5, scale);
+        double clampedScale = boundaryPointSizeScale * (previewSizingEnabled ? PREVIEW_BOUNDARY_MARKER_SCALE : 1.0d);
+        if (!Double.isFinite(clampedScale) || clampedScale <= 0.0d) {
+            clampedScale = 1.0d;
+        }
+        double minimumDiameter = clampedScale < 1.0 ? 0.5 : 1.0;
+        double markerDiameter = Math.max(minimumDiameter, (10.0 / scaleFactor) * 0.2 * clampedScale);
+        double markerRadius = markerDiameter / 2.0;
+        
+        // 璁剧疆瀛椾綋锛堜笌闅滅鐗╁悕绉颁竴鑷达紝涓嶉殢缂╂斁鍙樺寲锛�
+        Font labelFont = new Font("寰蒋闆呴粦", Font.PLAIN, 11);
+        g2d.setFont(labelFont);
+        FontMetrics fontMetrics = g2d.getFontMetrics(labelFont);
+        
+        // 閬嶅巻鎵�鏈夐殰纰嶇墿
+        for (Obstacledge.Obstacle obstacle : obstacles) {
+            if (obstacle == null || !obstacle.isValid()) {
+                continue;
+            }
+            
+            List<Obstacledge.XYCoordinate> xyCoords = obstacle.getXyCoordinates();
+            if (xyCoords == null || xyCoords.isEmpty()) {
+                continue;
+            }
+            
+            // 缁樺埗姣忎釜鐐瑰強鍏跺簭鍙�
+            for (int i = 0; i < xyCoords.size(); i++) {
+                Obstacledge.XYCoordinate coord = xyCoords.get(i);
+                double x = coord.getX();
+                double y = coord.getY();
+                
+                // 缁樺埗鐐癸紙鍦ㄤ笘鐣屽潗鏍囩郴涓紝闅忕缉鏀惧彉鍖栵級
+                g2d.setColor(OBSTACLE_POINT_COLOR);
+                Ellipse2D.Double marker = new Ellipse2D.Double(
+                    x - markerRadius, 
+                    y - markerRadius, 
+                    markerDiameter, 
+                    markerDiameter
+                );
+                g2d.fill(marker);
+                
+                // 灏嗕笘鐣屽潗鏍囪浆鎹负灞忓箷鍧愭爣浠ョ粯鍒跺簭鍙凤紙涓嶉殢缂╂斁鍙樺寲锛�
+                Point2D.Double worldPoint = new Point2D.Double(x, y);
+                Point2D.Double screenPoint = new Point2D.Double();
+                originalTransform.transform(worldPoint, screenPoint);
+                
+                // 淇濆瓨褰撳墠鍙樻崲
+                AffineTransform savedTransform = g2d.getTransform();
+                
+                // 閲嶇疆鍙樻崲涓哄睆骞曞潗鏍囩郴缁�
+                g2d.setTransform(new AffineTransform());
+                
+                // 缁樺埗搴忓彿锛堝湪灞忓箷鍧愭爣绯讳腑锛屼笉闅忕缉鏀惧彉鍖栵級
+                String numberText = String.valueOf(i + 1);
+                int textWidth = fontMetrics.stringWidth(numberText);
+                int textHeight = fontMetrics.getHeight();
+                
+                // 鍦ㄧ偣涓績缁樺埗搴忓彿
+                int textX = (int)(screenPoint.x - textWidth / 2.0);
+                int textY = (int)(screenPoint.y + textHeight / 4.0);
+                
+                // 缁樺埗搴忓彿鏂囧瓧锛堟棤鑳屾櫙锛�
+                g2d.setColor(Color.BLACK);
+                g2d.drawString(numberText, textX, textY);
+                
+                // 鎭㈠鍙樻崲
+                g2d.setTransform(savedTransform);
+            }
+        }
+        
+        // 鎭㈠鍘熷鍙樻崲
+        g2d.setTransform(originalTransform);
+    }
+    
+    /**
+     * 缁樺埗杈圭晫鐐癸紙甯﹀簭鍙凤級
+     * 搴忓彿鏄剧ず鍦ㄧ偣涓績锛屽瓧浣撳ぇ灏忎笌闅滅鐗╁簭鍙蜂竴鑷达紙11鍙凤級锛屼笉闅忕缉鏀惧彉鍖�
+     */
+    private void drawBoundaryPointsWithNumbers(Graphics2D g2d, List<Point2D.Double> boundary, double scale) {
+        if (boundary == null || boundary.size() < 2) {
+            return;
+        }
+        
+        // 淇濆瓨鍘熷鍙樻崲
+        AffineTransform originalTransform = g2d.getTransform();
+        
+        int totalPoints = boundary.size();
+        boolean closed = totalPoints > 2 && areBoundaryPointsClose(boundary.get(0), boundary.get(totalPoints - 1));
+        int effectiveCount = closed ? totalPoints - 1 : totalPoints;
+        if (effectiveCount <= 0) {
+            return;
+        }
+        
+        // 璁剧疆鐐圭殑澶у皬锛堥殢缂╂斁鍙樺寲锛�
+        double scaleFactor = Math.max(0.5, scale);
+        double clampedScale = boundaryPointSizeScale * (previewSizingEnabled ? PREVIEW_BOUNDARY_MARKER_SCALE : 1.0d);
+        if (!Double.isFinite(clampedScale) || clampedScale <= 0.0d) {
+            clampedScale = 1.0d;
+        }
+        double minimumDiameter = clampedScale < 1.0 ? 0.5 : 1.0;
+        double markerDiameter = Math.max(minimumDiameter, (10.0 / scaleFactor) * 0.2 * clampedScale);
+        double markerRadius = markerDiameter / 2.0;
+        
+        // 璁剧疆瀛椾綋锛堜笌闅滅鐗╁簭鍙蜂竴鑷达紝涓嶉殢缂╂斁鍙樺寲锛�
+        Font labelFont = new Font("寰蒋闆呴粦", Font.PLAIN, 11);
+        g2d.setFont(labelFont);
+        FontMetrics fontMetrics = g2d.getFontMetrics(labelFont);
+        
+        // 缁樺埗姣忎釜鐐瑰強鍏跺簭鍙�
+        for (int i = 0; i < effectiveCount; i++) {
+            Point2D.Double point = boundary.get(i);
+            double x = point.x;
+            double y = point.y;
+            
+            // 缁樺埗鐐癸紙鍦ㄤ笘鐣屽潗鏍囩郴涓紝闅忕缉鏀惧彉鍖栵級
+            g2d.setColor(BOUNDARY_POINT_COLOR);
+            Ellipse2D.Double marker = new Ellipse2D.Double(
+                x - markerRadius, 
+                y - markerRadius, 
+                markerDiameter, 
+                markerDiameter
+            );
+            g2d.fill(marker);
+            
+            // 灏嗕笘鐣屽潗鏍囪浆鎹负灞忓箷鍧愭爣浠ョ粯鍒跺簭鍙凤紙涓嶉殢缂╂斁鍙樺寲锛�
+            Point2D.Double worldPoint = new Point2D.Double(x, y);
+            Point2D.Double screenPoint = new Point2D.Double();
+            originalTransform.transform(worldPoint, screenPoint);
+            
+            // 淇濆瓨褰撳墠鍙樻崲
+            AffineTransform savedTransform = g2d.getTransform();
+            
+            // 閲嶇疆鍙樻崲涓哄睆骞曞潗鏍囩郴缁�
+            g2d.setTransform(new AffineTransform());
+            
+            // 缁樺埗搴忓彿锛堝湪灞忓箷鍧愭爣绯讳腑锛屼笉闅忕缉鏀惧彉鍖栵級
+            String numberText = String.valueOf(i + 1);
+            int textWidth = fontMetrics.stringWidth(numberText);
+            int textHeight = fontMetrics.getHeight();
+            
+            // 鍦ㄧ偣涓績缁樺埗搴忓彿
+            int textX = (int)(screenPoint.x - textWidth / 2.0);
+            int textY = (int)(screenPoint.y + textHeight / 4.0);
+            
+            // 缁樺埗搴忓彿鏂囧瓧锛堟棤鑳屾櫙锛�
+            g2d.setColor(Color.BLACK);
+            g2d.drawString(numberText, textX, textY);
+            
+            // 鎭㈠鍙樻崲
+            g2d.setTransform(savedTransform);
+        }
+        
+        // 鎭㈠鍘熷鍙樻崲
+        g2d.setTransform(originalTransform);
+    }
+    
+    /**
+     * 妫�鏌ヤ袱涓竟鐣岀偣鏄惁鎺ヨ繎锛堢敤浜庡垽鏂竟鐣屾槸鍚﹂棴鍚堬級
+     */
+    private boolean areBoundaryPointsClose(Point2D.Double a, Point2D.Double b) {
+        if (a == null || b == null) {
+            return false;
+        }
+        double dx = a.x - b.x;
+        double dy = a.y - b.y;
+        return Math.hypot(dx, dy) <= BOUNDARY_POINT_MERGE_THRESHOLD;
+    }
+    
     private void drawViewInfo(Graphics2D g2d) {
         g2d.setColor(new Color(80, 80, 80));
         g2d.setFont(new Font("寰蒋闆呴粦", Font.PLAIN, 11));

--
Gitblit v1.10.0