Sunday, November 10, 2013

Get screen size using javafx.stage.Screen

javafx.stage.Screen class describes the characteristics of a graphics destination such as monitor. In a virtual device multi-screen environment in which the desktop area could span multiple physical screen devices, the bounds of the Screen objects are relative to the Screen.primary. We can obtain the width and height of screen by calling Screen.getPrimary().getVisualBounds().getWidth() and Screen.getPrimary().getVisualBounds().getHeight().

This example set the application run in full screen.
application run in full screen
JavaFX application run in full screen

package javafx_screen;

import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Screen;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_Screen extends Application {

    @Override
    public void start(Stage primaryStage) {
        
        Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
        
        Label label = new Label();
        label.setText(
                visualBounds.getWidth() + " x " + visualBounds.getHeight());

        StackPane root = new StackPane();
        root.getChildren().add(label);
        
        Scene scene = new Scene(root, 
                visualBounds.getWidth(), visualBounds.getHeight());
        
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

No comments:

Post a Comment