From 8ce07ce9a4034fdc959d280dd38ecb3e05cbe6e1 Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期三, 17 十二月 2025 16:40:11 +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