Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8204508

Robot ScreenCapture fails on HiDPI system

    XMLWordPrintable

    Details

    • Subcomponent:
    • Introduced In Version:
      9
    • Resolved In Build:
      b03
    • CPU:
      x86
    • OS:
      windows_10

      Backports

        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);
          }
        }

          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  herrick Andy Herrick
                  Reporter:
                  wzberger Wolfgang Zitzelsberger
                • Votes:
                  0 Vote for this issue
                  Watchers:
                  3 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved: