/* * Copyright (c) 2016, 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. */ import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.Label; import java.awt.MediaTracker; import java.awt.Toolkit; import java.awt.Toolkit.MediaResolutionNamingScheme; import java.awt.image.BufferedImage; import java.awt.image.MultiResolutionImage; import java.io.File; import javax.imageio.ImageIO; /** * @test * @bug 8163854 * @summary Add ToolkitImage.getImage() method which loads an image * with schema variant * @key headful * @run main MultiResolutionToolkitMediaSchemeTest */ public class MultiResolutionToolkitMediaSchemeTest { private static final int IMAGE_WIDTH = 300; private static final int IMAGE_HEIGHT = 200; private static final int WAIT_TIME = 400; private static final RVTest[] TESTS = { new RVTest("image.png", "", Color.GREEN, 1), new RVTest("image@150pct.png", "@150pct", Color.BLUE, 1.5f), new RVTest("image@2x.png", "@2x", Color.ORANGE, 2), new RVTest("image.scale-250.png", ".scale-250", Color.PINK, 2.5f), new RVTest("image.scale-3x.png", ".scale-3x", Color.MAGENTA, 3f), }; public static void main(String[] args) throws Exception { generateImages(); testToolkitMultiResolutionImageLoad(); } static void testToolkitMultiResolutionImageLoad() throws Exception { RVTest baseImageTest = TESTS[0]; String imageName = baseImageTest.name; File imageFile = new File(imageName); String fileName = imageFile.getAbsolutePath(); int resolutionVariants = TESTS.length - 1; MediaResolutionNamingScheme[] schemes = new MediaResolutionNamingScheme[resolutionVariants]; for (int i = 0; i < resolutionVariants; i++) { schemes[i] = TESTS[i + 1].getScheme(); } Image image = Toolkit.getDefaultToolkit().getImageUsingNamingSchemes( fileName, schemes); if (!(image instanceof MultiResolutionImage)) { throw new RuntimeException("MultiResolutionImage is not returned!"); } Label label = new Label("Test"); MediaTracker mediaTracker = new MediaTracker(label); mediaTracker.addImage(image, 0); mediaTracker.waitForAll(WAIT_TIME); MultiResolutionImage mrImage = (MultiResolutionImage) image; for (int i = 1; i < TESTS.length; i++) { RVTest test = TESTS[i]; int w = test.getWidth(); int h = test.getHeight(); Image rvImage = mrImage.getResolutionVariant(w, h); int rvWidth = rvImage.getWidth(null); int rvHeight = rvImage.getHeight(null); if (rvWidth != w || rvHeight != h) { throw new RuntimeException("Wrong resolution variant size!"); } if (!getColor(rvImage, w, h).equals(test.color)) { throw new RuntimeException("Wrong resolution variant color!"); } } } private static Color getColor(Image image, int w, int h) { BufferedImage buffImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics g = buffImg.createGraphics(); g.drawImage(image, 0, 0, null); g.dispose(); return new Color(buffImg.getRGB(w / 2, h / 2)); } static void generateImages() throws Exception { for (RVTest test : TESTS) { if (!new File(test.name).exists()) { generateImage(test); } } } static void generateImage(RVTest test) throws Exception { int w = test.getWidth(); int h = test.getHeight(); BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); g.setColor(test.color); g.fillRect(0, 0, w, h); File file = new File(test.name); ImageIO.write(image, "png", file); } static class RVTest{ final String name; final String scheme; final Color color; final float scale; public RVTest(String name, String scheme, Color color, float scale) { this.name = name; this.scheme = scheme; this.color = color; this.scale = scale; } MediaResolutionNamingScheme getScheme(){ return new MediaResolutionNamingScheme(scheme, scale); } int getWidth() { return (int) Math.ceil(IMAGE_WIDTH * scale); } int getHeight() { return (int) Math.ceil(IMAGE_HEIGHT * scale); } } }