The Practice and Philosophy of Object-Oriented Programming in Java

New Practice Problems for Chapter 10

SpinLines.  Create a custom class named SpinLine that extends javafx.scene.shape.Line. A spin line is a kind of line with the ability to rotate itself by a fixed number of degrees, which we will call its rotation increment. For purposes of construction, a spin line is specified by the coordinates of its two endpoints and its rotation increment. The endpoints will be passed to the superclass constructor and the rotation increment will be copied to a field of the class.

Provide a set method that sets the stroke color and stroke width to parameter values, and a spin method that applies the rotation increment using the inherited getRotate and setRotate methods.

Write a JavaFX application that displays five spin lines. The scene is initially empty, but after the user clicks the mouse twice a new spin line is created with endpoints at the locations of the two mouse clicks. This process is repeated until there are 5 spin lines. Subsequent mouse clicks anywhere in the scene cause each spin line to rotate. The spin line colors and rotation increments are selected at random, the latter in the range [-45, 45].

The following output screenshots show the scene after each line has been added.


The output screenshots below show the scene after rotations arising from additional mouse clicks.


Ultra Circles.  Create a class called UltraCircle that extends javafx.scene.shape.Circle. This new class represents a colored circle that can be translated in a fixed direction. The direction is determined by two integers specifying horizontal and vertical increments. The fill color appears when the mouse cursor enters the circle and disappears when it exits. Provide the following class members.

  1. A constructor with parameters for the center coordinates, radius, fill color, and horizontal/vertical increments. After invoking the appropriate superclass constructor, the increments are copied to fields of the class and event handlers for mouse enter/exit events are defined. The fill color can removed by passing a null reference to setFill.
  2. A translate method that resets the center of the shape to its current position plus the horizontal and vertical increments.
  3. A reverse method that sets the horizontal and vertical increments to their additive inverses.
  4. A distanceFrom method that returns the distance from the center of the circle to a specified location.

Now write a JavaFX application to test your UltraCircle class. When the user clicks the mouse in the root node, an ultra circle centered at that location is created. It will have a radius of 30, a random fill color, and random translation increments in the range [-50, 50]. Set the stroke width to 5 and the stroke color to a darker version of the fill color.

Once the scene contains 8 ultra circles, no new ones wil be created; instead, subsequent mouse clicks cause each ultra circle to be translated. When the distance of an ultra circle to the center of the viewing area reaches n/2, where n is the the width/height of the scene, its direction is reversed. In the following output screenshots, the mouse cursor is currently in the circle whose fill color is visible.

corners corners

The following output screenshot shows how the scene might look translate is invoked on each circle.


Return to Companion Site home page