1 /*
   2  * Copyright (c) 2017, 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 /*
  25   @test
  26   @bug 8148619
  27   @summary Select the closest resolution variant in BaseMultiResolutionImage
  28   @run main/othervm -Dsun.java2d.uiScale=1 MultiResolutionVariantSelectionTest
  29   @run main/othervm -Dsun.java2d.uiScale=2 MultiResolutionVariantSelectionTest
  30   @run main/othervm -Dsun.java2d.uiScale=3 MultiResolutionVariantSelectionTest
  31   @run main/othervm -Dsun.java2d.uiScale=4 MultiResolutionVariantSelectionTest
  32 */
  33 
  34 import java.awt.*;
  35 import java.awt.image.*;
  36 
  37 public class MultiResolutionVariantSelectionTest {
  38 
  39     static Color[] colors = {Color.RED, Color.GREEN, Color.BLUE};
  40     static int W = 100, H = 100;
  41 
  42     public static void main(String[] args) throws Exception {
  43         Image[] images = new Image[colors.length];
  44         for (int i = colors.length - 1; i >= 0; i--) {
  45             images[i] = generateImage(i + 1, colors[i]);
  46         }
  47         Image mrImage =new BaseMultiResolutionImage(images);
  48 
  49         Frame frame = new Frame();
  50         Panel panel = new Panel() {
  51             @Override
  52             public void paint(Graphics g) {
  53                 super.paint(g);
  54                 g.drawImage(mrImage, 0, 0, W, H, null);
  55             }
  56         };
  57         panel.setPreferredSize(new Dimension(W, H));
  58         frame.add(panel);
  59         frame.pack();
  60         frame.setVisible(true);
  61 
  62         Robot robot = new Robot();
  63         robot.delay(200);
  64         robot.waitForIdle();
  65 
  66         Point p = panel.getLocationOnScreen();
  67         p.translate(W / 2, H / 2);
  68         Color color = robot.getPixelColor(p.x, p.y);
  69         int expected = Math.min(
  70                 Integer.parseInt(System.getProperty("sun.java2d.uiScale")),
  71                 colors.length) - 1;
  72         if (!color.equals(colors[expected])) {
  73             throw new RuntimeException("Wrong image variant painted " + color);
  74         }
  75     }
  76 
  77     private static BufferedImage generateImage(int scale, Color c) {
  78         BufferedImage image = new BufferedImage(
  79                 scale * W, scale * H, BufferedImage.TYPE_INT_RGB);
  80         Graphics g = image.getGraphics();
  81         g.setColor(c);
  82         g.fillRect(0, 0, scale * W + 1, scale * H + 1);
  83         g.dispose();
  84         return image;
  85     }
  86 }