Skip to content

Commit f5cc5a0

Browse files
author
jsquared21
committed
Add Ex 18.20
1 parent b642be8 commit f5cc5a0

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

Diff for: Exercise_18/Exercise_18_20/Exercise_18_20.class

1.34 KB
Binary file not shown.

Diff for: Exercise_18/Exercise_18_20/Exercise_18_20.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*********************************************************************************
2+
* (Display circles) Write a Java program that displays ovals, as shown in Figure *
3+
* 18.12b. The circles are centered in the pane. The gap between two adjacent *
4+
* circles is 10 pixels, and the gap between the border of the pane and the *
5+
* largest circle is also 10. *
6+
*********************************************************************************/
7+
import javafx.application.Application;
8+
import javafx.stage.Stage;
9+
import javafx.scene.Scene;
10+
import javafx.scene.layout.StackPane;
11+
import javafx.geometry.Insets;
12+
import javafx.scene.shape.Circle;
13+
import javafx.scene.paint.Color;
14+
15+
public class Exercise_18_20 extends Application {
16+
protected static StackPane pane = new StackPane();
17+
18+
@Override // Override the start method in the Application class
19+
public void start(Stage primaryStage) {
20+
int numberOfCircles = 16; // Number of ovals
21+
addCircles(numberOfCircles);
22+
pane.setPadding(new Insets(10, 10, 10, 10));
23+
24+
// Create a scene and place it in the stage
25+
Scene scene = new Scene(pane);
26+
primaryStage.setTitle("Exercise_18_20"); // Set the stage title
27+
primaryStage.setScene(scene); // Place the scene in the stage
28+
primaryStage.show(); // Display the stage
29+
}
30+
31+
/** Method adds n circles to pane recursively */
32+
public static void addCircles(int n) {
33+
if (n > 0) {
34+
Circle circle = new Circle(10 * n);
35+
circle.setFill(Color.WHITE);
36+
circle.setStroke(Color.BLACK);
37+
pane.getChildren().add(circle);
38+
addCircles(n - 1); // Recursive call
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)