Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Basic JavaFX Application
package com.knoldus.javafx;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class JavaFXSeeder extends Application {
public final static String ID = "seeder";
public static final String SEEDER_CONTAINER_ID = "seeder-container-id";
public final static String SEEDER_BACKGROUND_ID = "seeder-background-id";
public final static String SEEDER_CONTENT_ID = "seeder-content-id";
public final static String SEEDER_CONTROLS_ID = "seeder-controls-id";
public final static String SEEDER_LOGO_ID = "seeder-logo-id";
public final static String SEEDER_STATUS_ID = "seeder-status-id";
public final static String SEEDER_CLOSE_BUTTON_ID = "seeder-close-button-id";
public final static String IMAGES = "file:src/main/resources/img";
public final static String SEEDER_BACKGROUND = IMAGES + "/seeder-background.jpg";
public final static String SEEDER_LOGO = IMAGES + "/seeder-logo.gif";
public final static String SEEDER_ICON = IMAGES + "/seeder-icon.jpg";
public final static String SEEDER_CLOSE_BUTTON = IMAGES + "/seeder-close-button.png";
public final static String SEEDER_CLOSE_BUTTON_HOVERED = IMAGES + "/seeder-close-button-hovered.png";
public final static String SEEDER_STAGE_TITLE = "Loader";
public final static String SEEDER_STATUS = "Loading";
public final static double SEEDER_CLOSE_BUTTON_SIZE = 24;
private static final double SEEDER_WIDTH = 768;
private static final double SEEDER_HEIGHT = 464;
private static final double SEEDER_LOGO_SIZE = 200;
private static double xOffset = 0;
private static double yOffset = 0;
private final double SEEDER_STATUS_FONT_SIZE = 24;
private final String SEEDER_STATUS_FONT_FAMILY = "Roboto Medium";
private final Color SEEDER_STATUS_COLOR = Color.rgb(25, 25, 25, 1);
private final Font SEEDER_STATUS_FONT = Font.font(SEEDER_STATUS_FONT_FAMILY, FontWeight.BOLD, SEEDER_STATUS_FONT_SIZE);
private final StackPane m_seederContainer = new StackPane();
private final VBox m_seederContent = new VBox();
private final HBox m_seederControls = new HBox();
private final ImageView m_seederBackground = new ImageView();
private final ImageView m_seederLogo = new ImageView();
private final ImageView m_seederCloseButtonImage = new ImageView();
private final Text m_seederStatus = new Text();
private final Button m_seederCloseButton = new Button();
private final Group m_root = new Group();
private final Stage m_stage = new Stage();
private Image m_image;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
initialize();
showSeederWindow();
}
private void initialize() {
Scene scene = new Scene(m_root, SEEDER_WIDTH, SEEDER_HEIGHT);
m_stage.setScene(scene);
m_stage.setTitle(JavaFXSeeder.SEEDER_STAGE_TITLE);
m_stage.setHeight(SEEDER_HEIGHT);
m_stage.setWidth(SEEDER_WIDTH);
m_stage.initStyle(StageStyle.TRANSPARENT);
m_stage.getIcons().addAll(new Image(SEEDER_ICON));
m_stage.setOnCloseRequest(windowEvent ->
{
windowEvent.consume();
handleCloseEvent();
});
m_root.setId(ID);
StackPane.setAlignment(m_seederContainer, Pos.CENTER);
m_seederContainer.setId(SEEDER_CONTAINER_ID);
m_seederContainer.setMaxSize(SEEDER_WIDTH, SEEDER_HEIGHT);
initializeSeederBackground();
initializeSeederContent();
initializeSeederControls();
m_seederContainer.getChildren().addAll(m_seederBackground, m_seederContent, m_seederControls);
m_root.getChildren().add(m_seederContainer);
dragSeeder();
}
public void initializeSeederBackground() {
m_image = new Image(SEEDER_BACKGROUND);
m_seederBackground.setId(SEEDER_BACKGROUND_ID);
m_seederBackground.setImage(m_image);
m_seederBackground.setFitWidth(SEEDER_WIDTH);
m_seederBackground.setFitHeight(SEEDER_HEIGHT);
}
public void initializeSeederContent() {
initializeSeederLogo();
initializeSeederStatus();
m_seederContent.getChildren().addAll(m_seederLogo, m_seederStatus);
m_seederContent.setId(SEEDER_CONTENT_ID);
m_seederContent.setSpacing(10);
m_seederContent.setAlignment(Pos.CENTER);
}
public void initializeSeederLogo() {
m_image = new Image(SEEDER_LOGO);
m_seederLogo.setId(SEEDER_LOGO_ID);
m_seederLogo.setImage(m_image);
m_seederLogo.setFitWidth(SEEDER_LOGO_SIZE);
m_seederLogo.setFitHeight(SEEDER_LOGO_SIZE);
}
public void initializeSeederStatus() {
m_seederStatus.setText(SEEDER_STATUS);
m_seederStatus.setId(SEEDER_STATUS_ID);
m_seederStatus.setFill(SEEDER_STATUS_COLOR);
m_seederStatus.setFont(SEEDER_STATUS_FONT);
m_seederStatus.setTextAlignment(TextAlignment.CENTER);
}
public void initializeSeederControls() {
initializeSeederCloseButton();
m_seederControls.getChildren().addAll(m_seederCloseButton);
m_seederControls.setId(SEEDER_CONTROLS_ID);
m_seederControls.setAlignment(Pos.TOP_RIGHT);
}
public void initializeSeederCloseButton() {
m_image = new Image(SEEDER_CLOSE_BUTTON);
m_seederCloseButtonImage.setImage(m_image);
m_seederCloseButtonImage.setFitWidth(SEEDER_CLOSE_BUTTON_SIZE);
m_seederCloseButtonImage.setFitHeight(SEEDER_CLOSE_BUTTON_SIZE);
m_seederCloseButton.setId(SEEDER_CLOSE_BUTTON_ID);
m_seederCloseButton.setGraphic(m_seederCloseButtonImage);
m_seederCloseButton.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT, CornerRadii.EMPTY, Insets.EMPTY)));
m_seederCloseButton.setOnAction(handleCloseEvent());
m_seederCloseButton.addEventHandler(MouseEvent.ANY, seederCloseButtonMouseEvent());
}
private EventHandler<? super MouseEvent> seederCloseButtonMouseEvent() {
return (EventHandler<MouseEvent>) event ->
{
if (event.getEventType() == MouseEvent.MOUSE_ENTERED) {
m_image = new Image(SEEDER_CLOSE_BUTTON_HOVERED);
m_seederCloseButtonImage.setImage(m_image);
} else if (event.getEventType() == MouseEvent.MOUSE_EXITED) {
m_image = new Image(SEEDER_CLOSE_BUTTON);
m_seederCloseButtonImage.setImage(m_image);
}
};
}
public void showSeederWindow() {
m_stage.show();
m_stage.toFront();
m_stage.setAlwaysOnTop(true);
}
private EventHandler<ActionEvent> handleCloseEvent() {
return event -> System.exit(0);
}
private void dragSeeder() {
m_seederContainer.setOnMousePressed(event ->
{
xOffset = event.getSceneX();
yOffset = event.getSceneY();
});
m_seederContainer.setOnMouseDragged(event ->
{
m_stage.setX(event.getScreenX() - xOffset);
m_stage.setY(event.getScreenY() - yOffset);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment