JDK-8204508 : Robot ScreenCapture fails on HiDPI system
  • Type: Bug
  • Component: deploy
  • Sub-Component: webstart
  • Affected Version: 8u171,9
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_10
  • CPU: x86
  • Submitted: 2018-06-06
  • Updated: 2019-04-05
  • Resolved: 2018-07-16
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8
8u192 b03Fixed
Related Reports
Relates :  
Relates :  
Description
When using Robot#createScreenCapture(...) on a HiDpi system the wrong area is captured. The issue occurs only if:
- the application runs on a HiDpi system
- the application is started with Webstart
- the system property 'sun.java2d.dpiaware' is set to 'false'

A simple test can be found below - you can also give it a try by executing http://www.jyloo.com/downloads/public/test/robotScreenTest.jnlp. When pressing the ScreenCaptureNow button a screen capture of the yellow rectangle is made and displayed in a new window. On a HiDpi system the captured area is wrong.
 
Test Case:
-----------------------------------
package test.robot;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.border.LineBorder;

public class RobotScreenTest extends JFrame{
  private Rectangle rect = new Rectangle(100, 100, 400, 300);
  
  public static void main(String[] args) throws Exception{
    EventQueue.invokeLater(new Runnable(){      
      @Override
      public void run(){
        new RobotScreenTest();        
      }
    });
  }

  public RobotScreenTest(){
    setLayout(new FlowLayout());
    
    add(new JButton(new AbstractAction("Screen Capture Now"){      
      @Override
      public void actionPerformed(ActionEvent evt){
        try{
          final BufferedImage screenshot = new Robot().createScreenCapture(rect);
          JPanel pane = new JPanel(){
            @Override
            protected void paintComponent(Graphics g){
              g.drawImage(screenshot, 0, 0, null);
            }
          };
          pane.setPreferredSize(rect.getSize());
          JFrame f = new JFrame();
          f.setTitle("ScreenCapture");
          f.add(pane);
          f.pack();
          f.setVisible(true);
        }
        catch (Exception e){
          e.printStackTrace();
        }                
      }
    }));
    add(new JLabel("Bounds: "+rect));
    
    setTitle(getClass().getSimpleName());
    setSize(400, 300);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);    
    
    JWindow w = new JWindow();
    JPanel p = new JPanel();
    p.setBackground(Color.YELLOW);
    p.setBorder(new LineBorder(Color.RED));
    w.setContentPane(p);
    w.setBounds(rect);
    w.setVisible(true);    
  }  
}
Comments
change making javaws behavior the same as stand alone apps on windows by adding the manifest entry to jweblauncher.exe: crucible review: https://java.se.oracle.com/code/cru/CR-JDK8UDEV-491
13-07-2018

OK - I can reproduce this with 8u171 on windows 7 when changing Display settings to "Larger - 150%" The reason it makes any difference whether or not 'sun.java2d.dpiaware' is set to false, is that jweblauncher.exe does not have the manifest entry dpiAware set to true (where java.exe, javacpl.exe, and javaws.exe do have this manifest entry). The underlying problem is that there were a lot of changes in 9 to *really* support hi dpi on windows. In 8u we are telling GDI we are high dpi aware (in the case of webstart, only with the property) so that we do not get scaled, and as a result we display smaller than other apps but a pixel is a pixel. Then by telling it sun.java2d.dpiaware, he is actually asking for an unsupportable situation for robot screen capture. What I don't yet understand is why this is any different in webstart from running the same app locally with : java -Dsun.java2d.dpiaware=true -jar robotScreenTest.jar in that case you can see the windows are displayed smaller (unscaled), where in webstart the same windows are scaled up.
10-07-2018

I cannot reproduce this on my macbook pro (with Retina display) using 10.0.2 or 8u161. My windows desktop does not have HiDPI display - AM lookiing for a system this shows on.
10-07-2018

Yes, it's reproducible with Java 8u171 too.
07-06-2018

does it affect 8u?
07-06-2018