Question
Can you modify the below simple javaFX code to achieve thatwhen run it displays a splash screen with an image of your likingand any sentence
Can you modify the below simple javaFX code to achieve thatwhen run it displays a splash screen with an image of your likingand any sentence at the bottom of the splash window?
code:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javax.swing.Timer;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
A simple class demonstrating how to create a simple splashscreen in JavaFX.
For more sophisticated splash screens you should use thePreloader class provided in JavaFX.
To find how to use the Preloader class visit the linkbelow:
http://docs.oracle.com/javafx/2/deployment/preloaders.htm
@version 1.16.2
@since JavaFX 8.45
*/
public class SplashScreenDemoFX extends Application {
/**splash screen time interval - application delay*/
private final int delay = 5000;
@Override // Override the start method in the Applicationclass
public void start(Stage primaryStage) {
//Create a layout container
BorderPane borderPane = new BorderPane();
//Create the splash screen components
//Create a splash screen image
Image splashImage = newImage("DukeAppSplash.gif");
//Place the image in center of the container
borderPane.setCenter(newImageView(splashImage));
//Create and set the properties of of a label
Label label = new Label(" SBahr's App SplashScreen Demo FX ");
label.setAlignment(Pos.CENTER);
label.setFont(Font.font("Arial", FontWeight.BOLD,FontPosture.ITALIC, 14));
//Set a border using the API Border class (new classintroduced in Java 8)
label.setBorder(new Border(newBorderStroke(Color.RED,BorderStrokeStyle.SOLID,newCornerRadii(10),new BorderWidths(2.0) )));
//Place the label into a container so that it appearsin the center
StackPane stackPane = new StackPane();
stackPane.getChildren().add(label);
//Place the container into another container
borderPane.setBottom(stackPane);
//Set the container border
borderPane.setBorder(new Border(newBorderStroke(Color.GREEN,BorderStrokeStyle.SOLID,newCornerRadii(10),new BorderWidths(6.0))));
//Place the container on the scene
Scene scene = new Scene(borderPane, 545, 295);
//Create a stage for the splash screen scene
Stage splashStage = new Stage(); // Create a newstage
splashStage.setTitle("SplashScreenDemoFX"); // Setthe stage title
splashStage.setScene(scene); // Place the scene inthe stage
splashStage.initStyle(StageStyle.UNDECORATED );
//Set the application stage
primaryStage.setTitle("Application Stage"); // Setthe stage title
// Set a scene with a button in the stage
primaryStage.setScene(new Scene(newButton("Application Stage"), 400, 200));
// Display the splash stage
splashStage.show();
/*The following code makes sure that the stages aredisplayed in proper order.
The splash stage is closed after initial somedelay and the primary stage is displayed.
A Timer object is used to provide thedelay.
The delay is 5000 msec (5 sec).
The Timer object calls the actionPerformed()method of the ActionListener after an initial delay.
Since the actionPerformed() method executes onthe event-dispatch thread
the code must use Platform.runLater() toexecute the JavaFX code on the application thread.
*/
// create an ActionListener object using an anonymousinner class implementing ActionListener
ActionListener listener = new ActionListener(){
@Override
public void actionPerformed(ActionEvente){
//Run the codeclosing the splash stage and showing the primary stage on theapplication thread
//Anonymous inner classimplementing Runnable() is used to provide a parameter forrunLater()
Platform.runLater(new Runnable(){
@Override
public void run() {
splashStage.close();
primaryStage.show(); //Display the stage
}
}
);
}
};
//Create a Timer object
Timer t = new Timer (delay, listener);
//make the timer to generate one time event only
t.setRepeats(false);
//Start the timer
t.start();
}//end start
public static void main(String[] args) {
Application.launch(args);
}
}// end class
Step by Step Solution
3.30 Rating (147 Votes )
There are 3 Steps involved in it
Step: 1
Here is the modified code that displays a splash screen with an image of your liking and any sentenc...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