Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Paint/PgramUserBoundsTest.java
47490 views
/*1* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 704305426* @summary Verifies that Paint objects receive the appropriate user space27* bounds in their createContext() method28* @run main PgramUserBoundsTest29*/3031import java.awt.Color;32import java.awt.Graphics2D;33import java.awt.Paint;34import java.awt.PaintContext;35import java.awt.RenderingHints;36import java.awt.Rectangle;37import java.awt.geom.AffineTransform;38import java.awt.geom.Line2D;39import java.awt.geom.Rectangle2D;40import java.awt.image.BufferedImage;41import java.awt.image.ColorModel;4243public class PgramUserBoundsTest {44static final int MinX = 10;45static final int MinY = 20;46static final int MaxX = 30;47static final int MaxY = 50;48static AffineTransform identity = new AffineTransform();4950public static void main(String argv[]) {51BufferedImage bimg =52new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);53Graphics2D g2d = bimg.createGraphics();54g2d.setPaint(new BoundsCheckerPaint(MinX, MinY, MaxX, MaxY));55testAll(g2d);56g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,57RenderingHints.VALUE_ANTIALIAS_ON);58testAll(g2d);59}6061static void testAll(Graphics2D g2d) {62g2d.setTransform(identity);63g2d.translate(100, 100);64testPrimitives(g2d);6566g2d.setTransform(identity);67g2d.scale(10, 10);68testPrimitives(g2d);6970g2d.setTransform(identity);71g2d.rotate(Math.PI/6);72testPrimitives(g2d);73}7475static void testPrimitives(Graphics2D g2d) {76testLine(g2d);77testRect(g2d);78}7980static void testLine(Graphics2D g2d) {81testLine(g2d, MinX, MinY, MaxX, MaxY);82testLine(g2d, MaxX, MinY, MinX, MaxY);83testLine(g2d, MinX, MaxY, MaxX, MinY);84testLine(g2d, MaxX, MaxY, MinX, MinY);85}8687static void testRect(Graphics2D g2d) {88g2d.fillRect(MinX, MinY, MaxX - MinX, MaxY - MinY);89g2d.fill(new Rectangle(MinX, MinY, MaxX - MinX, MaxY - MinY));90}9192static void testLine(Graphics2D g2d, int x1, int y1, int x2, int y2) {93g2d.drawLine(x1, y1, x2, y2);94g2d.draw(new Line2D.Double(x1, y1, x2, y2));95}9697static class BoundsCheckerPaint implements Paint {98private Color c = Color.WHITE;99private Rectangle2D expectedBounds;100101public BoundsCheckerPaint(double x1, double y1,102double x2, double y2)103{104expectedBounds = new Rectangle2D.Double();105expectedBounds.setFrameFromDiagonal(x1, y1, x2, y2);106}107108public int getTransparency() {109return c.getTransparency();110}111112public PaintContext createContext(ColorModel cm,113Rectangle deviceBounds,114Rectangle2D userBounds,115AffineTransform xform,116RenderingHints hints)117{118System.out.println("user bounds = "+userBounds);119if (!userBounds.equals(expectedBounds)) {120throw new RuntimeException("bounds fail to match");121}122return c.createContext(cm, deviceBounds, userBounds, xform, hints);123}124}125}126127128