Login  Register

Re: GLcanvas vs NEWT on Hi DPI Screens

Posted by Martin on Feb 14, 2022; 9:59am
URL: https://forum.jogamp.org/GLcanvas-vs-NEWT-on-Hi-DPI-Screens-tp4041191p4041642.html

Hi,

Mahesh fix is good but it is not dynamic. It forces a multiplying factor of 2, whatever the actual pixel scale. This won't work properly on screens with 1.5 pixel scales. This will also prevent programs ran on Java <= 8 to keep their pixel scale = 1 (The JVM only detects HiDPI as of Java 9).

I made a tiny improvement visible here : https://gist.github.com/jzy3d/bc392d97fe8bd956470a2f8f002f4c87

It basically get pixel ratio from the AWT Component Graphics instance :

                @Override
                public int getWidth() {
                        double scale = getPixelScaleX();
                       
                        return (int)(super.getWidth() * scale);
                }
               
                @Override
                public int getHeight() {
                        double scale = getPixelScaleY();

                        return (int)(super.getHeight() * scale);
                }
               
                protected double getPixelScaleX() {
                        Graphics2D g2d = (Graphics2D)getGraphics();
                        AffineTransform globalTransform = g2d.getTransform();
                        return globalTransform.getScaleX();
                }
               
                protected double getPixelScaleY() {
                        Graphics2D g2d = (Graphics2D)getGraphics();
                        AffineTransform globalTransform = g2d.getTransform();
                        return globalTransform.getScaleY();
                }