| | |
| | | import zhangaiwu.Obstacledge; |
| | | import zhangaiwu.yulanzhangaiwu; |
| | | import yaokong.Control03; |
| | | import bianjie.shudongdraw; |
| | | |
| | | /** |
| | | * 地图渲染器 - 负责坐标系绘制、视图变换等功能 |
| | |
| | | private String boundaryName; |
| | | private boolean boundaryPointsVisible; |
| | | private boolean obstaclePointsVisible; |
| | | private boolean boundaryLengthVisible = false; // 是否显示边界距离,默认关闭 |
| | | private double boundaryPointSizeScale = 1.0d; |
| | | private boolean previewSizingEnabled; |
| | | private String currentBoundaryLandNumber; |
| | |
| | | private CircleCaptureOverlay circleCaptureOverlay; |
| | | private final List<double[]> circleSampleMarkers = new ArrayList<>(); |
| | | private final List<Point2D.Double> realtimeMowingTrack = new ArrayList<>(); |
| | | private final List<Point2D.Double> navigationPreviewTrack = new ArrayList<>(); // 导航预览轨迹 |
| | | private final Deque<tuowei.TrailSample> idleMowerTrail = new ArrayDeque<>(); |
| | | private final List<Point2D.Double> handheldBoundaryPreview = new ArrayList<>(); |
| | | private double boundaryPreviewMarkerScale = 1.0d; |
| | |
| | | private double mowingCompletionRatio; |
| | | private long lastTrackPersistTimeMillis; |
| | | private boolean trackDirty; |
| | | private boolean measurementModeActive = false; // 测量模式是否激活 |
| | | private boolean handheldBoundaryPreviewActive; |
| | | private boolean pendingTrackBreak = true; |
| | | private bianjie.shudongdraw manualBoundaryDrawer = new bianjie.shudongdraw(); // 手动绘制边界绘制器 |
| | | private boolean idleTrailSuppressed; |
| | | private Path2D.Double realtimeBoundaryPathCache; |
| | | private String realtimeBoundaryPathLand; |
| | | private WangfanDraw returnPathDrawer; // 往返路径绘制管理器 |
| | | private List<Point2D.Double> currentReturnPath; // 当前地块的往返路径(用于显示) |
| | | private List<Point2D.Double> previewReturnPath; // 预览的往返路径 |
| | | |
| | | private static final double TRACK_SAMPLE_MIN_DISTANCE_METERS = 0.2d; |
| | | private static final double TRACK_DUPLICATE_TOLERANCE_METERS = 1e-3d; |
| | |
| | | lastDragPoint = null; |
| | | dragInProgress = false; |
| | | } |
| | | |
| | | public void mouseExited(MouseEvent e) { |
| | | // 鼠标离开面板时,清除鼠标位置显示 |
| | | if (manualBoundaryDrawer.isManualBoundaryDrawingMode()) { |
| | | manualBoundaryDrawer.clearMousePosition(); |
| | | visualizationPanel.repaint(); |
| | | } |
| | | } |
| | | |
| | | public void mouseClicked(MouseEvent e) { |
| | | if (dragInProgress) { |
| | |
| | | if (!SwingUtilities.isLeftMouseButton(e) || e.getClickCount() != 1) { |
| | | return; |
| | | } |
| | | // 优先处理手动绘制边界模式点击 |
| | | if (manualBoundaryDrawer.isManualBoundaryDrawingMode()) { |
| | | Point2D.Double worldPoint = screenToWorld(e.getPoint()); |
| | | if (manualBoundaryDrawer.handleClick(worldPoint)) { |
| | | visualizationPanel.repaint(); |
| | | return; |
| | | } |
| | | } |
| | | // 优先处理测量模式点击 |
| | | if (measurementModeActive && handleMeasurementClick(e.getPoint())) { |
| | | return; |
| | | } |
| | | if (handleMowerClick(e.getPoint())) { |
| | | return; |
| | | } |
| | |
| | | visualizationPanel.repaint(); |
| | | } |
| | | } |
| | | |
| | | public void mouseMoved(MouseEvent e) { |
| | | // 在手动绘制边界模式时,更新鼠标位置 |
| | | if (manualBoundaryDrawer.isManualBoundaryDrawingMode()) { |
| | | Point2D.Double worldPoint = screenToWorld(e.getPoint()); |
| | | manualBoundaryDrawer.updateMousePosition(worldPoint); |
| | | visualizationPanel.repaint(); |
| | | } else { |
| | | manualBoundaryDrawer.clearMousePosition(); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | |
| | | |
| | | adddikuaiyulan.drawPreview(g2d, handheldBoundaryPreview, scale, handheldBoundaryPreviewActive, boundaryPreviewMarkerScale); |
| | | |
| | | // 绘制手动绘制的边界 |
| | | manualBoundaryDrawer.drawBoundary(g2d, scale); |
| | | |
| | | // 绘制鼠标实时位置(手动绘制边界模式时) |
| | | manualBoundaryDrawer.drawMousePosition(g2d, scale); |
| | | |
| | | // 绘制导航路径(中层) |
| | | if (hasPlannedPath) { |
| | | drawCurrentPlannedPath(g2d); |
| | |
| | | Obstacledraw.drawObstacles(g2d, currentObstacles, scale, selectedObstacleName); |
| | | } |
| | | |
| | | if (boundaryPointsVisible && hasBoundary) { |
| | | // 显示边界点(如果边界点可见,或者边界距离可见) |
| | | if ((boundaryPointsVisible || boundaryLengthVisible) && hasBoundary) { |
| | | // 预览模式下显示序号 |
| | | if (previewSizingEnabled) { |
| | | drawBoundaryPointsWithNumbers(g2d, currentBoundary, scale); |
| | |
| | | ); |
| | | } |
| | | } |
| | | |
| | | |
| | | // 绘制障碍物坐标点(带序号) |
| | | if (obstaclePointsVisible && hasObstacles) { |
| | | drawObstaclePointsWithNumbers(g2d, currentObstacles, scale); |
| | |
| | | if (!realtimeMowingTrack.isEmpty()) { |
| | | drawRealtimeMowingCoverage(g2d); |
| | | } |
| | | |
| | | // 绘制导航预览已割区域 |
| | | if (!navigationPreviewTrack.isEmpty()) { |
| | | drawNavigationPreviewCoverage(g2d); |
| | | } |
| | | |
| | | // 先画往返路径(线+点),保证割草机图标在其上方 |
| | | if (returnPathDrawer != null && returnPathDrawer.isActive()) { |
| | | returnPathDrawer.draw(g2d, scale); |
| | | } else if (previewReturnPath != null && !previewReturnPath.isEmpty()) { |
| | | // 绘制预览的往返路径(铁线路图风格) |
| | | WangfanDraw.drawRailwayPath(g2d, previewReturnPath, scale); |
| | | } else if (currentReturnPath != null && !currentReturnPath.isEmpty()) { |
| | | // 绘制保存的往返路径(铁线路图风格) |
| | | WangfanDraw.drawRailwayPath(g2d, currentReturnPath, scale); |
| | | } |
| | | |
| | | drawMower(g2d); |
| | | |
| | | // 绘制导航预览速度(如果正在导航预览) |
| | | if (navigationPreviewSpeed > 0 && mower != null && mower.hasValidPosition()) { |
| | | drawNavigationPreviewSpeed(g2d, scale); |
| | | } |
| | | |
| | | // 绘制测量模式(如果激活) |
| | | if (measurementModeActive) { |
| | | drawMeasurementMode(g2d, scale); |
| | | } |
| | | |
| | | // 保存当前变换(包含视图变换)用于坐标转换 |
| | | AffineTransform currentTransformForLength = g2d.getTransform(); |
| | | |
| | | // 恢复原始变换 |
| | | g2d.setTransform(originalTransform); |
| | | |
| | | // 绘制边界长度(如果启用)- 在恢复原始变换后绘制 |
| | | if (boundaryLengthVisible && hasBoundary) { |
| | | bianjie.BoundaryLengthDrawer.drawBoundaryLengths(g2d, currentBoundary, scale, |
| | | visualizationPanel.getWidth(), visualizationPanel.getHeight(), translateX, translateY); |
| | | } |
| | | |
| | | // 绘制视图信息 |
| | | drawViewInfo(g2d); |
| | | } |
| | |
| | | private void drawMower(Graphics2D g2d) { |
| | | mower.draw(g2d, scale); |
| | | } |
| | | |
| | | /** |
| | | * 绘制导航预览速度(在割草机图标上方) |
| | | */ |
| | | private void drawNavigationPreviewSpeed(Graphics2D g2d, double scale) { |
| | | if (mower == null || !mower.hasValidPosition()) { |
| | | return; |
| | | } |
| | | |
| | | Point2D.Double mowerPos = mower.getPosition(); |
| | | if (mowerPos == null) { |
| | | return; |
| | | } |
| | | |
| | | // 将速度从米/秒转换为KM/h |
| | | double speedKmh = navigationPreviewSpeed * 3.6; |
| | | String speedText = String.format("%.1f km/h", speedKmh); |
| | | |
| | | // 保存原始变换 |
| | | AffineTransform originalTransform = g2d.getTransform(); |
| | | |
| | | // 将世界坐标转换为屏幕坐标 |
| | | Point2D.Double screenPos = worldToScreen(mowerPos); |
| | | |
| | | // 恢复原始变换以绘制文字(固定大小,不随缩放变化) |
| | | g2d.setTransform(new AffineTransform()); |
| | | |
| | | // 设置字体(与缩放文字大小一致,11号字体) |
| | | Font labelFont = new Font("微软雅黑", Font.PLAIN, 11); |
| | | g2d.setFont(labelFont); |
| | | FontMetrics metrics = g2d.getFontMetrics(labelFont); |
| | | |
| | | // 计算文字位置(在割草机图标上方) |
| | | int textWidth = metrics.stringWidth(speedText); |
| | | int textHeight = metrics.getHeight(); |
| | | int textX = (int)Math.round(screenPos.x - textWidth / 2.0); |
| | | // 在割草机图标上方,留出一定间距 |
| | | // 图标在世界坐标系中的大小约为 48 * 0.8 / scale 米 |
| | | // 转换为屏幕像素:图标高度(像素)= (48 * 0.8 / scale) * scale = 48 * 0.8 = 38.4 像素 |
| | | double iconSizePixels = 48.0 * 0.8; // 图标在屏幕上的大小(像素) |
| | | int spacing = 8; // 间距(像素) |
| | | int textY = (int)Math.round(screenPos.y - iconSizePixels / 2.0 - spacing - textHeight); |
| | | |
| | | // 绘制文字背景(半透明白色,增强可读性) |
| | | g2d.setColor(new Color(255, 255, 255, 200)); |
| | | g2d.fillRoundRect(textX - 4, textY - metrics.getAscent() - 2, textWidth + 8, textHeight + 4, 4, 4); |
| | | |
| | | // 绘制文字 |
| | | g2d.setColor(new Color(46, 139, 87)); // 使用主题绿色 |
| | | g2d.drawString(speedText, textX, textY); |
| | | |
| | | // 恢复变换 |
| | | g2d.setTransform(originalTransform); |
| | | } |
| | | |
| | | private void drawRealtimeMowingCoverage(Graphics2D g2d) { |
| | | if (realtimeMowingTrack == null || realtimeMowingTrack.size() < 2) { |
| | |
| | | double effectiveWidth = getEffectiveMowerWidthMeters(); |
| | | gecaolunjing.draw(g2d, realtimeMowingTrack, effectiveWidth, boundaryPath); |
| | | } |
| | | |
| | | /** |
| | | * 绘制导航预览已割区域 |
| | | */ |
| | | private void drawNavigationPreviewCoverage(Graphics2D g2d) { |
| | | if (navigationPreviewTrack == null || navigationPreviewTrack.size() < 2) { |
| | | return; |
| | | } |
| | | |
| | | Path2D.Double boundaryPath = currentBoundaryPath; |
| | | // 获取导航预览的割草宽度(从daohangyulan获取) |
| | | double previewWidth = getNavigationPreviewWidth(); |
| | | if (previewWidth <= 0) { |
| | | previewWidth = 0.5; // 默认50厘米 |
| | | } |
| | | gecaolunjing.draw(g2d, navigationPreviewTrack, previewWidth, boundaryPath); |
| | | } |
| | | |
| | | /** |
| | | * 设置导航预览轨迹 |
| | | */ |
| | | public void setNavigationPreviewTrack(List<Point2D.Double> track) { |
| | | if (track == null) { |
| | | navigationPreviewTrack.clear(); |
| | | } else { |
| | | navigationPreviewTrack.clear(); |
| | | navigationPreviewTrack.addAll(track); |
| | | } |
| | | if (visualizationPanel != null) { |
| | | visualizationPanel.repaint(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 添加导航预览轨迹点 |
| | | */ |
| | | public void addNavigationPreviewTrackPoint(Point2D.Double point) { |
| | | if (point != null && Double.isFinite(point.x) && Double.isFinite(point.y)) { |
| | | navigationPreviewTrack.add(new Point2D.Double(point.x, point.y)); |
| | | if (visualizationPanel != null) { |
| | | visualizationPanel.repaint(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 清除导航预览轨迹 |
| | | */ |
| | | public void clearNavigationPreviewTrack() { |
| | | navigationPreviewTrack.clear(); |
| | | if (visualizationPanel != null) { |
| | | visualizationPanel.repaint(); |
| | | } |
| | | } |
| | | |
| | | private double navigationPreviewWidth = 0.5; // 导航预览的割草宽度(米) |
| | | private double navigationPreviewSpeed = 0.0; // 导航预览的割草机速度(米/秒) |
| | | |
| | | /** |
| | | * 设置导航预览的割草宽度 |
| | | */ |
| | | public void setNavigationPreviewWidth(double widthMeters) { |
| | | navigationPreviewWidth = widthMeters > 0 ? widthMeters : 0.5; |
| | | } |
| | | |
| | | /** |
| | | * 获取导航预览的割草宽度 |
| | | */ |
| | | private double getNavigationPreviewWidth() { |
| | | return navigationPreviewWidth; |
| | | } |
| | | |
| | | /** |
| | | * 设置导航预览的割草机速度(米/秒) |
| | | */ |
| | | public void setNavigationPreviewSpeed(double speedMetersPerSecond) { |
| | | navigationPreviewSpeed = speedMetersPerSecond >= 0 ? speedMetersPerSecond : 0.0; |
| | | } |
| | | |
| | | /** |
| | | * 获取导航预览的割草机速度(米/秒) |
| | | */ |
| | | private double getNavigationPreviewSpeed() { |
| | | return navigationPreviewSpeed; |
| | | } |
| | | |
| | | private Path2D.Double getRealtimeBoundaryPath() { |
| | | if (realtimeTrackLandNumber == null) { |
| | |
| | | mowerEffectiveWidthMeters = defaultMowerWidthMeters; |
| | | } |
| | | |
| | | // 加载往返路径 |
| | | String returnPathStr = dikuai != null ? dikuai.getReturnPathCoordinates() : null; |
| | | if (returnPathStr != null && !returnPathStr.isEmpty() && !"-1".equals(returnPathStr)) { |
| | | currentReturnPath = lujingdraw.parsePlannedPath(returnPathStr); |
| | | } else { |
| | | currentReturnPath = null; |
| | | } |
| | | |
| | | loadRealtimeTrack(landNumber, dikuai != null ? dikuai.getMowingTrack() : null); |
| | | visualizationPanel.repaint(); |
| | | } |
| | |
| | | double worldY = (screenPoint.y - visualizationPanel.getHeight() / 2.0) / scale - translateY; |
| | | return new Point2D.Double(worldX, worldY); |
| | | } |
| | | |
| | | /** |
| | | * 处理测量模式点击 |
| | | */ |
| | | private boolean handleMeasurementClick(Point screenPoint) { |
| | | if (!measurementModeActive) { |
| | | return false; |
| | | } |
| | | Point2D.Double worldPoint = screenToWorld(screenPoint); |
| | | celiangmoshi.addPoint(worldPoint); |
| | | visualizationPanel.repaint(); |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * 设置手动绘制边界模式 |
| | | */ |
| | | public void setManualBoundaryDrawingMode(boolean active) { |
| | | manualBoundaryDrawer.setManualBoundaryDrawingMode(active); |
| | | if (visualizationPanel != null) { |
| | | visualizationPanel.repaint(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取手动绘制的边界点列表 |
| | | */ |
| | | public List<Point2D.Double> getManualBoundaryPoints() { |
| | | return manualBoundaryDrawer.getManualBoundaryPoints(); |
| | | } |
| | | |
| | | /** |
| | | * 清空手动绘制的边界点 |
| | | */ |
| | | public void clearManualBoundaryPoints() { |
| | | manualBoundaryDrawer.clearManualBoundaryPoints(); |
| | | if (visualizationPanel != null) { |
| | | visualizationPanel.repaint(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 设置测量模式 |
| | | */ |
| | | public void setMeasurementMode(boolean active) { |
| | | measurementModeActive = active; |
| | | if (!active) { |
| | | celiangmoshi.clear(); |
| | | } |
| | | if (visualizationPanel != null) { |
| | | visualizationPanel.repaint(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 触发地图重绘 |
| | | */ |
| | | public void repaint() { |
| | | if (visualizationPanel != null) { |
| | | visualizationPanel.repaint(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 绘制测量模式 |
| | | */ |
| | | private void drawMeasurementMode(Graphics2D g2d, double scale) { |
| | | List<Point2D.Double> points = celiangmoshi.getPoints(); |
| | | if (points.isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | // 保存原始变换 |
| | | AffineTransform originalTransform = g2d.getTransform(); |
| | | |
| | | // 设置测量模式颜色 |
| | | Color lineColor = new Color(255, 0, 0, 200); // 红色半透明 |
| | | Color pointColor = new Color(255, 0, 0, 255); // 红色 |
| | | Color textColor = new Color(33, 37, 41, 220); // 深灰色文字 |
| | | |
| | | // 设置线宽和点的大小(确保点和线连接) |
| | | float lineWidth = (float)(2.0 / scale); |
| | | // 点的大小(在世界坐标系中,米),确保点足够大以覆盖线的端点 |
| | | double pointSizeInWorld = 0.15d; // 点的大小(米) |
| | | double halfSize = pointSizeInWorld / 2.0; |
| | | |
| | | // 先绘制所有测量点(作为基础层) |
| | | g2d.setColor(pointColor); |
| | | for (Point2D.Double point : points) { |
| | | // 点的中心在 point.x, point.y |
| | | Ellipse2D.Double pointShape = new Ellipse2D.Double( |
| | | point.x - halfSize, |
| | | point.y - halfSize, |
| | | pointSizeInWorld, |
| | | pointSizeInWorld |
| | | ); |
| | | g2d.fill(pointShape); |
| | | } |
| | | |
| | | // 然后绘制连线,确保线从点的中心开始和结束,点和线连接在一起 |
| | | g2d.setColor(lineColor); |
| | | g2d.setStroke(new BasicStroke(lineWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); |
| | | |
| | | // 绘制连线和距离文字(显示两个相邻点连线的长度) |
| | | for (int i = 0; i < points.size() - 1; i++) { |
| | | Point2D.Double p1 = points.get(i); |
| | | Point2D.Double p2 = points.get(i + 1); |
| | | |
| | | // 绘制连线(从第一个点的中心到第二个点的中心) |
| | | // 使用 Path2D 确保精确绘制,保持浮点精度 |
| | | Path2D.Double linePath = new Path2D.Double(); |
| | | linePath.moveTo(p1.x, p1.y); |
| | | linePath.lineTo(p2.x, p2.y); |
| | | g2d.draw(linePath); |
| | | |
| | | // 计算距离(两个相邻点连线的长度) |
| | | double distance = celiangmoshi.calculateDistance(p1, p2); |
| | | String distanceText = celiangmoshi.formatDistance(distance); |
| | | |
| | | // 计算中点位置(用于显示文字) |
| | | double midX = (p1.x + p2.x) / 2.0; |
| | | double midY = (p1.y + p2.y) / 2.0; |
| | | |
| | | // 将世界坐标转换为屏幕坐标(用于文字显示) |
| | | Point2D.Double worldMid = new Point2D.Double(midX, midY); |
| | | Point2D.Double screenMid = worldToScreen(worldMid); |
| | | |
| | | // 恢复原始变换以绘制文字(固定大小,不随缩放变化) |
| | | g2d.setTransform(new AffineTransform()); |
| | | |
| | | // 设置字体(与缩放文字大小一致,11号字体) |
| | | Font labelFont = new Font("微软雅黑", Font.PLAIN, 11); |
| | | g2d.setFont(labelFont); |
| | | FontMetrics metrics = g2d.getFontMetrics(labelFont); |
| | | |
| | | // 计算文字位置(居中显示) |
| | | int textWidth = metrics.stringWidth(distanceText); |
| | | int textHeight = metrics.getHeight(); |
| | | int textX = (int)Math.round(screenMid.x - textWidth / 2.0); |
| | | int textY = (int)Math.round(screenMid.y - textHeight / 2.0) + metrics.getAscent(); |
| | | |
| | | // 绘制文字背景(可选,用于提高可读性) |
| | | g2d.setColor(new Color(255, 255, 255, 200)); |
| | | g2d.fillRoundRect(textX - 2, textY - metrics.getAscent() - 2, textWidth + 4, textHeight + 4, 4, 4); |
| | | |
| | | // 绘制文字 |
| | | g2d.setColor(textColor); |
| | | g2d.drawString(distanceText, textX, textY); |
| | | |
| | | // 恢复变换 |
| | | g2d.setTransform(originalTransform); |
| | | } |
| | | |
| | | // 最后再次绘制测量点(在连线之上,确保点覆盖在线的端点上,点和线连接在一起) |
| | | g2d.setColor(pointColor); |
| | | for (Point2D.Double point : points) { |
| | | // 点的中心在 point.x, point.y,正好是线的端点位置 |
| | | Ellipse2D.Double pointShape = new Ellipse2D.Double( |
| | | point.x - halfSize, |
| | | point.y - halfSize, |
| | | pointSizeInWorld, |
| | | pointSizeInWorld |
| | | ); |
| | | g2d.fill(pointShape); |
| | | } |
| | | } |
| | | |
| | | private void drawCurrentBoundary(Graphics2D g2d) { |
| | | bianjiedrwa.drawBoundary(g2d, currentBoundary, scale, GRASS_FILL_COLOR, GRASS_BORDER_COLOR); |
| | |
| | | if (markers == null || markers.isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | // 保存原始变换 |
| | | AffineTransform originalTransform = g2d.getTransform(); |
| | | |
| | | Shape markerShape; |
| | | double half = CIRCLE_SAMPLE_SIZE / 2.0; |
| | | g2d.setColor(CIRCLE_SAMPLE_COLOR); |
| | | g2d.setStroke(new BasicStroke((float) (1.2f / scale), BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); |
| | | Font originalFont = g2d.getFont(); |
| | | float baseSize = (float) Math.max(12f / scale, 9f); |
| | | float reducedSize = Math.max(baseSize / 3f, 3f); |
| | | Font labelFont = originalFont.deriveFont(reducedSize); |
| | | |
| | | // 设置字体(与缩放文字大小一致,11号字体,不随缩放变化) |
| | | Font labelFont = new Font("微软雅黑", Font.PLAIN, 11); |
| | | g2d.setFont(labelFont); |
| | | FontMetrics metrics = g2d.getFontMetrics(); |
| | | FontMetrics metrics = g2d.getFontMetrics(labelFont); |
| | | |
| | | for (double[] pt : markers) { |
| | | if (pt == null || pt.length < 2 || !Double.isFinite(pt[0]) || !Double.isFinite(pt[1])) { |
| | | continue; |
| | | } |
| | | double x = pt[0]; |
| | | double y = pt[1]; |
| | | |
| | | // 绘制点(在世界坐标系中,随缩放变化) |
| | | markerShape = new Ellipse2D.Double(x - half, y - half, CIRCLE_SAMPLE_SIZE, CIRCLE_SAMPLE_SIZE); |
| | | g2d.fill(markerShape); |
| | | |
| | | // 将世界坐标转换为屏幕坐标以绘制文字(不随缩放变化) |
| | | Point2D.Double worldPoint = new Point2D.Double(x, y); |
| | | Point2D.Double screenPoint = new Point2D.Double(); |
| | | originalTransform.transform(worldPoint, screenPoint); |
| | | |
| | | // 恢复原始变换以使用屏幕坐标绘制文字 |
| | | g2d.setTransform(new AffineTransform()); |
| | | |
| | | String label = String.format(Locale.US, "%.2f,%.2f", x, y); |
| | | int textWidth = metrics.stringWidth(label); |
| | | float textX = (float) (x - textWidth / 2.0); |
| | | float textY = (float) (y - half - 0.2d) - metrics.getDescent(); |
| | | int textHeight = metrics.getHeight(); |
| | | |
| | | // 在屏幕坐标系中绘制文字(不随缩放变化) |
| | | int textX = (int)(screenPoint.x - textWidth / 2.0); |
| | | int textY = (int)(screenPoint.y - half - 0.2d) - metrics.getDescent(); |
| | | g2d.setColor(new Color(33, 37, 41, 220)); |
| | | g2d.drawString(label, textX, textY); |
| | | |
| | | // 恢复原始变换 |
| | | g2d.setTransform(originalTransform); |
| | | g2d.setColor(CIRCLE_SAMPLE_COLOR); |
| | | } |
| | | g2d.setFont(originalFont); |
| | | } |
| | | |
| | | private void drawCircleCaptureOverlay(Graphics2D g2d, CircleCaptureOverlay overlay, double scale) { |
| | |
| | | return; |
| | | } |
| | | |
| | | // 设置点的大小(随缩放变化) |
| | | // 设置点的大小(边界线宽度的2倍) |
| | | // 边界线宽度:3 / Math.max(0.5, scale) |
| | | 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 boundaryLineWidth = 3.0 / scaleFactor; // 边界线宽度 |
| | | double markerDiameter = boundaryLineWidth * 2.0; // 边界点直径 = 边界线宽度的2倍 |
| | | double markerRadius = markerDiameter / 2.0; |
| | | |
| | | // 设置字体(与障碍物序号一致,不随缩放变化) |
| | |
| | | this.obstaclePointsVisible = visible; |
| | | visualizationPanel.repaint(); |
| | | } |
| | | |
| | | /** |
| | | * 设置是否显示边界距离 |
| | | */ |
| | | public void setBoundaryLengthVisible(boolean visible) { |
| | | boundaryLengthVisible = visible; |
| | | if (visualizationPanel != null) { |
| | | visualizationPanel.repaint(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取是否显示边界距离 |
| | | */ |
| | | public boolean isBoundaryLengthVisible() { |
| | | return boundaryLengthVisible; |
| | | } |
| | | |
| | | public void setBoundaryPointSizeScale(double sizeScale) { |
| | | double normalized = (Double.isFinite(sizeScale) && sizeScale > 0.0d) ? sizeScale : 1.0d; |
| | |
| | | public Gecaoji getMower() { |
| | | return mower; |
| | | } |
| | | |
| | | /** |
| | | * 设置往返路径绘制管理器 |
| | | */ |
| | | public void setReturnPathDrawer(WangfanDraw drawer) { |
| | | this.returnPathDrawer = drawer; |
| | | } |
| | | |
| | | /** |
| | | * 设置预览的往返路径 |
| | | */ |
| | | public void setPreviewReturnPath(List<Point2D.Double> path) { |
| | | this.previewReturnPath = path; |
| | | if (visualizationPanel != null) { |
| | | visualizationPanel.repaint(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 开始往返路径绘制 |
| | | */ |
| | | public void startReturnPathDrawing() { |
| | | if (returnPathDrawer != null) { |
| | | // 禁用拖尾效果(在往返路径绘制模式下不显示实时轨迹拖尾) |
| | | idleTrailSuppressed = true; |
| | | clearIdleMowerTrail(); |
| | | // 清空之前的路径点(通过 WangfanDraw 管理) |
| | | repaint(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 停止往返路径绘制 |
| | | */ |
| | | public void stopReturnPathDrawing() { |
| | | // 恢复拖尾效果 |
| | | idleTrailSuppressed = false; |
| | | repaint(); |
| | | } |
| | | |
| | | /** |
| | | * 添加往返路径点(已废弃,路径点由 WangfanDraw 直接管理) |
| | | */ |
| | | @Deprecated |
| | | public void addReturnPathPoint(double x, double y) { |
| | | // 路径点由 WangfanDraw 直接管理,这里只需要重绘 |
| | | repaint(); |
| | | } |
| | | |
| | | /** |
| | | * 获取往返路径点列表的快照 |
| | | */ |
| | | public List<Point2D.Double> getReturnPathPointsSnapshot() { |
| | | if (returnPathDrawer != null) { |
| | | return returnPathDrawer.getPointsSnapshot(); |
| | | } |
| | | return new ArrayList<>(); |
| | | } |
| | | |
| | | } |