Question
Write a java code for below instruction: For this programming assignment, you'll create a JavaFX application that displays and controls Vehicle objects created by Vehicle
Write a java code for below instruction:
For this programming assignment, you'll create a JavaFX application that displays and controls Vehicle objects created by Vehicle class(given program Vehicle.java). The diagram below shows what your GUI should look like. Your GUI should allow the user to create up to five vheicles. As each vehicle is created, it is represented as a colored circle in the central drawing pane and as a label in the left area of the GUI (with the same background color as the circle). In the figure below, the user has just used the Create Vehicle button to create the first vehicle at location (100, -120) with velocity 50 and direction 90. The first vehicle color is red, so the vehicle is represented by a red circle in the central drawing pane and via the red label on the left.
Details of the GUI's operation: Your application must obey the following rules:
Your class for the GUI must be called VehicleGUI and it must be part of the vehicle package that contains the Vehicle class.
There can be only five vehicles created by your GUI. If the user tries to create a sixth vehicle; your GUI will simple ignore the request.
Each of the vehicles created must have a unique color chosen from the following: RED, GREEN, BLUE, PURPLE, PINK.
The central drawing pane should be 300 by 300 and surrounded by a black border
When the Create Vehicle button is press, your GUI should attempt to create a vehicle. In order to succesfully create a vehicle, the following must hold: 1) there must be less than five vehicles, the four text fields at the top of the GUI (X Coordinate, Y Coordinate, Velocity, and Direction) plus the Vehicle Number field must be filled with numeric values and the Y Coordinate value must be zero or less. If any of these conditions do not hold, simply ignore the request. When creating the vehicle, negate the Y Coordinate value, which will produce a Y Coordinate value suitable for JavaFX. After the vehicle is created, draw the circle with one of the remaining colors and create the label for the vehicle.
When the Delete Vehicle button is pressed, your GUI should attempt to delete the vehicle identified by the number in the Vehicle Number text field. If the Vehicle Number text field is empty or does not hold a number, simply ignore the request. Remove the circle and label for this vehicle and return its color to the available colors.
When the Accelerate button is pressed, your GUI should attempt to change the velocity of the vehicle identified by the Vehicle Number field, using the values in the Accelerate and Time text fields. If any of these three text fields do not contain numeric values, simply ignore the request. Note that this will not have an effect on the GUI.
When the Change Direction button is pressed, your GUI should attempt to change the direction of the vehicle identified by the Vehicle Number field, using the value in the Direction text field. If either of these text fields do not contain numeric values, simply ignore the request. Note that this will not have an effect on the GUI.
When the Move Vehicle is pressed, your GUI should attempt to move the vehicle identified by the Vehicle Number field, using the value in the Time field as the amount of time the vehicle travels. If either of the text fields do not contain numeric values, simply ignore the request. If either of the new X, Y values is outside the drawing area (the values are negative or greater than 300, ignore the request. If the request is successful, change the vehicle's coordinates and also the representing circle's coordinates.
When the Quit button is pressed, terminate the application. You can use System.exit() to do this.
[Can anyone help me to get answer for this java program]
Vehicle.java file
package vehicle; public class Vehicle { // should be private //make the given variables private private double velocity; private double x; private double y; private double direction;
//class constructor public Vehicle(double vehObj, double x, double y, double d) {
// d is given as degrees, you should store it internally as radians //set the values this.velocity = vehObj; this.x = x; this.y = y;
//set the radian of direction this.direction = d * ( 3.14 / 180.0 );
}
//The method to get velocity public double getVelocity() { return this.velocity; }
//The method to get position public double getDirection() {
//return in degree return this.direction / ( 3.14 / 180.0 ); }
public double [] getPosition() { // return an array of doubles as the x, y pair double ar[] = {this.x, this.y}; return ar; }
public void changeVelocity(double accelartion, int time) { // acceleration can be positive or negative // velocity is changed based on duration of acceleration // vehObj = vo + at; this.velocity = this.velocity + accelartion * time; } public void changeDirection(double nd) { // change the direction // the parameter nd is in degrees // store the result in radians direction = Math.toRadians(nd); }
public double [] showNewPosition(int t) { // return the new x,y pair based on travelling for time t // based on current velocity, direction and position // does not change the vehicle's position
double dist = this.velocity * t; double xVal = dist * Math.cos(this.direction); double yVal = dist * Math.sin(this.direction); double arryNew[] = {Math.round(xVal), Math.round(yVal)}; return arryNew; }
public void changePosition(int t) { // changes the x,y pair based on travelling for time t // based on current velocity, direction and position.
//update the position double dist = this.velocity * t; x = Math.round(dist * Math.cos(this.direction)); y = Math.round(dist * Math.sin(this.direction)); } public String toString() {
// override the Object toString method to produce a //formatted string String newStr=""; // representation of a vehicle: newStr=""; // "
// overloading of toString that takes an array of two doubles // and returns a position string: "(x,y)" String cc=""; cc=cc+"("+p[0]+","+p[1]+")"; return cc; } private double rndTo2DP(double d) { // rounds d to have two decimal places return Math.round(100*d)/100.0; }
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Here is the solution for this program but I am getting an error. Can anybody help me to run this program without error?
package vehicle
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.shape.Rectangle; import javafx.scene.paint.Color; import javafx.scene.paint.Paint; import javafx.stage.Stage; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.event.ActionEvent; import javafx.event.EventHandler;
// import javafx.application.Application; // import javafx.beans.binding.Bindings; import javafx.geometry.Insets; // import javafx.geometry.Pos; // import javafx.scene.Scene; // import javafx.scene.control.ToggleButton; import javafx.scene.layout.Background; import javafx.scene.layout.BackgroundFill; // import javafx.scene.layout.BorderPane; import javafx.scene.layout.CornerRadii; // import javafx.scene.layout.HBox; // import javafx.scene.layout.VBox; // import javafx.scene.paint.Color; // import javafx.stage.Stage;
public class VehicleGUI extends Application {
int numVehicles = 0; Color vehicleColor[] = {Color.RED, Color.GREEN, Color.BLUE, Color.PURPLE, Color.PINK}; int VEHICLE_RADIUS = 20; int vehicleNumberArr[] = {0,0,0,0,0}; Vehicle vehicles[] = {null, null, null, null, null};
Label labelXCoord, labelYCoord, labelVelocity, labelDirection; TextField xCoord, yCoord, velocity, direction; HBox hbox1;
public HBox getHboxTop(){ labelXCoord = new Label("X Coordinate"); xCoord = new TextField(); labelYCoord = new Label("Y Coordinate"); yCoord = new TextField(); labelVelocity = new Label("Velocity"); velocity = new TextField(); labelDirection = new Label("Direction"); direction = new TextField(); return new HBox(labelXCoord,xCoord,labelYCoord,yCoord,labelVelocity,velocity,labelDirection,direction); }
Label labelV1, labelV2, labelV3, labelV4, labelV5; VBox vboxleft; Canvas canvas; GraphicsContext gc; Label labelAcceleration, labelTime, labelDirectionChange, labelVehicleNumber; TextField acceleration, time, directionChange, vehicleNumber; VBox vboxright; HBox hboxMid; Label labelV[] = {new Label(""),new Label(""),new Label(""),new Label(""),new Label("")};
public HBox getHboxMid(){ vboxleft = new VBox(labelV[0],labelV[1],labelV[2],labelV[3],labelV[4]); vboxleft.setPrefWidth(300);
canvas = new Canvas(300, 300); gc = canvas.getGraphicsContext2D(); gc.setFill(Color.TRANSPARENT); gc.setStroke(Color.BLACK); gc.setLineWidth(5); gc.strokeRect(0, 0, canvas.getWidth(), canvas.getHeight());
// drawShapes(gc); labelAcceleration = new Label("Acceleration"); acceleration = new TextField(); labelTime = new Label("Time"); time = new TextField(); labelDirectionChange = new Label("DirectionChange"); directionChange = new TextField(); labelVehicleNumber = new Label("VehicleNumber"); vehicleNumber = new TextField(); vboxright = new VBox(labelAcceleration,acceleration,labelTime,time,labelDirectionChange,directionChange,labelVehicleNumber,vehicleNumber);
return new HBox(vboxleft,canvas,vboxright); }
Button button1, button2, button3, button4, button5, button6; HBox hbox2;
public HBox getHboxBottom(){ button1 = new Button("Create Vehicle"); button1.setOnAction(new EventHandler
public void createVehicle(){ System.out.println("Create Vehicle"); if(numVehicles=0){ int i = numVehicles; Color c = vehicleColor[i]; gc.setFill(c); vehicles[i] = new Vehicle(vobj,x,y,d); vehicleNumberArr[i] = vn; gc.fillOval(x, y, VEHICLE_RADIUS, VEHICLE_RADIUS); labelV[i].setText("Vehicle " + vn); labelV[i].setBackground(new Background(new BackgroundFill(c, CornerRadii.EMPTY, Insets.EMPTY))); numVehicles++; } } }
public void deleteVehicle(){ System.out.println("Delete Vehicle"); }
public void accelVehicle(){ System.out.println("Accelerate Vehicle"); }
public void changeDirectionVehicle(){ System.out.println("Change Direction of Vehicle"); }
public void moveVehicle(){ System.out.println("Move Vehicle"); }
VBox vbox; @Override public void start(Stage primaryStage) throws Exception { primaryStage.setTitle("HBox Experiment 1");
hbox1 = getHboxTop(); hboxMid = getHboxMid(); hbox2 = getHboxBottom();
vbox = new VBox(hbox1,hboxMid,hbox2);
Scene scene = new Scene(vbox); primaryStage.setScene(scene); primaryStage.show(); }
public static void main(String[] args) { Application.launch(args); } }
Vehicle Display rection 90 Y Coordinate -120 Velocity 50 x Coordinate 100 Acceleration ime Direction Change Vehicle Number Create Vehicle Delete Vehicle Accelerate Change Direction Move Vehicle QuitStep 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