1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.awt.BorderLayout;
  25 import java.awt.Canvas;
  26 import java.awt.Color;
  27 import java.awt.Frame;
  28 import java.awt.Graphics;
  29 import java.awt.Panel;
  30 import java.awt.Rectangle;
  31 import java.awt.Robot;
  32 import java.awt.image.BufferedImage;
  33 import javax.swing.UIManager;
  34 
  35 /* @test
  36  * @bug 8073320
  37  * @summary  Windows HiDPI support
  38  * @author Alexander Scherbatiy
  39  * @requires (os.family == "windows")
  40  * @run main/othervm -Dsun.java2d.win.uiScaleX=3 -Dsun.java2d.win.uiScaleY=2
  41  *                    HiDPIRobotScreenCaptureTest
  42  */
  43 public class HiDPIRobotScreenCaptureTest {
  44 
  45     private static final Color[] COLORS = {
  46         Color.GREEN, Color.BLUE, Color.ORANGE, Color.RED};
  47 
  48     public static void main(String[] args) throws Exception {
  49 
  50         try {
  51             UIManager.setLookAndFeel(
  52                     "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  53         } catch (Exception e) {
  54             return;
  55         }
  56 
  57         Frame frame = new Frame();
  58         frame.setBounds(40, 30, 400, 300);
  59         frame.setUndecorated(true);
  60 
  61         Panel panel = new Panel(new BorderLayout());
  62         Canvas canvas = new Canvas() {
  63             @Override
  64             public void paint(Graphics g) {
  65                 super.paint(g);
  66                 int w = getWidth();
  67                 int h = getHeight();
  68                 g.setColor(COLORS[0]);
  69                 g.fillRect(0, 0, w / 2, h / 2);
  70                 g.setColor(COLORS[1]);
  71                 g.fillRect(w / 2, 0, w / 2, h / 2);
  72                 g.setColor(COLORS[2]);
  73                 g.fillRect(0, h / 2, w / 2, h / 2);
  74                 g.setColor(COLORS[3]);
  75                 g.fillRect(w / 2, h / 2, w / 2, h / 2);
  76             }
  77         };
  78 
  79         panel.add(canvas);
  80         frame.add(panel);
  81         frame.setVisible(true);
  82         Robot robot = new Robot();
  83         robot.waitForIdle();
  84         Thread.sleep(200);
  85 
  86         Rectangle rect = canvas.getBounds();
  87         rect.setLocation(canvas.getLocationOnScreen());
  88 
  89         BufferedImage image = robot.createScreenCapture(rect);
  90         frame.dispose();
  91 
  92         int w = image.getWidth();
  93         int h = image.getHeight();
  94 
  95         if (w != frame.getWidth() || h != frame.getHeight()) {
  96             throw new RuntimeException("Wrong image size!");
  97         }
  98 
  99         if (image.getRGB(w / 4, h / 4) != COLORS[0].getRGB()) {
 100             throw new RuntimeException("Wrong image color!");
 101         }
 102 
 103         if (image.getRGB(3 * w / 4, h / 4) != COLORS[1].getRGB()) {
 104             throw new RuntimeException("Wrong image color!");
 105         }
 106 
 107         if (image.getRGB(w / 4, 3 * h / 4) != COLORS[2].getRGB()) {
 108             throw new RuntimeException("Wrong image color!");
 109         }
 110 
 111         if (image.getRGB(3 * w / 4, 3 * h / 4) != COLORS[3].getRGB()) {
 112             throw new RuntimeException("Wrong image color!");
 113         }
 114     }
 115 }