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

---
 src/zhangaiwu/Obstacledraw.java |  124 +++++++++++++++++++++++++++++++----------
 1 files changed, 93 insertions(+), 31 deletions(-)

diff --git a/src/zhangaiwu/Obstacledraw.java b/src/zhangaiwu/Obstacledraw.java
index a01e0e0..3125dc7 100644
--- a/src/zhangaiwu/Obstacledraw.java
+++ b/src/zhangaiwu/Obstacledraw.java
@@ -10,21 +10,28 @@
 
 /**
  * 闅滅鐗╃粯鍒剁被 - 璐熻矗缁樺埗鍦板潡涓殑闅滅鐗�
+ * 
+ * 娉ㄦ剰锛氶殰纰嶇墿鍥惧眰闇�瑕佸浜庡湴鍧楀拰瀵艰埅璺緞涓婃柟銆�
+ * 鍦� MapRenderer.renderMap() 涓紝缁樺埗椤哄簭搴斾负锛�
+ * 1. 鍦板潡杈圭晫锛堝簳灞傦級
+ * 2. 瀵艰埅璺緞锛堜腑灞傦級
+ * 3. 闅滅鐗╋紙椤跺眰锛屾樉绀哄湪鍦板潡鍜屽鑸矾寰勪笂鏂癸級
  */
 public class Obstacledraw {
     
     // 棰滆壊瀹氫箟
-    private static final Color CIRCLE_FILL_COLOR = new Color(255, 182, 193, 120); // 鍦嗗舰濉厖鑹� - 娴呯矇绾�
+    private static final Color CIRCLE_FILL_COLOR = new Color(128, 128, 128, 128); // 鍦嗗舰濉厖鑹� - 鐏拌壊锛岄�忔槑搴�50%
     private static final Color CIRCLE_BORDER_COLOR = new Color(199, 21, 133);    // 鍦嗗舰杈规鑹� - 娣辩矇绾�
-    private static final Color POLYGON_FILL_COLOR = new Color(173, 216, 230, 120); // 澶氳竟褰㈠~鍏呰壊 - 娴呰摑
+    private static final Color POLYGON_FILL_COLOR = new Color(128, 128, 128, 128); // 澶氳竟褰㈠~鍏呰壊 - 鐏拌壊锛岄�忔槑搴�50%
     private static final Color POLYGON_BORDER_COLOR = new Color(25, 25, 112);    // 澶氳竟褰㈣竟妗嗚壊 - 娣辫摑
     private static final Color OBSTACLE_LABEL_COLOR = Color.BLACK;
     private static final Color OBSTACLE_POINT_COLOR = Color.RED;
     
     // 灏哄瀹氫箟
     private static final double OBSTACLE_POINT_SIZE = 0.1; // 闅滅鐗╂帶鍒剁偣澶у皬锛堢背锛�
-    private static final float DEFAULT_BORDER_WIDTH = 1.0f;
-    private static final float SELECTED_BORDER_WIDTH = 2.5f;
+    // 杈圭晫绾垮搴︿笌鍦板潡杈圭晫绾垮搴︿竴鑷达細3 / Math.max(0.5, scale)
+    private static final float BOUNDARY_STROKE_BASE = 3.0f; // 涓庡湴鍧楄竟鐣岀嚎瀹藉害涓�鑷�
+    private static final float SELECTED_WIDTH_MULTIPLIER = 1.5f; // 閫変腑鏃剁殑瀹藉害鍊嶆暟
     
     /**
      * 缁樺埗鍦板潡鐨勬墍鏈夐殰纰嶇墿
@@ -109,13 +116,15 @@
         g2d.setColor(CIRCLE_FILL_COLOR);
         g2d.fill(circle);
         
-        // 璁剧疆杈规棰滆壊鍜屽搴�
+        // 璁剧疆杈规棰滆壊鍜屽搴︼紙涓庡湴鍧楄竟鐣岀嚎瀹藉害涓�鑷达級
+        float strokeWidth = (float)(BOUNDARY_STROKE_BASE / Math.max(0.5, scale));
         if (isSelected) {
             g2d.setColor(CIRCLE_BORDER_COLOR.darker());
-            g2d.setStroke(new BasicStroke(SELECTED_BORDER_WIDTH / (float)scale));
+            // 閫変腑鏃剁◢寰姞绮�
+            g2d.setStroke(new BasicStroke(strokeWidth * SELECTED_WIDTH_MULTIPLIER));
         } else {
             g2d.setColor(CIRCLE_BORDER_COLOR);
-            g2d.setStroke(new BasicStroke(DEFAULT_BORDER_WIDTH / (float)scale));
+            g2d.setStroke(new BasicStroke(strokeWidth));
         }
         g2d.draw(circle);
         
@@ -153,13 +162,15 @@
         g2d.setColor(POLYGON_FILL_COLOR);
         g2d.fill(polygon);
         
-        // 璁剧疆杈规棰滆壊鍜屽搴�
+        // 璁剧疆杈规棰滆壊鍜屽搴︼紙涓庡湴鍧楄竟鐣岀嚎瀹藉害涓�鑷达級
+        float strokeWidth = (float)(BOUNDARY_STROKE_BASE / Math.max(0.5, scale));
         if (isSelected) {
             g2d.setColor(POLYGON_BORDER_COLOR.darker());
-            g2d.setStroke(new BasicStroke(SELECTED_BORDER_WIDTH / (float)scale));
+            // 閫変腑鏃剁◢寰姞绮�
+            g2d.setStroke(new BasicStroke(strokeWidth * SELECTED_WIDTH_MULTIPLIER));
         } else {
             g2d.setColor(POLYGON_BORDER_COLOR);
-            g2d.setStroke(new BasicStroke(DEFAULT_BORDER_WIDTH / (float)scale));
+            g2d.setStroke(new BasicStroke(strokeWidth));
         }
         g2d.draw(polygon);
         
@@ -202,6 +213,7 @@
     
     /**
      * 缁樺埗闅滅鐗╂爣绛�
+     * 鏂囧瓧澶у皬涓�"缂╂斁"鏂囧瓧涓�鑷达紙11鍙峰瓧浣擄級锛屼笖涓嶉殢鍦板浘缂╂斁鍙樺寲
      */
     private static void drawObstacleLabel(Graphics2D g2d, Obstacledge.Obstacle obstacle, 
                                           double scale) {
@@ -210,42 +222,92 @@
             return;
         }
         
-        // 璁$畻鏍囩浣嶇疆锛堜腑蹇冪偣锛�
-        double centerX = 0;
-        double centerY = 0;
+        // 淇濆瓨褰撳墠鍙樻崲
+        AffineTransform originalTransform = g2d.getTransform();
         
-        for (Obstacledge.XYCoordinate coord : xyCoords) {
-            centerX += coord.getX();
-            centerY += coord.getY();
+        double centerX;
+        double centerY;
+
+        Obstacledge.ObstacleShape shape = obstacle.getShape();
+        if (shape == Obstacledge.ObstacleShape.CIRCLE) {
+            Obstacledge.XYCoordinate centerCoord = xyCoords.get(0);
+            centerX = centerCoord.getX();
+            centerY = centerCoord.getY();
+        } else {
+            Point2D.Double centroid = computePolygonCentroid(xyCoords);
+            centerX = centroid.x;
+            centerY = centroid.y;
         }
         
-        centerX /= xyCoords.size();
-        centerY /= xyCoords.size();
+        // 灏嗕笘鐣屽潗鏍囪浆鎹负灞忓箷鍧愭爣
+        Point2D.Double worldPoint = new Point2D.Double(centerX, centerY);
+        Point2D.Double screenPoint = new Point2D.Double();
+        originalTransform.transform(worldPoint, screenPoint);
         
-        // 鑾峰彇闅滅鐗╁悕绉板拰褰㈢姸
+        // 鎭㈠鍘熷鍙樻崲浠ヤ娇鐢ㄥ睆骞曞潗鏍囩粯鍒�
+        g2d.setTransform(new AffineTransform());
+        
+        // 鑾峰彇闅滅鐗╁悕绉�
         String obstacleName = obstacle.getObstacleName();
-        String shapeDesc = obstacle.getShape().getDescription();
+        if (obstacleName == null || obstacleName.trim().isEmpty()) {
+            obstacleName = "闅滅鐗�";
+        } else {
+            obstacleName = obstacleName.trim();
+        }
         
-        // 璁剧疆瀛椾綋鍜岄鑹�
+        // 璁剧疆瀛椾綋鍜岄鑹诧紙涓�"缂╂斁"鏂囧瓧涓�鑷达級
         g2d.setColor(OBSTACLE_LABEL_COLOR);
-        
-        // 鏍规嵁缂╂斁姣斾緥璋冩暣瀛椾綋澶у皬
-        int fontSize = (int)(10 / scale);
-        fontSize = Math.max(8, Math.min(fontSize, 14)); // 闄愬埗瀛椾綋澶у皬鑼冨洿
-        
-        g2d.setFont(new Font("寰蒋闆呴粦", Font.PLAIN, fontSize));
+        g2d.setFont(new Font("寰蒋闆呴粦", Font.PLAIN, 11)); // 涓�"缂╂斁"鏂囧瓧澶у皬涓�鑷�
         
         // 缁樺埗鏍囩
-        String label = String.format("%s(%s)", obstacleName, shapeDesc);
+        String label = obstacleName;
         FontMetrics metrics = g2d.getFontMetrics();
         int textWidth = metrics.stringWidth(label);
         int textHeight = metrics.getHeight();
         
-        // 鍦ㄤ腑蹇冪偣缁樺埗鏍囩
-        int textX = (int)(centerX - textWidth / 2.0);
-        int textY = (int)(centerY + textHeight / 4.0); // 绋嶅井鍚戜笅鍋忕Щ
+        // 鍦ㄥ睆骞曞潗鏍囦腑蹇冪偣缁樺埗鏍囩
+        int textX = (int)(screenPoint.x - textWidth / 2.0);
+        int textY = (int)(screenPoint.y + textHeight / 4.0); // 绋嶅井鍚戜笅鍋忕Щ
         
         g2d.drawString(label, textX, textY);
+        
+        // 鎭㈠鍘熷鍙樻崲
+        g2d.setTransform(originalTransform);
+    }
+
+    private static Point2D.Double computePolygonCentroid(List<Obstacledge.XYCoordinate> xyCoords) {
+        double area = 0.0;
+        double cx = 0.0;
+        double cy = 0.0;
+        int n = xyCoords.size();
+        for (int i = 0; i < n; i++) {
+            Obstacledge.XYCoordinate current = xyCoords.get(i);
+            Obstacledge.XYCoordinate next = xyCoords.get((i + 1) % n);
+            double x0 = current.getX();
+            double y0 = current.getY();
+            double x1 = next.getX();
+            double y1 = next.getY();
+            double cross = x0 * y1 - x1 * y0;
+            area += cross;
+            cx += (x0 + x1) * cross;
+            cy += (y0 + y1) * cross;
+        }
+
+        double areaFactor = area * 0.5;
+        if (Math.abs(areaFactor) < 1e-9) {
+            double avgX = 0.0;
+            double avgY = 0.0;
+            for (Obstacledge.XYCoordinate coord : xyCoords) {
+                avgX += coord.getX();
+                avgY += coord.getY();
+            }
+            int size = Math.max(1, xyCoords.size());
+            return new Point2D.Double(avgX / size, avgY / size);
+        }
+
+        double centroidX = cx / (6.0 * areaFactor);
+        double centroidY = cy / (6.0 * areaFactor);
+        return new Point2D.Double(centroidX, centroidY);
     }
     
     /**

--
Gitblit v1.10.0