--- old/src/java.desktop/share/classes/java/awt/image/BaseMultiResolutionImage.java 2017-09-06 19:20:21.785903422 -0700 +++ new/src/java.desktop/share/classes/java/awt/image/BaseMultiResolutionImage.java 2017-09-06 19:20:21.685909384 -0700 @@ -24,11 +24,9 @@ */ package java.awt.image; -import java.awt.Image; +import java.awt.*; +import java.util.*; import java.util.List; -import java.util.Arrays; -import java.util.Collections; -import java.util.Objects; /** * This class is an array-based implementation of @@ -37,14 +35,12 @@ * This class will implement the * {@code getResolutionVariant(double destImageWidth, double destImageHeight)} * method using a simple algorithm which will return the first image variant - * in the array that is large enough to satisfy the rendering request. The - * last image in the array will be returned if no suitable image is found - * that is as large as the rendering request. + * in the array which size equals to the rendering area or is wider and taller + * than it. The largest image in the array will be returned if no suitable + * image is found that is as large as the rendering request. *

- * For best effect the array of images should be sorted with each image being - * both wider and taller than the previous image. The base image need not be - * the first image in the array. No exception will be thrown if the images - * are not sorted as suggested. + * The base image need not be the first image in the array. No exception will + * be thrown if the images are not sorted as suggested. * * @see java.awt.Image * @see java.awt.image.MultiResolutionImage @@ -117,13 +113,19 @@ checkSize(destImageWidth, destImageHeight); + int[][] sizes = new int[resolutionVariants.length][3]; + int idx = 0; for (Image rvImage : resolutionVariants) { - if (destImageWidth <= rvImage.getWidth(null) - && destImageHeight <= rvImage.getHeight(null)) { - return rvImage; + sizes[idx] = new int[] {idx++, rvImage.getWidth(null), + rvImage.getHeight(null)}; + } + Arrays.sort(sizes, (o1, o2) -> o1[1] + o1[2] - o2[1] - o2[2]); + for (int[] size : sizes) { + if (destImageWidth <= size[1] && destImageHeight <= size[2]) { + return resolutionVariants[size[0]]; } } - return resolutionVariants[resolutionVariants.length - 1]; + return resolutionVariants[sizes[sizes.length - 1][0]]; } private static void checkSize(double width, double height) { --- /dev/null 2017-09-05 07:53:18.071706981 -0700 +++ new/test/java/awt/image/BaseMultiResolutionImage/MultiResolutionVariantSelectionTest.java 2017-09-06 19:20:21.993891020 -0700 @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + @test + @bug 8148619 + @summary Select the closest resolution variant in BaseMultiResolutionImage + @run main/othervm -Dsun.java2d.uiScale=1 MultiResolutionVariantSelectionTest + @run main/othervm -Dsun.java2d.uiScale=2 MultiResolutionVariantSelectionTest + @run main/othervm -Dsun.java2d.uiScale=3 MultiResolutionVariantSelectionTest + @run main/othervm -Dsun.java2d.uiScale=4 MultiResolutionVariantSelectionTest +*/ + +import java.awt.*; +import java.awt.image.*; + +public class MultiResolutionVariantSelectionTest { + + static Color[] colors = {Color.RED, Color.GREEN, Color.BLUE}; + static int W = 100, H = 100; + + public static void main(String[] args) throws Exception { + Image[] images = new Image[colors.length]; + for (int i = colors.length - 1; i >= 0; i--) { + images[i] = generateImage(i + 1, colors[i]); + } + Image mrImage =new BaseMultiResolutionImage(images); + + Frame frame = new Frame(); + Panel panel = new Panel() { + @Override + public void paint(Graphics g) { + super.paint(g); + g.drawImage(mrImage, 0, 0, W, H, null); + } + }; + panel.setPreferredSize(new Dimension(W, H)); + frame.add(panel); + frame.pack(); + frame.setVisible(true); + + Robot robot = new Robot(); + robot.delay(200); + robot.waitForIdle(); + + Point p = panel.getLocationOnScreen(); + p.translate(W / 2, H / 2); + Color color = robot.getPixelColor(p.x, p.y); + int expected = Math.min( + Integer.parseInt(System.getProperty("sun.java2d.uiScale")), + colors.length) - 1; + if (!color.equals(colors[expected])) { + throw new RuntimeException("Wrong image variant painted " + color); + } + } + + private static BufferedImage generateImage(int scale, Color c) { + BufferedImage image = new BufferedImage( + scale * W, scale * H, BufferedImage.TYPE_INT_RGB); + Graphics g = image.getGraphics(); + g.setColor(c); + g.fillRect(0, 0, scale * W + 1, scale * H + 1); + g.dispose(); + return image; + } +}