The Practice and Philosophy of Object-Oriented Programming in Java

New Practice Problems for Chapter 10

SpinLines.  Create a custom class called SpinLine that extends Line (in javafx.scene.shape). 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 method called set that sets the stroke color and stroke width to given values, and a method called spin that performs a rotation. You can write the spin method with two lines of code: first, get the current angle of rotation by calling the appropriate superclass method; second, add the rotation increment to the current angle, and then pass that value to the superclass method for rotating the shape.

Now write a JavaFX application the displays five spin lines. The scene is initially empty with a LAVENDER background, but after the user clicks the mouse twice a new spin line is created with endpoints at the positions of the two mouse clicks. It has a randomly generated dark color, a stroke width of 8, and a randomly generated rotation increment in the range [-45, 45]. This process repeats until five lines have been created. After five lines have been created, subsequent mouse clicks cause each spin line to be rotated by its rotation increment.

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

       

The following screen shots show the scene after additional mouse clicks (which cause each spin line to rotate by its rotation increment).

       

Ultra Circles  Create a class called UltraCircle that extends 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. The public interface for the class consists of the following:

  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 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. This can be used in a JavaFX application to find out how far the circle is from the center of the scene.

Now create a JavaFX application to test your UltraCircle class. Set the size of the scene to size=500 and the background to AliceBlue. When the user clicks the mouse in the root node (a Pane), a circle centered at that location is created. It will have a radius of 30, a randomly generated fill color, and randomly generated translation increments between -50 and 50 inclusive. Set the stroke width to 5 and the stroke color to a darker version of the fill color (invoke the inherited color method darker for this).

Once the scene contains 8 of circles, no new circles are created. Instead, a mouse click causes each circle to be translated. If the distance of a circle to the center of the scene reaches size/2 then its direction is reversed. In the following screenshots, the mouse cursor is currently in the circle whose fill color is visible.


corners

corners

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


corners