Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JavaFX - ( Raise flags ) - Rewrite Listing 15.13 using a thread to animate a fla

ID: 3815840 • Letter: J

Question

JavaFX - (Raise flags) - Rewrite Listing 15.13 using a thread to animate a flag being raised. Compare the program with Listing 15.13 by setting the delay time to 10 in both programs. Which one runs the animation faster?

Listing 15.13 is listed in the image below:

LISTING 15.13 FlagRisingAnimation java 1 import javafx animation PathTransition; 2 import javafx.appli Application lication. 3 import javafx. scene. Scene: 4 import javafx. mage. ImageView: 5 import javafx.scene .layout. Pane; 6 import javafx.scene. shape.Line 7 import javafx.stage. Stage: 8 import javafx.util.Duration; 10 public class FlagRisingAnimation extends Application 11 coverride Override the start method in the Application class 12 public void start(Stage pri Stage Create a pane 13 14 Pane pane new Pane CD create a pane 15 Add an image view and add it to pane Image View i mage View new ImageView /us gif'') 17 create an image view pane. getChildren add imageview 18 add image view to pane Create a path transition 20 PathTransition pt new PathTransition (Duration millis( create a path transition (10000), 21 612 Chapter 15 vent-Driven Programming and Animations new Line 100, 200, 100, 0 imageview 22 23 pt.setCycleCount (5) set cycle count pt .play Start animation play animation 24 25 Create a scene and place it in the stage 26 Scene scene new Scene (pane 250 200) 27 28 primary Stage. setTitle C FlagRising Animation Set the stage title primary stage setscene(scene) Place the scene in the stage 29 30 rimary Stage. show() Display the stage 31 32

Explanation / Answer


import javafx.animation.PathTransition;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import javafx.util.Duration;

public class FlagRisingAnimation extends Application {
    private double flagY = 200;
    @Override // Override the start method in the Application class
    public void start(Stage primaryStage) {
// Create a pane
        Pane pane = new Pane();

// Add an image view and add it to pane
        Image image = new Image("us.gif");
        ImageView imageView = new ImageView(image);
        pane.getChildren().add(imageView);
        imageView.setX(50);
        imageView.setY(flagY);

        new Thread(() -> {
            try {
                while (flagY >=0) {
                    Platform.runLater(() -> imageView.setY(--flagY));
                    Thread.sleep(20);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        ).start();

// Create a path transition
       /* PathTransition pt = new PathTransition(Duration.millis(10000),
                new Line(100, 200, 100, 0), imageView);
        pt.setCycleCount(5);
        pt.play(); // Start animation
*/

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

    /**
     * The main method is only needed for the IDE with limited
     * JavaFX support. Not needed for running from the command line.
     */
    public static void main(String[] args) {
        launch(args);
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote