Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How to fix my line so that it looks like the one in the photo? import javafx.application.*; import javafx.event.*; import javafx.scene.*; import javafx.scene.image.*; import javafx.scene.control.*;

How to fix my line so that it looks like the one in the photo?

import javafx.application.*;

import javafx.event.*;

import javafx.scene.*;

import javafx.scene.image.*;

import javafx.scene.control.*;

import javafx.scene.control.Alert.*;

import javafx.scene.text.*;

import javafx.scene.layout.*;

import javafx.scene.shape.*;

import javafx.stage.*;

import javafx.geometry.*;

import javafx.animation.*;

import java.io.*;

import java.util.*;

public class Races extends Application {

private Stage stage;

private Scene scene;

private VBox root;

private static String[] args;

private final static String ICON_IMAGE="embars.gif"; // file with icon for a racer

private final static int DEFAULT = 5;

private final static int WIDTH = 15;

private int iconWidth;

private int iconHeight;

private CarRacer racer = null;

private Image carImage = null;

private AnimationTimer timer;

private static int carNum;

public static void main(String [] _args) {

args = _args;

if(args.length == 0){

carNum = DEFAULT;

}

else{

carNum = Integer.parseInt(args[0]);

}

launch(args);

}

public void start(Stage _stage) {

stage = _stage;

stage.setTitle("Off to the Races");

stage.setOnCloseRequest(

new EventHandler() {

public void handle(WindowEvent evt) {

System.exit(0);

}

});

Line line = new Line();

line.setStartX(150.0);

line.setStartY(700.0);

line.setEndX(750.0);

line.setEndY(750.0);

root = new VBox(line);

initializeScene();

}

public void initializeScene() {

try {

carImage = new Image(new FileInputStream(ICON_IMAGE));

}

catch(Exception e) {

System.out.println("Exception: " + e);

System.exit(1);

}

iconWidth = (int)carImage.getWidth();

iconHeight = (int)carImage.getHeight();

scene = new Scene(root, iconWidth*WIDTH, iconHeight*carNum);

scene.getStylesheets().addAll(this.getClass().getResource("style.css").toExternalForm());

stage.setScene(scene);

stage.show();

System.out.println("Starting race...");

Vector cars = new Vector();

Vector threads = new Vector();

for(int i=1; i

CarRacer it = new CarRacer();

Thread thread = new Thread(it);

thread.start();

cars.add(it);

threads.add(thread);

root.getChildren().add(it);

}

for (Thread it: threads) {

try{

it.join();

}

catch(InterruptedException ie) {

ie.printStackTrace();

}

}

timer =

new AnimationTimer() {

public void handle(long now) {

for(CarRacer racer: cars){

racer.update();

}

}

};

TimerTask task =

new TimerTask() {

public void run() {

timer.start();

}

};

Timer startTimer = new Timer();

long delay = 1000L;

startTimer.schedule(task, delay);

}

protected class CarRacer extends Pane implements Runnable {

private int racePosX=0;

private ImageView aPicView;

public CarRacer() {

aPicView = new ImageView(carImage);

this.getChildren().add(aPicView);

}

@Override

public void run(){

while(true){

if(racePosX>500){

break;

}

update();

}

}

public void update() {

racePosX += (int)(Math.random() * iconWidth / 10);

aPicView.setTranslateX(racePosX);

if(racePosX>800) racePosX=0;

}

}

}

image text in transcribed

Off to the Races - Dave Patric 60 Winner is #2 00 Off to the Races - Dave Patric 60 Winner is #2 00

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Experts Guide To Database 2

Authors: Bruce L. Larson

1st Edition

0070232679, 978-0070232679

More Books

Students also viewed these Databases questions

Question

help asp

Answered: 1 week ago