Q:

(Racing cars) Write a program that simulates four cars racing, as shown in Figure 16.47b. You can set the speed for each car, with maximum 100

0

(Racing cars) Write a program that simulates four cars racing, as shown in Figure 16.47b. You can set the speed for each car, with maximum 100.

(b) You can set the speed for each car.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import javafx.util.Duration;

public class CarPane extends Pane {
	private double x;
	private double y = 40;
	private double radius = 5;
	private Rectangle rectangle;
	private Polygon polygon;
	private Circle circle1;
	private Circle circle2;
	private Timeline animation;

	/** Construct and animate a default CarPane */
	CarPane() {
		drawCar();
		animation = new Timeline(
			new KeyFrame(Duration.millis(50), e -> moveCar()));
		animation.setCycleCount(Timeline.INDEFINITE);
	}

	/** Create a car an place it in the pane */
	private void drawCar() {
		getChildren().clear();
		rectangle = new Rectangle(x, y - 20, 50, 10);
		polygon = new Polygon(x + 10, y - 20, x + 20, y - 30, x + 30, 
			y - 30, x + 40, y - 20);
		circle1 = new Circle(x + 15, y - 5, radius);
		circle2 = new Circle(x + 35, y - 5, radius);
		getChildren().addAll(rectangle, circle1, circle2, polygon);
	}

	/** Set y to specified value */
	public void setY(double y) {
		this.y = y;
	}

	/** Set x to specified value */
	public void setX(double x) {
		this.x = x;
	}

	/** return x */
	public double getX() {
		return x;
	}

	/** return y */
	public double getY() {
		return y;
	}

	/** Pause animation */
	public void pause() {
		animation.pause();
	}

	/** Play animation */
	public void play() {
		animation.play();
	}

	/** Set the animation rate to a specified amount */
	public void setSpeed(double speed) {
		if (speed <= 100)
			animation.setRate(speed);
	}

	/** Increase rate by 1 */
	public void increaseSpeed() {
		animation.setRate(animation.getRate() + 1);
	}

	/** decrease rate by 1 */
	public void decreaseSpeed() {
		animation.setRate(animation.getRate() > 0 ? animation.getRate() - 1 : 0);
	}

	/** Redraw car with new x value */
	protected void moveCar() {
		if (x <= getWidth()) {
			x += 1;	
		} 
		else
			x = 0;
		drawCar();
	}
}

Exercise_16_25.java 

/*********************************************************************************
* (Racing cars) Write a program that simulates four cars racing, as shown in     *
* Figure 16.47b. You can set the speed for each car, with maximum 100.           *
*********************************************************************************/
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.geometry.Pos;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.input.KeyCode;

public class Exercise_16_25 extends Application {
	protected HBox paneForSpeed = new HBox(5);
	protected VBox paneForCars = new VBox(5);
	protected ObservableList<Node> textFields = paneForSpeed.getChildren();
	protected ObservableList<Node> cars = paneForCars.getChildren();

	@Override // Override the start method in the Application class
	public void start(Stage primaryStage) {
		final int NUMBER_OF_CARS = 4;
		final int PREF_COLUMN_COUNT = 2;

		paneForSpeed.setAlignment(Pos.CENTER);

		// Place labels and text fields in a pane
		for (int i = 0; i < NUMBER_OF_CARS; i++) {
			paneForSpeed.getChildren().addAll(
				new Label("Car " + (i + 1) + ":"), new TextField());
		}

		// Set specified properties for each text field in a list
		for (int i = 1; i < textFields.size(); i+= 2) {
			((TextField)textFields.get(i)).setPrefColumnCount(PREF_COLUMN_COUNT);
		}

		// Place nodes in a pane for cars
		for (int i = 0; i < NUMBER_OF_CARS; i++) {
			paneForCars.getChildren().add(new CarPane());
		}

		// Set specified properties for each element in a list
		for (Node car: cars) {
			((CarPane)car).setStyle("-fx-border-color: black");
			((CarPane)car).setY(40);
		}

		// Create and register handlers
		for (int i = 1; i < textFields.size(); i += 2) {
			((TextField)textFields.get(i)).setOnKeyPressed(e -> {
				if (e.getCode() == KeyCode.ENTER) {
					setSpeed();
				}
			});

		}
		
		// Create a border pane
		BorderPane pane = new BorderPane();
		pane.setTop(paneForSpeed);
		pane.setCenter(paneForCars);

		// Create a scene and place it in the stage
		Scene scene = new Scene(pane, 400, 200);
		primaryStage.setTitle("Exercise_16_25"); // Set the stage title
		primaryStage.setScene(scene); // Place the scene in the stage
		primaryStage.show(); // Display the stage
	}

	/** Set specified rate for a CarPane list */
	private void setSpeed() {
		for (int i = 1, j = 0; i < textFields.size(); i += 2, j++) {
			if (((TextField)textFields.get(i)).getText().length() > 0) {
				((CarPane)cars.get(j)).setSpeed(
					Double.parseDouble(((TextField)textFields.get(i)).getText()));
				((CarPane)cars.get(j)).play();
			}
			else {
				((CarPane)cars.get(j)).pause();
			}
		}
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now