package zhuye;
|
|
import java.awt.BasicStroke;
|
import java.awt.Color;
|
import java.awt.Graphics2D;
|
import java.awt.Stroke;
|
import java.awt.geom.Path2D;
|
import java.awt.geom.Point2D;
|
|
/**
|
* 割草机拖尾绘制工具类。
|
*/
|
public final class tuowei {
|
private static final Color TRAIL_COLOR = new Color(30, 144, 255, 220);
|
|
private tuowei() {
|
// 工具类无需实例化
|
}
|
|
/**
|
* 绘制拖尾路径。
|
*
|
* @param g2d 画笔对象
|
* @param samples 拖尾采样点集合
|
* @param scale 当前缩放比例
|
*/
|
public static void draw(Graphics2D g2d, Iterable<TrailSample> samples, double scale) {
|
if (g2d == null || samples == null) {
|
return;
|
}
|
|
Path2D.Double path = new Path2D.Double();
|
boolean started = false;
|
for (TrailSample sample : samples) {
|
if (sample == null) {
|
continue;
|
}
|
Point2D.Double point = sample.getPoint();
|
if (point == null || !Double.isFinite(point.x) || !Double.isFinite(point.y)) {
|
continue;
|
}
|
if (!started) {
|
path.moveTo(point.x, point.y);
|
started = true;
|
} else {
|
path.lineTo(point.x, point.y);
|
}
|
}
|
|
if (!started) {
|
return;
|
}
|
|
Stroke originalStroke = g2d.getStroke();
|
Color originalColor = g2d.getColor();
|
|
float strokeWidth = (float) Math.max(0.08f, 1.4f / (float) Math.max(scale, 0.2));
|
g2d.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
|
g2d.setColor(TRAIL_COLOR);
|
g2d.draw(path);
|
|
g2d.setStroke(originalStroke);
|
g2d.setColor(originalColor);
|
}
|
|
/**
|
* 拖尾采样点。
|
*/
|
public static final class TrailSample {
|
private final long timestamp;
|
private final Point2D.Double point;
|
|
public TrailSample(long timestamp, Point2D.Double point) {
|
this.timestamp = timestamp;
|
this.point = point;
|
}
|
|
public long getTimestamp() {
|
return timestamp;
|
}
|
|
public Point2D.Double getPoint() {
|
return point;
|
}
|
}
|
}
|