Question

Write a JavaFX program to solve the following problem. Two buttons appear at the top side by side. When the Park button is clicked, a small car drives from the right side onto the road and parks in the garage. The garage can hold five cars. When the Unpark button is clicked, the bottom car drops down from the garage, drives left, drives up, and drives right off the screen. A car is designed, minimally as rectangle on two circular wheels. Each car should be a random color. Include your name as stage title. You do not need to do a lot of error checking; assume the user clicks Park five times and then Unpark five times. Your Name Display after parking five cars: Park Unpark Display showing unparking: Your Name Park Unpark

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  • I have added the working code below.
  • Tested as written in question Press Park button 5 times -> Press Unpark button 5 times.
  • In case of any doubts just comment down

--------------------Code--------------------

import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import java.util.List;
import javafx.animation.PathTransition;
import javafx.animation.SequentialTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.control.Button;
import javafx.scene.shape.*;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
* Simple example demonstrating JavaFX animations.
*
* Slightly adapted from Example 2 ("Path Transition") which is provided in
* "Creating Transitions and Timeline Animation in JavaFX"
* (http://docs.oracle.com/javafx/2.0/animations/jfxpub-animations.htm).
*
* @author Dustin
*/
public class JavaFxAnimations extends Application
{
private int WIDTH = 800;
private int HEIGHT = 600;
private int CARWIDTH = 30;
private int CARHEIGHT = 15;
private int WHEELRADIUS = 5;
private int margin = 50;
private int unParkX = WIDTH + margin;
private int unParkY = HEIGHT - margin - CARHEIGHT - 2*WHEELRADIUS;

private Group[] cars = new Group[5];
private Boolean[] isParked = new Boolean[5];
private SequentialTransition[] transitions = new SequentialTransition[5];

/**
* Generate Path upon which animation will occur.
*
* @param pathOpacity The opacity of the path representation.
* @return Generated path.
*/
private Path generateCurvyPath(final double pathOpacity)
{
final Path path = new Path();
path.getElements().add(new MoveTo(20,20));
path.getElements().add(new CubicCurveTo(380, 0, 380, 120, 200, 120));
path.getElements().add(new CubicCurveTo(0, 120, 0, 240, 380, 240));
path.setOpacity(pathOpacity);
return path;
}

private Path generatePath(final double pathOpacity, int carNumber, Boolean onPark)
{
final Path path = new Path();
if(onPark){
path.getElements().add(new MoveTo(unParkX + CARWIDTH/2,unParkY + CARHEIGHT/2 + WHEELRADIUS));
path.getElements().add(new LineTo(WIDTH/2 - CARWIDTH,unParkY + CARHEIGHT/2 + WHEELRADIUS));
int carY = HEIGHT - margin - 2*(6)*CARHEIGHT + CARHEIGHT + CARHEIGHT/2 + WHEELRADIUS + carNumber*2*CARHEIGHT;
path.getElements().add(new LineTo(WIDTH/2 - CARWIDTH,carY));
}else{
int carY = HEIGHT - margin - 2*(6)*CARHEIGHT + CARHEIGHT + CARHEIGHT/2 + WHEELRADIUS + carNumber*2*CARHEIGHT;
path.getElements().add(new MoveTo(WIDTH/2 - CARWIDTH,carY));
path.getElements().add(new LineTo(WIDTH/2 - CARWIDTH,unParkY + CARHEIGHT/2 + WHEELRADIUS));
path.getElements().add(new LineTo(margin + CARWIDTH/2,unParkY + CARHEIGHT/2 + WHEELRADIUS));
path.getElements().add(new LineTo(margin + CARWIDTH/2,2*margin + CARHEIGHT/2 + WHEELRADIUS));
path.getElements().add(new LineTo(unParkX + CARWIDTH/2,2*margin + CARHEIGHT/2 + WHEELRADIUS));
path.getElements().add(new LineTo(unParkX + CARWIDTH/2,unParkY + CARHEIGHT/2 + WHEELRADIUS));
}
path.setOpacity(pathOpacity);
return path;
}

private Path generateBase()
{
final Path path = new Path();
path.getElements().add(new MoveTo(WIDTH - margin,2*margin));
path.getElements().add(new LineTo(margin,2*margin));
path.getElements().add(new LineTo(margin,HEIGHT-margin));
path.getElements().add(new LineTo(WIDTH - margin,HEIGHT-margin));

path.getElements().add(new MoveTo(WIDTH/2,HEIGHT - margin - 2*CARHEIGHT));
path.getElements().add(new LineTo(WIDTH/2,HEIGHT - margin - 2*(6)*CARHEIGHT));
path.getElements().add(new LineTo(WIDTH/2 - 2*CARWIDTH,HEIGHT - margin - 2*(6)*CARHEIGHT));
path.getElements().add(new LineTo(WIDTH/2 - 2*CARWIDTH,HEIGHT - margin - 2*CARHEIGHT));
return path;
}


/**
* Generate the path transition.
*
* @param shape Shape to travel along path.
* @param path Path to be traveled upon.
* @return PathTransition.
*/

// private Shape getCar(){

// }

private PathTransition generatePathTransition(final Group group, final Path path,double duration)
{
final PathTransition pathTransition = new PathTransition();
pathTransition.setDuration(Duration.seconds(duration));
pathTransition.setDelay(Duration.seconds(0.0));
pathTransition.setPath(path);
pathTransition.setNode(group);
return pathTransition;
}

/**
* Determine the path's opacity based on command-line argument if supplied
* or zero by default if no numeric value provided.
*
* @return Opacity to use for path.
*/
private double determinePathOpacity()
{
final Parameters params = getParameters();
final List<String> parameters = params.getRaw();
double pathOpacity = 0.0;
if (!parameters.isEmpty())
{
try
{
pathOpacity = Double.valueOf(parameters.get(0));
}
catch (NumberFormatException nfe)
{
pathOpacity = 0.0;
}
}
return pathOpacity;
}

private Group generateCar(){
final Group car = new Group();
Rectangle r = new Rectangle(unParkX,unParkY,CARWIDTH,CARHEIGHT);
r.setFill(Color.DARKRED);
final Circle wheel1 = new Circle(unParkX + WHEELRADIUS, unParkY + CARHEIGHT + WHEELRADIUS, WHEELRADIUS);
final Circle wheel2 = new Circle(unParkX + CARWIDTH - WHEELRADIUS, unParkY + CARHEIGHT + WHEELRADIUS, WHEELRADIUS);
car.getChildren().add(r);
car.getChildren().add(wheel1);
car.getChildren().add(wheel2);
return car;
}

private void onPark(final Group group){
final Path path = generatePath(determinePathOpacity(),carToPark,true);
group.getChildren().add(path);
final PathTransition transition = generatePathTransition(cars[carToPark], path,3);
transitions[carToPark] = new SequentialTransition(transition);
transitions[carToPark].play();
carToPark++;
}

private void onUnPark(final Group group){
int cartounpark = carToPark - 1;
carToPark--;
final Path path = generatePath(determinePathOpacity(),cartounpark,false);
group.getChildren().add(path);
final PathTransition transition = generatePathTransition(cars[cartounpark], path,3);
transitions[cartounpark] = new SequentialTransition(transition);
transitions[cartounpark].play();
}

private int carToPark = 0;
private void addButtons(final Group group){
Button parkButton = new Button();
Button unparkButton = new Button();
parkButton.setText("Park");
unparkButton.setText("Unpark");
parkButton.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {
onPark(group);
}
});
unparkButton.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {
onUnPark(group);
}
});
VBox vbox = new VBox(5); // 5 is the spacing between elements in the VBox
vbox.getChildren().addAll(parkButton,unparkButton);
group.getChildren().add(vbox);
}

/**
* Start the JavaFX application
*
* @param stage Primary stage.
* @throws Exception Exception thrown during application.
*/
@Override
public void start(final Stage stage) throws Exception
{
final Group rootGroup = new Group();
final Scene scene = new Scene(rootGroup, 600, 400, Color.GHOSTWHITE);
rootGroup.getChildren().add(generateBase());

for (int i=0; i<5; i++) {
cars[i] = generateCar();
rootGroup.getChildren().add(cars[i]);
transitions[i] = new SequentialTransition();
}

stage.setScene(scene);
stage.setTitle("JavaFX 2 Animations");
stage.show();
addButtons(rootGroup);
}

/**
* Main function for running JavaFX application.
*
* @param arguments Command-line arguments; optional first argument is the
* opacity of the path to be displayed (0 effectively renders path
* invisible).
*/
public static void main(final String[] arguments)
{
Application.launch(arguments);
}
}

Add a comment
Know the answer?
Add Answer to:
Write a JavaFX program to solve the following problem. Two buttons appear at the top side...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVAFX PROGRAM Write a program that will allow two users to play a game of tic-tac-toe....

    JAVAFX PROGRAM Write a program that will allow two users to play a game of tic-tac-toe. The game area should consist of nine buttons for the play area, a reset button, and a label that will display the current player's turn. When the program starts, the game area buttons should have no values in them (i.e. they should be blank) When player one clicks on a game area button, the button should get the value X. When player two clicks...

  • Write a JavaFX program to display 5 labels and 3 buttons. You can use any kind...

    Write a JavaFX program to display 5 labels and 3 buttons. You can use any kind of Pane you like to use. Display 3 red circles and 2 blue rectangles (pick your own size). Use the Image and ImageView classes (explained in section 14.9 of your textbook) to show 2 images of your choice. Include your first and last name somewhere on the frame, label, button, etc. We won’t discuss handing events where one would be able to click buttons...

  • In Python Calculator Write a program that allows the user to enter two numbers and then...

    In Python Calculator Write a program that allows the user to enter two numbers and then adds, subtracts, divides or multiplies them when the user clicks on the appropriate button. Make sure you have a Quit button. Modification and requirements”: _____Window title should be  your name Calculate self.main_window.title("?????") _____Labels - change the attributes of the color, font, size and bold the answerExample self.label = tkinter.Label(self.main_window, \ text='?????????!', font=(" should Arial", 12, "bold"), fg="blue", bg="pink")Tip: choose a color from the...

  • Assignment 1. You are to write a simple program using Netbeans Java IDE  that consists of two...

    Assignment 1. You are to write a simple program using Netbeans Java IDE  that consists of two classes. The program will allow the user to place an order for a Tesla Model X car. 2. Present the user with a menu selection for each option on the site. We will use JOptionPane to generate different options for the user. See the examples posted for how to use JOPtionPane. 3. Each menu will have multiple items each item will be in the...

  • Zipcar: “It’s Not About Cars—It’s About Urban Life” Imagine a world in which no one owns...

    Zipcar: “It’s Not About Cars—It’s About Urban Life” Imagine a world in which no one owns a car. Cars would still exist, but rather than owning cars, people would just share them. Sounds crazy, right? But Scott Griffith, CEO of Zipcar, the world’s largest car-share company, paints a picture of just such an imaginary world. And he has nearly 800,000 passionate customers—or Zipsters, as they are called—who will back him up. Zipcar specializes in renting out cars by the hour...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT