|
| 1 | +package ch_15; |
| 2 | + |
| 3 | +import javafx.application.Application; |
| 4 | +import javafx.collections.ObservableList; |
| 5 | +import javafx.geometry.Insets; |
| 6 | +import javafx.scene.Node; |
| 7 | +import javafx.scene.Scene; |
| 8 | +import javafx.scene.control.Label; |
| 9 | +import javafx.scene.input.MouseButton; |
| 10 | +import javafx.scene.layout.Pane; |
| 11 | +import javafx.scene.layout.VBox; |
| 12 | +import javafx.scene.paint.Color; |
| 13 | +import javafx.scene.shape.Circle; |
| 14 | +import javafx.scene.shape.Rectangle; |
| 15 | +import javafx.stage.Stage; |
| 16 | + |
| 17 | +import java.util.ArrayList; |
| 18 | + |
| 19 | +/** |
| 20 | + * **15.17 (Geometry: find the bounding rectangle) Write a program that enables the user |
| 21 | + * to add and remove points in a two-dimensional plane dynamically, as shown |
| 22 | + * in Figure 15.29a. A minimum bounding rectangle is updated as the points are |
| 23 | + * added and removed. Assume that the radius of each point is 10 pixels |
| 24 | + */ |
| 25 | +public class Exercise15_17 extends javafx.application.Application { |
| 26 | + |
| 27 | + Pane pane = new Pane(); |
| 28 | + ArrayList<Circle> points = new ArrayList<>(); |
| 29 | + static final int RADIUS = 10; |
| 30 | + static final Color FILL_COLOR = Color.TRANSPARENT; |
| 31 | + static final Color LINE_COLOR = Color.BLACK; |
| 32 | + |
| 33 | + static final Label instructionsHeader = new Label("INSTRUCTION"); |
| 34 | + static final Label addInstruction = new Label("Add: Left Click"); |
| 35 | + static final Label removeInstruction = new Label("Remove: Right Click"); |
| 36 | + |
| 37 | + public static void main(String[] args) { |
| 38 | + Application.launch(args); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void start(Stage primaryStage) { |
| 43 | + double width = 600; |
| 44 | + double height = 400; |
| 45 | + VBox instructionsBox = new VBox(); |
| 46 | + instructionsBox.setPadding(new Insets(5, 5, 5, 5)); |
| 47 | + instructionsBox.setStyle("-fx-border-color: black"); |
| 48 | + |
| 49 | + |
| 50 | + instructionsBox.getChildren().addAll(instructionsHeader, addInstruction, removeInstruction); |
| 51 | + Pane infoPane = new Pane(instructionsBox); |
| 52 | + infoPane.setPadding(new Insets(10, 10, 10, 10)); |
| 53 | + |
| 54 | + pane.getChildren().addAll(infoPane); |
| 55 | + instructionsBox.setLayoutX(10); |
| 56 | + instructionsBox.setLayoutY(10); |
| 57 | + |
| 58 | + pane.setOnMouseClicked(e -> { |
| 59 | + double x = e.getX(); |
| 60 | + double y = e.getY(); |
| 61 | + if (infoPane.contains(x, y)) return; |
| 62 | + |
| 63 | + if (e.getButton() == MouseButton.PRIMARY) { |
| 64 | + Circle c = drawPoint(x, y); |
| 65 | + points.add(c); |
| 66 | + pane.getChildren().add(c); |
| 67 | + drawMinBoundingRec(); |
| 68 | + |
| 69 | + } else if (e.getButton() == MouseButton.SECONDARY) { |
| 70 | + removePoint(x, y); |
| 71 | + drawMinBoundingRec(); |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + }); |
| 76 | + |
| 77 | + Scene scene = new Scene(pane, width, height); |
| 78 | + primaryStage.setScene(scene); |
| 79 | + primaryStage.setTitle("click to draw circle"); |
| 80 | + primaryStage.show(); |
| 81 | + } |
| 82 | + |
| 83 | + private Circle drawPoint(double x, double y) { |
| 84 | + Circle c = new Circle(x, y, RADIUS, FILL_COLOR); |
| 85 | + c.setStroke(LINE_COLOR); |
| 86 | + return c; |
| 87 | + } |
| 88 | + |
| 89 | + private void drawMinBoundingRec() { |
| 90 | + clearCurrentRectangle(); |
| 91 | + |
| 92 | + if (points.isEmpty()) return; |
| 93 | + |
| 94 | + Circle top = points.get(0); |
| 95 | + Circle bottom = points.get(0); |
| 96 | + Circle right = points.get(0); |
| 97 | + Circle left = points.get(0); |
| 98 | + |
| 99 | + for (Circle c : points) { |
| 100 | + if (c.getCenterX() < left.getCenterX()) left = c; |
| 101 | + if (c.getCenterX() > right.getCenterX()) right = c; |
| 102 | + if (c.getCenterY() > bottom.getCenterY()) bottom = c; |
| 103 | + if (c.getCenterY() < top.getCenterY()) top = c; |
| 104 | + } |
| 105 | + double width = right.getCenterX() - left.getCenterX() + top.getRadius() * 2; |
| 106 | + double height = bottom.getCenterY() - top.getCenterY() + top.getRadius() * 2; |
| 107 | + double centerX = (right.getCenterX() + left.getCenterX()) / 2; |
| 108 | + double centerY = (top.getCenterY() + bottom.getCenterY()) / 2; |
| 109 | + |
| 110 | + Rectangle rec = calculateRectangle(centerX, centerY, width, height); |
| 111 | + pane.getChildren().add(rec); |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + Rectangle calculateRectangle(double centerX, double centerY, double width, double height) { |
| 116 | + Rectangle rec = new Rectangle(centerX - width / 2, centerY - height / 2, width, height); |
| 117 | + rec.setStroke(LINE_COLOR); |
| 118 | + rec.setFill(FILL_COLOR); |
| 119 | + return rec; |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + private void removePoint(double x, double y) { |
| 124 | + ObservableList<Node> list = pane.getChildren(); |
| 125 | + for (int i = list.size() - 1; i >= 0; i--) { |
| 126 | + Node c = list.get(i); |
| 127 | + |
| 128 | + if (c instanceof Circle && c.contains(x, y)) { |
| 129 | + pane.getChildren().remove(c); |
| 130 | + points.remove(c); |
| 131 | + |
| 132 | + break; |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private void clearCurrentRectangle() { |
| 138 | + ObservableList<Node> list = pane.getChildren(); |
| 139 | + |
| 140 | + for (Node c : list) { |
| 141 | + if (c instanceof Rectangle) { |
| 142 | + pane.getChildren().remove(c); |
| 143 | + |
| 144 | + break; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + } |
| 149 | +} |
0 commit comments