From e4b4347318f1f37c64b8055f1f1898da59a167ba Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期五, 26 十二月 2025 19:38:43 +0800
Subject: [PATCH] 优化了细节
---
src/gecaoji/Gecaoji.java | 114 ++++++++++++++++++++++++++++++++++++++++++++++++--------
1 files changed, 97 insertions(+), 17 deletions(-)
diff --git a/src/gecaoji/Gecaoji.java b/src/gecaoji/Gecaoji.java
index 177832f..18493ec 100644
--- a/src/gecaoji/Gecaoji.java
+++ b/src/gecaoji/Gecaoji.java
@@ -15,32 +15,45 @@
private static final double ICON_SCALE_FACTOR = 0.8;
private static final double MIN_SCALE = 1e-6;
private static final Color FALLBACK_FILL = new Color(0, 150, 0);
+ private static final String DEFAULT_ICON_PATH = "image/gecaoji.png";
+ private static final String HANDHELD_ICON_PATH = "image/URT.png";
- private final Image mowerIcon;
+ private final Image defaultIcon;
+ private final Image handheldIcon;
+ private Image activeIcon;
+ private boolean handheldIconActive;
private final Ellipse2D.Double fallbackShape = new Ellipse2D.Double();
private Point2D.Double position = new Point2D.Double();
private boolean positionValid;
private double headingDegrees;
public Gecaoji() {
- mowerIcon = loadIcon();
+ defaultIcon = loadIcon(DEFAULT_ICON_PATH);
+ handheldIcon = loadIcon(HANDHELD_ICON_PATH);
+ activeIcon = defaultIcon != null ? defaultIcon : handheldIcon;
+ handheldIconActive = false;
refreshFromDevice();
}
- private Image loadIcon() {
+ private Image loadIcon(String path) {
try {
- ImageIcon icon = new ImageIcon("image/gecaoji.png");
+ ImageIcon icon = new ImageIcon(path);
if (icon.getIconWidth() <= 0 || icon.getIconHeight() <= 0) {
return null;
}
return icon.getImage();
} catch (Exception ex) {
- System.err.println("Unable to load mower icon: " + ex.getMessage());
+ System.err.println("Unable to load mower icon from " + path + ": " + ex.getMessage());
return null;
}
}
public void refreshFromDevice() {
+ // 妫�鏌ユ槸鍚︽鍦ㄥ鑸瑙堟ā寮忥紝濡傛灉鏄垯涓嶆洿鏂颁綅缃�
+ if (isNavigating()) {
+ return;
+ }
+
Device device = Device.getGecaoji();
if (device == null) {
positionValid = false;
@@ -49,9 +62,9 @@
double x = parseCoordinate(device.getRealtimeX());
double y = parseCoordinate(device.getRealtimeY());
- double heading = parseHeading(device.getHeading());
+ double heading = parseHeading(device.getYaw());
if (Double.isNaN(x) || Double.isNaN(y)) {
- positionValid = false;
+ // Keep showing the last known mower position when temporary sensor glitches occur.
return;
}
@@ -61,6 +74,22 @@
positionValid = true;
headingDegrees = heading;
}
+
+ /**
+ * 妫�鏌ユ槸鍚︽鍦ㄥ鑸瑙堟ā寮�
+ * @return 濡傛灉姝e湪瀵艰埅棰勮杩斿洖true锛屽惁鍒欒繑鍥瀎alse
+ */
+ private boolean isNavigating() {
+ try {
+ dikuai.daohangyulan nav = dikuai.daohangyulan.getInstance();
+ if (nav != null) {
+ return nav.isNavigating();
+ }
+ } catch (Exception e) {
+ // 濡傛灉鑾峰彇瀵艰埅瀹炰緥澶辫触锛岃繑鍥瀎alse锛堜笉褰卞搷涓昏鍔熻兘锛�
+ }
+ return false;
+ }
private void ensurePosition() {
if (position == null) {
@@ -109,19 +138,21 @@
}
double worldSize = (ICON_PIXEL_SIZE * ICON_SCALE_FACTOR) / Math.max(scale, MIN_SCALE);
- if (mowerIcon != null && mowerIcon.getWidth(null) > 0 && mowerIcon.getHeight(null) > 0) {
- drawIcon(g2d, worldSize);
+ Image icon = activeIcon;
+ if (icon != null && icon.getWidth(null) > 0 && icon.getHeight(null) > 0) {
+ drawIcon(g2d, worldSize, icon);
} else {
drawFallback(g2d, worldSize);
}
}
- private void drawIcon(Graphics2D g2d, double worldSize) {
- double iconWidth = mowerIcon.getWidth(null);
- double iconHeight = mowerIcon.getHeight(null);
+ private void drawIcon(Graphics2D g2d, double worldSize, Image icon) {
+ double iconWidth = icon.getWidth(null);
+ double iconHeight = icon.getHeight(null);
double maxSide = Math.max(iconWidth, iconHeight);
double scaleFactor = worldSize / Math.max(maxSide, MIN_SCALE);
- double rotationRadians = Math.toRadians(-headingDegrees);
+ // 鍓茶崏鏈哄浘鏍囬粯璁ゆ湞鍗楋紝Yaw=0琛ㄧず姝e寳锛岄渶瑕佹棆杞�180搴�
+ double rotationRadians = Math.toRadians(headingDegrees + 180);
AffineTransform original = g2d.getTransform();
AffineTransform transformed = new AffineTransform(original);
@@ -132,7 +163,7 @@
transformed.scale(scaleFactor, scaleFactor);
transformed.translate(-iconWidth / 2.0, -iconHeight / 2.0);
g2d.setTransform(transformed);
- g2d.drawImage(mowerIcon, 0, 0, null);
+ g2d.drawImage(icon, 0, 0, null);
g2d.setTransform(original);
}
@@ -146,9 +177,11 @@
g2d.fill(fallbackShape);
g2d.setColor(Color.WHITE);
g2d.draw(fallbackShape);
- double rotationRadians = Math.toRadians(-headingDegrees);
- double lineLength = radius;
- double dx = lineLength * Math.sin(rotationRadians);
+ // Yaw=0琛ㄧず姝e寳(0, -1)锛屼娇鐢╯in/cos璁$畻鍧愭爣
+ // sin(180)=0, cos(180)=-1 -> 姝e寳
+ double rotationRadians = Math.toRadians(180 - headingDegrees);
+ double lineLength = radius;
+ double dx = lineLength * Math.sin(rotationRadians);
double dy = lineLength * Math.cos(rotationRadians);
g2d.drawLine(
(int) Math.round(position.x),
@@ -170,6 +203,38 @@
return new Point2D.Double(position.x, position.y);
}
+ /**
+ * 璁剧疆鍓茶崏鏈轰綅缃紙鐢ㄤ簬瀵艰埅棰勮绛夊満鏅級
+ * @param x X鍧愭爣
+ * @param y Y鍧愭爣
+ */
+ public void setPosition(double x, double y) {
+ ensurePosition();
+ position.x = x;
+ position.y = y;
+ positionValid = true;
+ }
+
+ /**
+ * 璁剧疆鍓茶崏鏈烘柟鍚戯紙鐢ㄤ簬瀵艰埅棰勮绛夊満鏅級
+ * @param headingDegrees 鏂瑰悜瑙掑害锛堝害锛�0-360锛�
+ */
+ public void setHeading(double headingDegrees) {
+ double normalized = headingDegrees % 360.0;
+ if (normalized < 0) {
+ normalized += 360.0;
+ }
+ this.headingDegrees = normalized;
+ }
+
+ /**
+ * 鑾峰彇鍓茶崏鏈烘柟鍚�
+ * @return 鏂瑰悜瑙掑害锛堝害锛�0-360锛�
+ */
+ public double getHeading() {
+ return headingDegrees;
+ }
+
public double getWorldRadius(double scale) {
if (!positionValid) {
return Double.NaN;
@@ -177,4 +242,19 @@
double worldSize = (ICON_PIXEL_SIZE * ICON_SCALE_FACTOR) / Math.max(scale, MIN_SCALE);
return worldSize / 2.0;
}
+
+ public boolean useHandheldIcon(boolean handheldMode) {
+ if (handheldIconActive == handheldMode) {
+ return false;
+ }
+ handheldIconActive = handheldMode;
+ Image next = handheldMode
+ ? (handheldIcon != null ? handheldIcon : defaultIcon)
+ : defaultIcon;
+ if (next == null) {
+ next = handheldIcon;
+ }
+ activeIcon = next;
+ return true;
+ }
}
--
Gitblit v1.10.0