-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLineDistance.java
More file actions
57 lines (46 loc) · 2.1 KB
/
LineDistance.java
File metadata and controls
57 lines (46 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class LineDistance extends Application {
private Circle firstPoint = null;
private Circle secondPoint = null;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
pane.setOnMouseClicked(e -> {
if (firstPoint == null) {
firstPoint = createPoint(e.getX(), e.getY());
pane.getChildren().add(firstPoint);
} else if (secondPoint == null) {
secondPoint = createPoint(e.getX(), e.getY());
pane.getChildren().add(secondPoint);
Line line = new Line(firstPoint.getCenterX(), firstPoint.getCenterY(), secondPoint.getCenterX(), secondPoint.getCenterY());
pane.getChildren().add(line);
double distance = Math.sqrt(Math.pow(secondPoint.getCenterX() - firstPoint.getCenterX(), 2) +
Math.pow(secondPoint.getCenterY() - firstPoint.getCenterY(), 2));
Text distanceText = new Text((firstPoint.getCenterX() + secondPoint.getCenterX()) / 2,
(firstPoint.getCenterY() + secondPoint.getCenterY()) / 2,
String.format("%.2f", distance));
pane.getChildren().add(distanceText);
firstPoint = null;
secondPoint = null;
}
});
Scene scene = new Scene(pane, 600, 600);
primaryStage.setTitle("Line Distance Calculator");
primaryStage.setScene(scene);
primaryStage.show();
}
private Circle createPoint(double x, double y) {
Circle circle = new Circle(x, y, 5, Color.RED);
return circle;
}
}