package sample;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class RadarMonitor extends Application {
private static final int WIDTH = 400;
private static final int HEIGHT = 400;
private static final double CENTER_X = WIDTH / 2;
private static final double CENTER_Y = HEIGHT / 2;
private static final double RADIUS = 150;
private double angle = 0;
private List<Double> lineXHistory = new ArrayList<>();
private List<Double> lineYHistory = new ArrayList<>();
private double targetX;
private double targetY;
private Random random = new Random();
private long targetUpdateInterval = 500_000_000;
private long lastTargetUpdateTime = 0;
@Override
public void start(Stage primaryStage) {
Canvas canvas = new Canvas(WIDTH, HEIGHT);
GraphicsContext gc = canvas.getGraphicsContext2D();
resetTargetPosition();
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
gc.setFill(Color.BLACK);
gc.fillRect(0, 0, WIDTH, HEIGHT);
gc.setStroke(Color.GREEN);
gc.setLineWidth(2);
gc.strokeOval(CENTER_X - RADIUS, CENTER_Y - RADIUS, 2 * RADIUS, 2 * RADIUS);
drawScanLine(gc);
gc.setFill(Color.RED);
gc.fillOval(targetX - 5, targetY - 5, 10, 10);
angle += 1;
if (angle >= 360) {
angle = 0;
}
updateTargetPosition(now);
}
};
Group root = new Group(canvas);
Scene scene = new Scene(root, WIDTH, HEIGHT);
primaryStage.setTitle("雷达监控画面");
primaryStage.setScene(scene);
primaryStage.show();
timer.start();
}
private void drawScanLine(GraphicsContext gc) {
lineXHistory.add(CENTER_X + RADIUS * Math.cos(Math.toRadians(angle)));
lineYHistory.add(CENTER_Y + RADIUS * Math.sin(Math.toRadians(angle)));
for (int i = 0; i < lineXHistory.size(); i++) {
double alpha = 1.0 - (double) i / lineXHistory.size();
gc.setStroke(Color.color(1, 1, 0, alpha));
gc.setLineWidth(1);
gc.strokeLine(CENTER_X, CENTER_Y, lineXHistory.get(i), lineYHistory.get(i));
}
if (lineXHistory.size() > 100) {
lineXHistory.remove(0);
lineYHistory.remove(0);
}
}
private void resetTargetPosition() {
targetX = CENTER_X + random.nextDouble() * RADIUS * 2 - RADIUS;
targetY = CENTER_Y + random.nextDouble() * RADIUS * 2 - RADIUS;
}
private void updateTargetPosition(long now) {
if (now - lastTargetUpdateTime >= targetUpdateInterval) {
resetTargetPosition();
lastTargetUpdateTime = now;
}
}
public static void main(String[] args) {
launch(args);
}
}