Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ch_09/exercise09_06/StopWatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ public StopWatch() {
/**
* ■ A method named start() that resets the startTime to the current time.
*/
void start() {
public void start() {
startTime = System.currentTimeMillis();
}

/**
* ■ A method named stop() that sets the endTime to the current time.
*/
void stop() {
public void stop() {
endTime = System.currentTimeMillis();
}

Expand Down
149 changes: 149 additions & 0 deletions ch_15/Exercise15_17.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package ch_15;

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

import java.util.ArrayList;

/**
* **15.17 (Geometry: find the bounding rectangle) Write a program that enables the user
* to add and remove points in a two-dimensional plane dynamically, as shown
* in Figure 15.29a. A minimum bounding rectangle is updated as the points are
* added and removed. Assume that the radius of each point is 10 pixels
*/
public class Exercise15_17 extends javafx.application.Application {

Pane pane = new Pane();
ArrayList<Circle> points = new ArrayList<>();
static final int RADIUS = 10;
static final Color FILL_COLOR = Color.TRANSPARENT;
static final Color LINE_COLOR = Color.BLACK;

static final Label instructionsHeader = new Label("INSTRUCTION");
static final Label addInstruction = new Label("Add: Left Click");
static final Label removeInstruction = new Label("Remove: Right Click");

public static void main(String[] args) {
Application.launch(args);
}

@Override
public void start(Stage primaryStage) {
double width = 600;
double height = 400;
VBox instructionsBox = new VBox();
instructionsBox.setPadding(new Insets(5, 5, 5, 5));
instructionsBox.setStyle("-fx-border-color: black");


instructionsBox.getChildren().addAll(instructionsHeader, addInstruction, removeInstruction);
Pane infoPane = new Pane(instructionsBox);
infoPane.setPadding(new Insets(10, 10, 10, 10));

pane.getChildren().addAll(infoPane);
instructionsBox.setLayoutX(10);
instructionsBox.setLayoutY(10);

pane.setOnMouseClicked(e -> {
double x = e.getX();
double y = e.getY();
if (infoPane.contains(x, y)) return;

if (e.getButton() == MouseButton.PRIMARY) {
Circle c = drawPoint(x, y);
points.add(c);
pane.getChildren().add(c);
drawMinBoundingRec();

} else if (e.getButton() == MouseButton.SECONDARY) {
removePoint(x, y);
drawMinBoundingRec();

}

});

Scene scene = new Scene(pane, width, height);
primaryStage.setScene(scene);
primaryStage.setTitle("click to draw circle");
primaryStage.show();
}

private Circle drawPoint(double x, double y) {
Circle c = new Circle(x, y, RADIUS, FILL_COLOR);
c.setStroke(LINE_COLOR);
return c;
}

private void drawMinBoundingRec() {
clearCurrentRectangle();

if (points.isEmpty()) return;

Circle top = points.get(0);
Circle bottom = points.get(0);
Circle right = points.get(0);
Circle left = points.get(0);

for (Circle c : points) {
if (c.getCenterX() < left.getCenterX()) left = c;
if (c.getCenterX() > right.getCenterX()) right = c;
if (c.getCenterY() > bottom.getCenterY()) bottom = c;
if (c.getCenterY() < top.getCenterY()) top = c;
}
double width = right.getCenterX() - left.getCenterX() + top.getRadius() * 2;
double height = bottom.getCenterY() - top.getCenterY() + top.getRadius() * 2;
double centerX = (right.getCenterX() + left.getCenterX()) / 2;
double centerY = (top.getCenterY() + bottom.getCenterY()) / 2;

Rectangle rec = calculateRectangle(centerX, centerY, width, height);
pane.getChildren().add(rec);

}

Rectangle calculateRectangle(double centerX, double centerY, double width, double height) {
Rectangle rec = new Rectangle(centerX - width / 2, centerY - height / 2, width, height);
rec.setStroke(LINE_COLOR);
rec.setFill(FILL_COLOR);
return rec;
}


private void removePoint(double x, double y) {
ObservableList<Node> list = pane.getChildren();
for (int i = list.size() - 1; i >= 0; i--) {
Node c = list.get(i);

if (c instanceof Circle && c.contains(x, y)) {
pane.getChildren().remove(c);
points.remove(c);

break;
}
}
}

private void clearCurrentRectangle() {
ObservableList<Node> list = pane.getChildren();

for (Node c : list) {
if (c instanceof Rectangle) {
pane.getChildren().remove(c);

break;
}
}

}
}
46 changes: 46 additions & 0 deletions ch_15/Exercise15_18.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ch_15;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

/**
* **15.18 (Move a rectangle using mouse) Write a program that displays a rectangle.
* You can point the mouse inside the rectangle and drag (i.e., move with mouse
* pressed) the rectangle wherever the mouse goes. The mouse point becomes the
* center of the rectangle.
*/
public class Exercise15_18 extends javafx.application.Application {
static final double WIDTH = 600;
static final double HEIGHT = 600;
static final Color FILL_COLOR = Color.TRANSPARENT;
static final Color STROKE_COLOR = Color.BLACK;

public static void main(String[] args) {
Application.launch(args);
}

@Override
public void start(Stage primaryStage) {
Rectangle rec = rectangle();
Pane pane = new Pane(rec);
rec.setOnMouseDragged(e -> {
rec.setX(e.getX() - rec.getWidth() / 2);
rec.setY(e.getY() - rec.getHeight() / 2);
});
primaryStage.setScene(new Scene(pane, WIDTH, HEIGHT));
primaryStage.setTitle("Move a rectangle using mouse");
primaryStage.show();
}

Rectangle rectangle() {
Rectangle rec = new Rectangle(WIDTH / 2, HEIGHT / 2, 60, 30);
rec.setFill(FILL_COLOR);
rec.setStroke(STROKE_COLOR);
return rec;
}

}
70 changes: 70 additions & 0 deletions ch_15/Exercise15_19.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package ch_15;

import ch_09.exercise09_06.StopWatch;
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.text.Text;
import javafx.stage.Stage;

import java.util.concurrent.atomic.AtomicInteger;

/**
* **15.19 (Game: eye-hand coordination) Write a program that displays a circle of radius
* 10 pixels filled with a random color at a random location on a pane, as shown
* in Figure 15.29b. When you click the circle, it disappears and a new random color
* circle is displayed at another random location. After twenty circles are
* clicked, display the time spent in the pane, as shown in Figure 15.29c
*/
public class Exercise15_19 extends javafx.application.Application {
private final double WIDTH = 600;
private final double HEIGHT = 600;
static final int MAX_CIRCLE_COUNT = 20;
static final String TITLE_TEXT = "Exercise15_19";

public static void main(String[] args) {
Application.launch(args);
}

@Override
public void start(Stage primaryStage) {
AtomicInteger circleCount = new AtomicInteger();
Circle circle = new Circle(0, 0, 10);
drawCircle(circle);
Pane pane = new Pane(circle);
Text countText = new Text(50, 50, String.valueOf(circleCount));
pane.getChildren().add(countText);
StopWatch stopWatch = new StopWatch();
circle.setOnMouseClicked(e -> {
int currentCount = circleCount.get();
if (currentCount == 0) {
stopWatch.start();
} else if (currentCount < MAX_CIRCLE_COUNT - 1) {
circleCount.getAndIncrement();
countText.setText(String.valueOf(circleCount));
drawCircle(circle);
} else {
stopWatch.stop();
circleCount.getAndIncrement();
countText.setText(String.valueOf(circleCount));
pane.getChildren().remove(circle);
pane.getChildren().add(new Text(WIDTH / 2, HEIGHT / 2, "Time spent is " +
stopWatch.getElapsedTime() + " milliseconds"));
}
});
primaryStage.setScene(new Scene(pane, WIDTH, HEIGHT));
primaryStage.setTitle(TITLE_TEXT);
primaryStage.show();
}

private void drawCircle(Circle circle) {
double min = circle.getRadius() + 5;
double max = WIDTH - circle.getRadius() - 5;
circle.setCenterX((Math.random() * (max - min) + min));
max = WIDTH - circle.getRadius() - 5;
circle.setCenterY((Math.random() * (max - min) + min));
circle.setFill(new Color(Math.random(), Math.random(), Math.random(), 1));
}
}