From f784463ab019c1113cf0b31a249e8802b07a57fa Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期二, 16 十二月 2025 15:26:15 +0800
Subject: [PATCH] 保存地图最后位置
---
src/zhuye/Shouye.java | 302 ++++++++++++++++++++++++++++++++++++++------------
1 files changed, 230 insertions(+), 72 deletions(-)
diff --git a/src/zhuye/Shouye.java b/src/zhuye/Shouye.java
index bf46c63..c462a83 100644
--- a/src/zhuye/Shouye.java
+++ b/src/zhuye/Shouye.java
@@ -15,7 +15,10 @@
import dikuai.Dikuaiguanli;
import dikuai.addzhangaiwu;
import gecaoji.Device;
+import gecaoji.Gecaoji;
+import gecaoji.MowerBoundaryChecker;
import set.Sets;
+import set.debug;
import udpdell.UDPServer;
import zhangaiwu.AddDikuai;
import yaokong.Control04;
@@ -64,7 +67,7 @@
private JLabel mowerSpeedValueLabel;
private JLabel mowerSpeedUnitLabel;
private JLabel mowingProgressLabel;
- private FixQualityIndicator fixQualityIndicator;
+ private gpszhuangtai fixQualityIndicator;
// 瀵艰埅鎸夐挳
private JButton homeNavBtn;
@@ -88,10 +91,20 @@
private Sets settingsDialog;
private BaseStation baseStation;
- private final Consumer<String> serialLineListener = line -> SwingUtilities.invokeLater(this::updateDataPacketCountLabel);
-
// 鍦板浘娓叉煋鍣�
private MapRenderer mapRenderer;
+
+ private final Consumer<String> serialLineListener = line -> {
+ SwingUtilities.invokeLater(() -> {
+ updateDataPacketCountLabel();
+ // 濡傛灉鏀跺埌$GNGGA鏁版嵁锛岀珛鍗虫洿鏂版嫋灏�
+ if (line != null && line.trim().startsWith("$GNGGA")) {
+ if (mapRenderer != null) {
+ mapRenderer.forceUpdateIdleMowerTrail();
+ }
+ }
+ });
+ };
private static final int FLOAT_ICON_SIZE = 32;
private JButton endDrawingButton;
private JButton drawingPauseButton;
@@ -205,15 +218,51 @@
SwingUtilities.invokeLater(() -> {
Shouye.this.checkIdentifiersAndPromptIfNeeded();
Shouye.this.showInitialMowerSelfCheckDialogIfNeeded();
+ // 璁剧疆绐楀彛鍏抽棴鐩戝惉鍣紝鍦ㄥ叧闂椂淇濆瓨缂╂斁姣斾緥
+ setupWindowCloseListener();
});
}
}
};
addHierarchyListener(listener);
}
+
+ /**
+ * 璁剧疆绐楀彛鍏抽棴鐩戝惉鍣紝鍦ㄧ獥鍙e叧闂椂淇濆瓨褰撳墠缂╂斁姣斾緥
+ */
+ private void setupWindowCloseListener() {
+ Window window = SwingUtilities.getWindowAncestor(this);
+ if (window != null && window instanceof JFrame) {
+ JFrame frame = (JFrame) window;
+ frame.addWindowListener(new WindowAdapter() {
+ @Override
+ public void windowClosing(WindowEvent e) {
+ // 淇濆瓨褰撳墠缂╂斁姣斾緥
+ saveCurrentScale();
+ }
+ });
+ }
+ }
+
+ /**
+ * 淇濆瓨褰撳墠鍦板浘缂╂斁姣斾緥鍜岃鍥句腑蹇冨潗鏍囧埌閰嶇疆鏂囦欢
+ */
+ public void saveCurrentScale() {
+ if (mapRenderer != null) {
+ double currentScale = mapRenderer.getScale();
+ double translateX = mapRenderer.getTranslateX();
+ double translateY = mapRenderer.getTranslateY();
+ Setsys setsys = new Setsys();
+ // 淇濈暀2浣嶅皬鏁�
+ setsys.updateProperty("mapScale", String.format("%.2f", currentScale));
+ setsys.updateProperty("viewCenterX", String.format("%.2f", translateX));
+ setsys.updateProperty("viewCenterY", String.format("%.2f", translateY));
+ }
+ }
private void showInitialMowerSelfCheckDialogIfNeeded() {
- zijian.showInitialPromptIfNeeded(this, this::showRemoteControlDialog);
+ // 宸茬Щ闄よ繘鍏ヤ富椤垫椂鐨勮嚜妫�鎻愮ず锛堟寜鐢ㄦ埛瑕佹眰鍒犻櫎锛�
+ // 浠ュ墠杩欓噷浼氳皟鐢� zijian.showInitialPromptIfNeeded(...) 灞曠ず鑷瀵硅瘽妗嗭紝鐜板凡绂佺敤銆�
}
private void applyIdleTrailDurationFromSettings() {
@@ -348,6 +397,37 @@
// 鍙鍖栧尯鍩� - 浣跨敤MapRenderer杩涜缁樺埗
visualizationPanel = new JPanel() {
+ private ImageIcon gecaojiIcon = null;
+ private static final int GECAOJI_ICON_X = 37;
+ private static final int GECAOJI_ICON_Y = 10;
+ private static final int GECAOJI_ICON_SIZE = 20;
+
+ {
+ // 鍔犺浇鍓茶崏鏈哄浘鏍囷紝澶у皬20x20鍍忕礌
+ gecaojiIcon = loadScaledIcon("image/gecaoji.png", GECAOJI_ICON_SIZE, GECAOJI_ICON_SIZE);
+ // 鍚敤宸ュ叿鎻愮ず
+ setToolTipText("");
+ }
+
+ /**
+ * 妫�鏌ラ紶鏍囦綅缃槸鍚﹀湪鍓茶崏鏈哄浘鏍囧尯鍩熷唴
+ */
+ private boolean isMouseOnGecaojiIcon(Point mousePoint) {
+ return mousePoint.x >= GECAOJI_ICON_X &&
+ mousePoint.x <= GECAOJI_ICON_X + GECAOJI_ICON_SIZE &&
+ mousePoint.y >= GECAOJI_ICON_Y &&
+ mousePoint.y <= GECAOJI_ICON_Y + GECAOJI_ICON_SIZE;
+ }
+
+ @Override
+ public String getToolTipText(MouseEvent event) {
+ // 濡傛灉榧犳爣鍦ㄥ壊鑽夋満鍥炬爣鍖哄煙鍐咃紝鏄剧ず鎻愮ず鏂囧瓧
+ if (isMouseOnGecaojiIcon(event.getPoint())) {
+ return "浠ュ壊鑽夋満涓轰腑蹇�";
+ }
+ return super.getToolTipText(event);
+ }
+
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
@@ -355,14 +435,48 @@
if (mapRenderer != null) {
mapRenderer.renderMap(g);
}
+ // 鍦ㄥ湴鍥惧乏涓婅缁樺埗鍓茶崏鏈哄浘鏍�
+ // 姘村钩鏂瑰悜涓庨�熷害鎸囩ず鍣ㄥ榻愶紙x=37锛�
+ // 鍨傜洿鏂瑰悜涓庡崼鏄熺姸鎬佸浘鏍囧榻愶紙y=10锛岄�熷害鎸囩ず鍣ㄩ潰鏉块《閮ㄨ竟璺�10鍍忕礌锛屼娇鍥炬爣涓績瀵归綈锛�
+ if (gecaojiIcon != null) {
+ g.drawImage(gecaojiIcon.getImage(), GECAOJI_ICON_X, GECAOJI_ICON_Y, null);
+ }
}
};
visualizationPanel.setLayout(new BorderLayout());
+
+ // 娣诲姞榧犳爣鐐瑰嚮鐩戝惉鍣紝妫�娴嬫槸鍚︾偣鍑讳簡鍓茶崏鏈哄浘鏍�
+ visualizationPanel.addMouseListener(new MouseAdapter() {
+ @Override
+ public void mouseClicked(MouseEvent e) {
+ if (SwingUtilities.isLeftMouseButton(e)) {
+ Point clickPoint = e.getPoint();
+ // 妫�鏌ユ槸鍚︾偣鍑讳簡鍓茶崏鏈哄浘鏍囧尯鍩燂紙37, 10, 20, 20锛�
+ if (clickPoint.x >= 37 && clickPoint.x <= 57 &&
+ clickPoint.y >= 10 && clickPoint.y <= 30) {
+ // 鐐瑰嚮浜嗗壊鑽夋満鍥炬爣锛屽皢鍦板浘瑙嗗浘涓績绉诲姩鍒板壊鑽夋満浣嶇疆
+ if (mapRenderer != null) {
+ Gecaoji mower = mapRenderer.getMower();
+ if (mower != null && mower.hasValidPosition()) {
+ Point2D.Double mowerPosition = mower.getPosition();
+ if (mowerPosition != null) {
+ // 鑾峰彇褰撳墠缂╂斁姣斾緥
+ double currentScale = mapRenderer.getScale();
+ // 璁剧疆瑙嗗浘鍙樻崲锛屼娇鍓茶崏鏈轰綅缃搴斿埌灞忓箷涓績
+ // translateX = -mowerX, translateY = -mowerY 鍙互璁╁壊鑽夋満鍦ㄥ睆骞曚腑蹇�
+ mapRenderer.setViewTransform(currentScale, -mowerPosition.x, -mowerPosition.y);
+ }
+ }
+ }
+ }
+ }
+ }
+ });
JPanel speedIndicatorPanel = createSpeedIndicatorPanel();
visualizationPanel.add(speedIndicatorPanel, BorderLayout.NORTH);
- // 鍒涘缓鍔熻兘鎸夐挳闈㈡澘锛堟斁鍦ㄥ乏涓婅锛�
+ // 鍒涘缓鍔熻兘鎸夐挳闈㈡澘
JPanel functionButtonsPanel = new JPanel();
functionButtonsPanel.setLayout(new BoxLayout(functionButtonsPanel, BoxLayout.Y_AXIS));
functionButtonsPanel.setOpaque(false);
@@ -1125,32 +1239,121 @@
return;
}
if (startButtonShowingPause) {
- if (!zijian.ensureBeforeMowing(this, this::showRemoteControlDialog)) {
- return;
- }
+ // 鐐瑰嚮寮�濮嬫寜閽椂涓嶅啀寮瑰嚭鑷鎻愮ず锛堟寜鐢ㄦ埛瑕佹眰鍒犻櫎锛�
+ // 鏃ч�昏緫锛氳皟鐢� zijian.ensureBeforeMowing(...) 骞跺湪鏈‘璁よ嚜妫�鏃堕樆姝㈠紑濮�
+ // 鏂伴�昏緫锛氱洿鎺ュ厑璁稿紑濮嬩綔涓�
}
startButtonShowingPause = !startButtonShowingPause;
if (!startButtonShowingPause) {
- statusLabel.setText("浣滀笟涓�");
- if (stopButtonActive) {
- stopButtonActive = false;
- updateStopButtonIcon();
- }
- if (!beginMowingSession()) {
+ // 妫�鏌ュ壊鑽夋満鏄惁鍦ㄤ綔涓氬湴鍧楄竟鐣岃寖鍥村唴
+ if (!checkMowerInBoundary()) {
startButtonShowingPause = true;
statusLabel.setText("寰呮満");
updateStartButtonAppearance();
return;
}
- Control04.sendStartCommandIfDebugSerialOpen();
+
+ statusLabel.setText("浣滀笟涓�");
+ if (stopButtonActive) {
+ stopButtonActive = false;
+ updateStopButtonIcon();
+ }
+ if (!beginMowingSession()) {
+ startButtonShowingPause = true;
+ statusLabel.setText("寰呮満");
+ updateStartButtonAppearance();
+ return;
+ }
} else {
statusLabel.setText("鏆傚仠涓�");
pauseMowingSession();
- Control04.sendPauseCommandIfDebugSerialOpen();
}
updateStartButtonAppearance();
}
+ /**
+ * 妫�鏌ュ壊鑽夋満鏄惁鍦ㄥ綋鍓嶉�変腑鐨勪綔涓氬湴鍧楄竟鐣岃寖鍥村唴
+ * @return 濡傛灉鍓茶崏鏈哄湪杈圭晫鍐呰繑鍥瀟rue锛屽惁鍒欒繑鍥瀎alse骞舵樉绀烘彁绀�
+ */
+ private boolean checkMowerInBoundary() {
+ if (mapRenderer == null) {
+ return true; // 濡傛灉娌℃湁鍦板浘娓叉煋鍣紝璺宠繃妫�鏌�
+ }
+
+ // 鑾峰彇褰撳墠杈圭晫
+ List<Point2D.Double> boundary = mapRenderer.getCurrentBoundary();
+ if (boundary == null || boundary.size() < 3) {
+ return true; // 濡傛灉娌℃湁杈圭晫鎴栬竟鐣岀偣涓嶈冻锛岃烦杩囨鏌�
+ }
+
+ // 鑾峰彇鍓茶崏鏈轰綅缃�
+ Gecaoji mower = mapRenderer.getMower();
+ if (mower == null || !mower.hasValidPosition()) {
+ showCustomMessageDialog("鏃犳硶鑾峰彇鍓茶崏鏈轰綅缃紝璇锋鏌ヨ澶囪繛鎺�", "鎻愮ず");
+ return false;
+ }
+
+ Point2D.Double mowerPosition = mower.getPosition();
+ if (mowerPosition == null) {
+ showCustomMessageDialog("鏃犳硶鑾峰彇鍓茶崏鏈轰綅缃紝璇锋鏌ヨ澶囪繛鎺�", "鎻愮ず");
+ return false;
+ }
+
+ // 浣跨敤 MowerBoundaryChecker 妫�鏌ユ槸鍚﹀湪杈圭晫鍐�
+ boolean isInside = MowerBoundaryChecker.isInsideBoundaryPoints(
+ boundary,
+ mowerPosition.x,
+ mowerPosition.y
+ );
+
+ if (!isInside) {
+ showCustomMessageDialog("璇峰皢鍓茶崏鏈哄紑鍒颁綔涓氬湴鍧楀唴鐒跺悗鐐瑰嚮寮�濮嬩綔涓�", "鎻愮ず");
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * 鏄剧ず鑷畾涔夋秷鎭璇濇锛屼娇鐢� buttonset 鍒涘缓纭畾鎸夐挳
+ * @param message 娑堟伅鍐呭
+ * @param title 瀵硅瘽妗嗘爣棰�
+ */
+ private void showCustomMessageDialog(String message, String title) {
+ Window parentWindow = SwingUtilities.getWindowAncestor(this);
+ JDialog dialog = new JDialog(parentWindow, title, Dialog.ModalityType.APPLICATION_MODAL);
+ dialog.setLayout(new BorderLayout(20, 20));
+ dialog.setResizable(false);
+
+ // 鍐呭闈㈡澘
+ JPanel contentPanel = new JPanel(new BorderLayout(0, 15));
+ contentPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 10, 20));
+ contentPanel.setBackground(Color.WHITE);
+
+ // 娑堟伅鏍囩
+ JLabel messageLabel = new JLabel("<html><div style='text-align: center;'>" + message + "</div></html>");
+ messageLabel.setFont(new Font("寰蒋闆呴粦", Font.PLAIN, 14));
+ messageLabel.setHorizontalAlignment(SwingConstants.CENTER);
+ contentPanel.add(messageLabel, BorderLayout.CENTER);
+
+ // 鎸夐挳闈㈡澘
+ JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 0));
+ buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
+ buttonPanel.setOpaque(false);
+
+ // 浣跨敤 buttonset 鍒涘缓纭畾鎸夐挳
+ JButton okButton = buttonset.createStyledButton("纭畾", THEME_COLOR);
+ okButton.addActionListener(e -> dialog.dispose());
+ buttonPanel.add(okButton);
+
+ contentPanel.add(buttonPanel, BorderLayout.SOUTH);
+ dialog.add(contentPanel, BorderLayout.CENTER);
+
+ dialog.pack();
+ dialog.setLocationRelativeTo(this);
+ dialog.setVisible(true);
+ }
+
private void handleStopAction() {
if (handheldCaptureInlineUiActive) {
handleHandheldFinishAction();
@@ -1166,7 +1369,6 @@
statusLabel.setText("宸茬粨鏉�");
startButtonShowingPause = false;
stopMowingSession();
- Control04.sendStopCommandIfDebugSerialOpen();
} else {
statusLabel.setText("寰呮満");
startButtonShowingPause = true;
@@ -1458,19 +1660,15 @@
if (bluetoothBtn == null) {
return;
}
- if (Bluelink.isConnected()) {
- Bluelink.disconnect();
- bluetoothConnected = false;
- } else {
- boolean success = Bluelink.connect();
- if (success) {
- bluetoothConnected = true;
- } else {
- bluetoothConnected = false;
- JOptionPane.showMessageDialog(this, "钃濈墮杩炴帴澶辫触锛岃閲嶈瘯", "鎻愮ず", JOptionPane.WARNING_MESSAGE);
- }
- }
- updateBluetoothButtonIcon();
+ // 寮瑰嚭绯荤粺璋冭瘯椤甸潰
+ showDebugDialog();
+ }
+
+ private void showDebugDialog() {
+ Window parentWindow = SwingUtilities.getWindowAncestor(this);
+ debug debugDialog = new debug(parentWindow, THEME_COLOR);
+ debugDialog.setLocationRelativeTo(this); // 灞呬腑鏄剧ず鍦ㄩ椤�
+ debugDialog.setVisible(true);
}
private void updateBluetoothButtonIcon() {
@@ -1496,7 +1694,7 @@
JPanel rightPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 0));
rightPanel.setOpaque(false);
- fixQualityIndicator = new FixQualityIndicator();
+ fixQualityIndicator = new gpszhuangtai(THEME_COLOR);
fixQualityIndicator.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
fixQualityIndicator.addMouseListener(new MouseAdapter() {
@Override
@@ -3289,47 +3487,7 @@
return !"鏈�夋嫨鍦板潡".equals(trimmed);
}
- private final class FixQualityIndicator extends JComponent {
- private static final long serialVersionUID = 1L;
- private static final int DIAMETER = 16;
- private String currentCode;
- private Color currentColor = new Color(160, 160, 160);
-
- private FixQualityIndicator() {
- setPreferredSize(new Dimension(DIAMETER, DIAMETER));
- setMinimumSize(new Dimension(DIAMETER, DIAMETER));
- setMaximumSize(new Dimension(DIAMETER, DIAMETER));
- setToolTipText("鏈煡");
- }
-
- private void setQuality(String code) {
- if (Objects.equals(currentCode, code)) {
- return;
- }
- currentCode = code;
- currentColor = resolveFixQualityColor(code);
- setToolTipText(resolveFixQualityDescription(code));
- repaint();
- }
-
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- Graphics2D g2 = (Graphics2D) g.create();
- try {
- g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- int diameter = Math.min(getWidth(), getHeight()) - 2;
- int x = (getWidth() - diameter) / 2;
- int y = (getHeight() - diameter) / 2;
- g2.setColor(currentColor);
- g2.fillOval(x, y, diameter, diameter);
- g2.setColor(new Color(255, 255, 255, 128));
- g2.drawOval(x, y, diameter, diameter);
- } finally {
- g2.dispose();
- }
- }
- }
+
// 娴嬭瘯鏂规硶
public static void main(String[] args) {
--
Gitblit v1.10.0