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.imageio.ImageIO;
  34 import javax.swing.UIManager;
  35 
  36 /* @test
  37  * @bug 8073320 8137571
  38  * @summary Windows and Linux HiDPI Graphics support
  39  * @author Alexander Scherbatiy
  40  * @requires (os.family == "windows" | os.family == "linux")
  41  * @run main/othervm -Dsun.java2d.uiScale=2 HiDPIRobotScreenCaptureTest
  42  */
  43 public class HiDPIRobotScreenCaptureTest {
  44 
  45     private static final Color[] COLORS = {
  46         Color.GREEN, Color.BLUE, Color.ORANGE, Color.RED};
  47 
  48     private static final String[] SUPPORTED_LAFS = {
  49         "com.sun.java.swing.plaf.windows.WindowsLookAndFeel",
  50         "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"
  51     };
  52 
  53     public static void main(String[] args) throws Exception {
  54 
  55         if (!setSupportedLAF()) {
  56             return;
  57         }
  58 
  59         Frame frame = new Frame();
  60         frame.setBounds(10, 20, 200, 150);
  61         frame.setUndecorated(true);
  62 
  63         Panel panel = new Panel(new BorderLayout());
  64         Canvas canvas = new Canvas() {
  65             @Override
  66             public void paint(Graphics g) {
  67                 super.paint(g);
  68                 int w = getWidth();
  69                 int h = getHeight();
  70                 g.setColor(COLORS[0]);
  71                 g.fillRect(0, 0, w / 2, h / 2);
  72                 g.setColor(COLORS[1]);
  73                 g.fillRect(w / 2, 0, w / 2, h / 2);
  74                 g.setColor(COLORS[2]);
  75                 g.fillRect(0, h / 2, w / 2, h / 2);
  76                 g.setColor(COLORS[3]);
  77                 g.fillRect(w / 2, h / 2, w / 2, h / 2);
  78             }
  79         };
  80 
  81         panel.add(canvas);
  82         frame.add(panel);
  83         frame.setVisible(true);
  84         Robot robot = new Robot();
  85         robot.waitForIdle();
  86         Thread.sleep(200);
  87 
  88         Rectangle rect = canvas.getBounds();
  89         rect.setLocation(canvas.getLocationOnScreen());
  90 
  91         BufferedImage image = robot.createScreenCapture(rect);
  92         frame.dispose();
  93 
  94         int w = image.getWidth();
  95         int h = image.getHeight();
  96 
  97         if (w != frame.getWidth() || h != frame.getHeight()) {
  98             throw new RuntimeException("Wrong image size!");
  99         }
 100 
 101         if (image.getRGB(w / 4, h / 4) != COLORS[0].getRGB()) {
 102             throw new RuntimeException("Wrong image color!");
 103         }
 104 
 105         if (image.getRGB(3 * w / 4, h / 4) != COLORS[1].getRGB()) {
 106             throw new RuntimeException("Wrong image color!");
 107         }
 108 
 109         if (image.getRGB(w / 4, 3 * h / 4) != COLORS[2].getRGB()) {
 110             throw new RuntimeException("Wrong image color!");
 111         }
 112 
 113         if (image.getRGB(3 * w / 4, 3 * h / 4) != COLORS[3].getRGB()) {
 114             throw new RuntimeException("Wrong image color!");
 115         }
 116     }
 117 
 118     private static boolean setSupportedLAF() {
 119 
 120         for (String supportedLAF : SUPPORTED_LAFS) {
 121             try {
 122                 UIManager.setLookAndFeel(supportedLAF);
 123                 return true;
 124             } catch (Exception ignored) {
 125             }
 126         }
 127 
 128         return false;
 129     }
 130 }