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 25 /** 26 * @test 27 * @bug 8146042 28 * @run main RenderRectTest 29 */ 30 31 import java.awt.*; 32 33 import java.awt.geom.AffineTransform; 34 import java.awt.image.BufferedImage; 35 import java.awt.image.VolatileImage; 36 37 38 public class RenderRectTest { 39 40 public static void main(String[] args) throws Exception { 41 GraphicsConfiguration gc = 42 GraphicsEnvironment.getLocalGraphicsEnvironment(). 43 getDefaultScreenDevice().getDefaultConfiguration(); 44 45 AffineTransform transform = gc.getDefaultTransform(); 46 if(transform.getScaleX() == 1 && transform.getScaleY() == 1) { 47 VolatileImage vi = gc.createCompatibleVolatileImage(12, 12); 48 Graphics2D g = vi.createGraphics(); 49 g.setColor(Color.black); 50 g.drawRect(1, 1, 9, 9); 51 g.dispose(); 52 BufferedImage capture = vi.getSnapshot(); 53 54 int w = capture.getWidth(); 55 int h = capture.getHeight(); 56 for (int y = 0; y < h >> 1; y++) { 57 for (int x = 0; x < w >> 1; x++) { 58 if (x > 0 && y > 0 && (x == 1 || y == 1)) { 59 if ((capture.getRGB(x, y) 60 | capture.getRGB(x, h - 1 - y) 61 | capture.getRGB(w - 1 - x, y) 62 | capture.getRGB(w - 1 - x, h - 1 - y)) != 63 Color.black.getRGB()) { 64 throw new RuntimeException("Wrong rectangle"); 65 } 66 } else { 67 if ((capture.getRGB(x, y) 68 & capture.getRGB(x, h - 1 - y) 69 & capture.getRGB(w - 1 - x, y) 70 & capture.getRGB(w - 1 - x, h - 1 - y)) != 71 Color.white.getRGB()) { 72 throw new RuntimeException("Wrong rectangle"); 73 } 74 } 75 } 76 } 77 } else { 78 VolatileImage vi = gc.createCompatibleVolatileImage(12, 12); 79 Graphics2D g = vi.createGraphics(); 80 g.setColor(Color.black); 81 g.drawRect(1, 1, 9, 9); 82 g.dispose(); 83 84 BufferedImage i1 = new BufferedImage((int)(12 * transform.getScaleX()), 85 (int)(12 * transform.getScaleY()), BufferedImage.TYPE_4BYTE_ABGR_PRE); 86 g = i1.createGraphics(); 87 g.drawImage(vi, 0, 0, i1.getWidth(), i1.getHeight(), null); 88 g.dispose(); 89 90 91 BufferedImage i2 = gc.createCompatibleImage(i1.getWidth(), i1.getHeight()); 92 g = i2.createGraphics(); 93 g.setBackground(Color.white); 94 g.clearRect(0, 0, i2.getWidth(), i2.getHeight()); 95 g.setTransform(transform); 96 g.setColor(Color.black); 97 g.drawRect(1, 1, 9, 9); 98 g.dispose(); 99 100 int w = i1.getWidth(); 101 int h = i1.getHeight(); 102 for (int y = 0; y < h; y++) { 103 for (int x = 0; x < w ; x++) { 104 if (i1.getRGB(x, y) != i2.getRGB(x, y)) { 105 throw new RuntimeException("Rectangles are different"); 106 } 107 } 108 } 109 } 110 } 111 }