Is it possible to draw the inner rectangle first, and then draw the rectangles that contain it?
Yes. This is an easy modification to the program.
Is is possible to reduce the scale
each time by multiplying by 0.9
?
Yes. This is an even easier modification to the program. You will get a different picture, though.
There are many other variations on this basic program. To really learn recursion, try some of them and invent your own. See the exercises.
Here is a program that draws a line.
It does this by calling addLine()
,
which adds one line with specified end points and Color
to a Group
.
Recall that class Line
is a subclass of Shape
,
so it inherits all the methods of that class and its grandparent class Node
.
import javafx.application.*; import javafx.stage.*; import javafx.scene.*; import javafx.scene.shape.*; import javafx.scene.paint.*; public class DrawLine extends Application { void addLine( Group root, Color color, double startX, double startY, double endX, double endY ) { Line line = new Line( startX, startY, endX, endY ); line.setStrokeWidth( 3.0 ); line.setStroke( color ); root.getChildren().add( line ); } public void start(Stage stage) { double sceneWidth=400, sceneHeight= 300; double startX = 20.0, startY = 250.0, // left end point endX = 380.0, endY = 50.0; // right end point Group root = new Group( ); addLine( root, Color.DARKSEAGREEN, startX, startY, endX, endY ); Scene scene = new Scene(root, sceneWidth, sceneHeight, Color.BLANCHEDALMOND ); stage.setTitle("One Line"); stage.setScene(scene); stage.show(); } }
Could addLine()
be used to draw two lines?