teraspora
teraspora

Reputation: 442

JavaFX misreports my screen sizes on Linux. Is this a bug?

The following code uses java.awt.GraphicsDevice and javafx.stage.Screen to get the screen sizes in pixels. JavaFX seems to get it wrong under Linux, as shown below (but correct under Windows 7). I would be interested to know if anyone else has experienced the same apparent bug.

import javafx.application.Application;
import javafx.stage.*;
import java.awt.*;

public class DisplayCheck extends Application {

    static int getScreenWidthViaAWT(int screenNum) {
        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[screenNum];
        return gd.getDisplayMode().getWidth();
    }

    static int getScreenHeightViaAWT(int screenNum) {
        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[screenNum];
        return gd.getDisplayMode().getHeight();
    }

    static int getScreenWidthViaJavaFX(int screenNum) {
        return (int)Screen.getScreens().get(screenNum).getVisualBounds().getWidth();
    }

    static int getScreenHeightViaJavaFX(int screenNum) {
        return (int)Screen.getScreens().get(screenNum).getVisualBounds().getHeight();
    }

    public static void main(String[] args) {

        int w0 = getScreenWidthViaAWT(0);
        int h0 = getScreenHeightViaAWT(0);
        int w1 = getScreenWidthViaAWT(1);
        int h1 = getScreenHeightViaAWT(1);

        System.out.println ("\n\nScreen sizes from java.awt.GraphicsDevice:\n\nScreen 0: " + w0 + " x " + h0 + "\nScreen 1: " + w1 + " x " + h1);

        w0 = getScreenWidthViaJavaFX(0);
        h0 = getScreenHeightViaJavaFX(0);
        w1 = getScreenWidthViaJavaFX(1);
        h1 = getScreenHeightViaJavaFX(1);

        System.out.println ("\n\nScreen sizes from javafx.stage.Screen:\n\nScreen 0: " + w0 + " x " + h0 + "\nScreen 1: " + w1 + " x " + h1);

        launch(args);
    }

    public void start (Stage stage) {
        System.exit(0);     
    }
 } 

On my system (Ubuntu MATE), JavaFX misreports the heights of both my laptop screen and my external monitor as 718 pixels. AWT gets it right. The results are as follows:

john@jlaptop2:/java$ javac DisplayCheck.java
john@jlaptop2:/java$ java DisplayCheck


Screen sizes from java.awt.GraphicsDevice:

Screen 0: 1024 x 768
Screen 1: 1920 x 1080


Screen sizes from javafx.stage.Screen:

Screen 0: 1920 x 718
Screen 1: 1024 x 718
john@jlaptop2:/java$

Upvotes: 1

Views: 227

Answers (1)

fabian
fabian

Reputation: 82461

You are using Screen.getVisualBounds instead of Screen.getBounds. The visual bounds exclude the area occupied by task bars, ect.

From the javadoc of Screen.getVisualBounds:

These bounds account for objects in the native windowing system such as task bars and menu bars.

Upvotes: 1

Related Questions