1/* 2 * Copyright (c) 2011, 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 7043054 27 * @summary Verifies that Paint objects receive the appropriate user space 28 * bounds in their createContext() method 29 * @run main PgramUserBoundsTest 30 */ 31 32import java.awt.Color; 33import java.awt.Graphics2D; 34import java.awt.Paint; 35import java.awt.PaintContext; 36import java.awt.RenderingHints; 37import java.awt.Rectangle; 38import java.awt.geom.AffineTransform; 39import java.awt.geom.Line2D; 40import java.awt.geom.Rectangle2D; 41import java.awt.image.BufferedImage; 42import java.awt.image.ColorModel; 43 44public class PgramUserBoundsTest { 45 static final int MinX = 10; 46 static final int MinY = 20; 47 static final int MaxX = 30; 48 static final int MaxY = 50; 49 static AffineTransform identity = new AffineTransform(); 50 51 public static void main(String argv[]) { 52 BufferedImage bimg = 53 new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); 54 Graphics2D g2d = bimg.createGraphics(); 55 g2d.setPaint(new BoundsCheckerPaint(MinX, MinY, MaxX, MaxY)); 56 testAll(g2d); 57 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 58 RenderingHints.VALUE_ANTIALIAS_ON); 59 testAll(g2d); 60 } 61 62 static void testAll(Graphics2D g2d) { 63 g2d.setTransform(identity); 64 g2d.translate(100, 100); 65 testPrimitives(g2d); 66 67 g2d.setTransform(identity); 68 g2d.scale(10, 10); 69 testPrimitives(g2d); 70 71 g2d.setTransform(identity); 72 g2d.rotate(Math.PI/6); 73 testPrimitives(g2d); 74 } 75 76 static void testPrimitives(Graphics2D g2d) { 77 testLine(g2d); 78 testRect(g2d); 79 } 80 81 static void testLine(Graphics2D g2d) { 82 testLine(g2d, MinX, MinY, MaxX, MaxY); 83 testLine(g2d, MaxX, MinY, MinX, MaxY); 84 testLine(g2d, MinX, MaxY, MaxX, MinY); 85 testLine(g2d, MaxX, MaxY, MinX, MinY); 86 } 87 88 static void testRect(Graphics2D g2d) { 89 g2d.fillRect(MinX, MinY, MaxX - MinX, MaxY - MinY); 90 g2d.fill(new Rectangle(MinX, MinY, MaxX - MinX, MaxY - MinY)); 91 } 92 93 static void testLine(Graphics2D g2d, int x1, int y1, int x2, int y2) { 94 g2d.drawLine(x1, y1, x2, y2); 95 g2d.draw(new Line2D.Double(x1, y1, x2, y2)); 96 } 97 98 static class BoundsCheckerPaint implements Paint { 99 private Color c = Color.WHITE; 100 private Rectangle2D expectedBounds; 101 102 public BoundsCheckerPaint(double x1, double y1, 103 double x2, double y2) 104 { 105 expectedBounds = new Rectangle2D.Double(); 106 expectedBounds.setFrameFromDiagonal(x1, y1, x2, y2); 107 } 108 109 public int getTransparency() { 110 return c.getTransparency(); 111 } 112 113 public PaintContext createContext(ColorModel cm, 114 Rectangle deviceBounds, 115 Rectangle2D userBounds, 116 AffineTransform xform, 117 RenderingHints hints) 118 { 119 System.out.println("user bounds = "+userBounds); 120 if (!userBounds.equals(expectedBounds)) { 121 throw new RuntimeException("bounds fail to match"); 122 } 123 return c.createContext(cm, deviceBounds, userBounds, xform, hints); 124 } 125 } 126} 127