Question
Part 1: Finishing the Car class The shapes for drawing a Car object are stored as non-static private data fields of class Car. We must
Part 1: Finishing the Car class
The shapes for drawing a Car object are stored as non-static private data fields of class Car. We must update the x and y positions of each of these shapes whenever a cars position is changed. This is to be done in method updateChildren() (your task to write). All other properties of these shapes (such as their color, radius, width, and height) are to be initialized in constructor Car(double xPos, double yPos, Color theColor) (your task to do).
Your task: initialize/update the properties of the shapes for drawing a car. Use the relative coordinates in the following image (see Figure 2):
The x and y coordinate of the vehicle can be retrieved with getX() and getY() respectively.
The color of the car must be set to the vehicles color. We can get the vehicles color by calling getColor().
Set the color of the wheels to Color.BLACK.
The width and height of a rectangle r can be set to w and h respectively with:
r.setWidth(w);
r.setHeight(h);
The coordinates of a rectangle r can be set to position (x,y) with:
r.setX(x);
r.setY(y);
The center of a circle c can be set to position (x,y) with:
c.setCenterX(x);
c.setCenterY(y);
The radius of a circle c can be set to r with:
c.setRadius(r);
We can reposition a Polygon p with:
ObservableList
points.clear(); // remove all points from p
points.addAll(x1, y1, x2, y2, x3, y3);
Here (x1, y1), (x2, y2), and (x3, y3) are the new points to position p to (add more points as needed).
When a car is being drawn, its shapes are not drawn unless they are added to its list of children. To accomplish this, at the end of constructor Car(double xPos, double yPos, Color theColor) add the line
getChildren().addAll(roof, body, backWheel, frontWheel);
Part 2: Finishing the Truck class
Finish writing the Truck class so that it draws a truck when being displayed. This is similar to the Car class. Remember, all drawing must be done relative to the x and y coordinate of the truck.
The color of the truck must be set to the vehicles color. Set the color of the wheels to Color.BLACK.
Use the relative coordinates in the following image to draw the truck (see Figure 3):
Do not add anymore members to the public interface of class Truck. You may, however, add any needed private members. Note: overloading an inherited public method does not add it to the public interface because it is already a part of the public interface.
Part 3: Creating the Buttons
In the start method of class Hw3 add the following buttons:
1. A button with the text Add Truck on it for adding a new truck. When this button is clicked, call addTruck(). Add this button to topBox.
2. A button with the text Move on it. When this button is clicked, call move(). Add this button to topBox. Note: the move button will not do anything yet since the move() method is not yet completed.
3. A button with the text Clear on it. When this button is clicked, remove all children nodes from drawPane, then remove all elements from the ArrayList vehicles. This can be accomplished with the following two lines of code:
drawPane.getChildren().clear();
vehicles.clear();
Multiple statements can be added to a lambda expression by doing, for example:
e -> {
// add multiple statements here
}
Finally, add the clear button totopBox.
Part 4: Writing the functionality for the Move button
The move() method of class Hw3 is called whenever the Move button is clicked. Its task is to move each vehicle on the screen to a new random position. The vehicles in our program are stored in the ArrayList vehicles. Your task is to finish the move() method to do the following: for each Vehicle v in the ArrayList vehicles, move v to a random position on the screen by calling:
randomlyPosition(v);
Note: In method randomlyPosition(Vehicle v) the call
v.setPosition(
generator.nextDouble()*MAX_X_POS,
generator.nextDouble()*MAX_Y_POS);
is polymorphic. Which implementation of method setPosition to call (Cars version or Trucks version) is determined at runtime based on the data type of v.
package hw; import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.Pane; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.paint.Color; import javafx.geometry.Insets; import java.util.Random; import java.util.ArrayList; public class Hw3 extends Application { // window width and height private static final double WIDTH = 610; private static final double HEIGHT = 377; // maximum x and y position to place vehicles private static final double MAX_X_POS = WIDTH - 80; private static final double MAX_Y_POS = HEIGHT - 90; // spacing between and around elements private static final double SPACING = 10.0; private ArrayListvehicles; private Pane drawPane; private Random generator; @Override public void start(Stage stage) { vehicles = new ArrayList(); drawPane = new Pane(); generator = new Random(); Insets padding = new Insets(SPACING); HBox topBox = new HBox(SPACING); topBox.setPadding(padding); Button addCarBtn = new Button("Add Car"); addCarBtn.setOnAction(e -> addCar()); topBox.getChildren().add(addCarBtn); // TODO: Create a button for adding Trucks // similar to the one above for adding Cars. // When the button is clicked, call the addTruck() method. // Add this button to topBox. // TODO: Create a button with the text "Move" on it. When // this button is clicked, call the move() method to // rearrang the position of the vehicles. // Add this button to topBox. // TODO: Create a button with the text "Clear" on it. When // this button is clicked, remove all children nodes from // drawPane, then remove all elements from the ArrayList vehicles. // To remove all children from drawPane, execute // drawPane.getChildren().clear(); // Removing all elements from an ArrayList can be done by calling // its clear() method. // More than one statement can be added to a lambda expression // by doing: // e -> { // statement1; // statement2; // // etc. // } // Add this button to topBox. BorderPane root = new BorderPane(); root.setTop(topBox); root.setCenter(drawPane); // Create a scene and place it in the stage Scene scene = new Scene(root, WIDTH, HEIGHT); stage.setTitle("Hw3"); // Set the stage title stage.setScene(scene); // Place the scene in the stage stage.setResizable(false); // Prevent window from being resized stage.show(); // Display the stage } private void addCar() { // create a new car with a random color Car car = new Car(randomColor()); // randomly position car randomlyPosition(car); vehicles.add(car); // add car to the vehicles ArrayList drawPane.getChildren().add(car); // add car to drawPane } private void addTruck() { // create a new truck with a random number of wheels Truck truck = new Truck(randomColor()); // randomly position truck randomlyPosition(truck); vehicles.add(truck); // add truck to the vehicles ArrayList drawPane.getChildren().add(truck); // add truck to drawPane } // This method moves each vehicle to a random position on the screen private void move() { // TODO: For each vehicle v in the ArrayList vehicles, // move it to a random position by calling: // randomlyPosition(v); } // This method moves a vehicle to a random position private void randomlyPosition(Vehicle v) { v.setPosition( generator.nextDouble()*MAX_X_POS, generator.nextDouble()*MAX_Y_POS); } // This method returns a random color private Color randomColor() { return new Color(generator.nextDouble(), // red component generator.nextDouble(), // green component generator.nextDouble(), // blue component 1.0); // opacity } // The main method is only needed for IDEs with limited JavaFX support public static void main(String[] args) { Application.launch(args); } }
package hw; import javafx.scene.paint.Color; import javafx.scene.shape.*; import javafx.collections.ObservableList; public class Car extends Vehicle { // The children nodes private Polygon roof = new Polygon(); private Rectangle body = new Rectangle(); private Circle backWheel = new Circle(); private Circle frontWheel = new Circle(); public Car(double xPos, double yPos, Color theColor) { super(xPos, yPos, theColor); // TODO: Initialize properties of the children nodes that // are not set in updateChildren(). updateChildren() // only updates the x and y coordinates of each children. updateChildren(); // update position of children // TODO: add children nodes here } public Car(Color theColor) { this(0.0, 0.0, theColor); } @Override public void setPosition(double x, double y) { super.setPosition(x, y); updateChildren(); } private void updateChildren() { // TODO: Update the x and y coordinates of each children node. // The x and y coordinate of this object can be // retrieved with getX() and getY() respectively. } }
package hw; import javafx.scene.paint.Color; import javafx.scene.shape.*; import javafx.collections.ObservableList; // TODO: Finish writing the Truck class. // This is similar to the Car class. // Do not add anymore members to its public interface. // You may, however, add any needed private members. // Note: overloading an inherited public method does not // add it to the public interface because it is already // a part of the public interface. public class Truck extends Vehicle { public Truck(double xPos, double yPos, Color theColor) { super(xPos, yPos, theColor); } public Truck(Color theColor) { this(0.0, 0.0, theColor); } }(12,0) (30,0) (48,11) (0,11)16.12) (35,12) (0,25) (48,25) (36,27) (12,27) Wheel Radius = 6 FIGURE 2. Relative Car Coordinates (42,0) (60,0) (78,12) (66,12) (0,24) (42.24) (0,35) (78,35) (12,36) (66,36) (30,36) Wheel Radius = 9 (12,0) (30,0) (48,11) (0,11)16.12) (35,12) (0,25) (48,25) (36,27) (12,27) Wheel Radius = 6 FIGURE 2. Relative Car Coordinates (42,0) (60,0) (78,12) (66,12) (0,24) (42.24) (0,35) (78,35) (12,36) (66,36) (30,36) Wheel Radius = 9
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started