4190N/A/*
4190N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4190N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4190N/A *
4190N/A * This code is free software; you can redistribute it and/or modify it
4190N/A * under the terms of the GNU General Public License version 2 only, as
4190N/A * published by the Free Software Foundation.
4190N/A *
4190N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4190N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4190N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4190N/A * version 2 for more details (a copy is included in the LICENSE file that
4190N/A * accompanied this code).
4190N/A *
4190N/A * You should have received a copy of the GNU General Public License version
4190N/A * 2 along with this work; if not, write to the Free Software Foundation,
4190N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4190N/A *
4190N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4190N/A * or visit www.oracle.com if you need additional information or have any
4190N/A * questions.
4190N/A */
4190N/A
4190N/A/**
4190N/A * @test
4190N/A * @bug 7043054
4190N/A * @summary Verifies that Paint objects receive the appropriate user space
4190N/A * bounds in their createContext() method
4190N/A * @run main PgramUserBoundsTest
4190N/A */
4190N/A
4190N/Aimport java.awt.Color;
4190N/Aimport java.awt.Graphics2D;
4190N/Aimport java.awt.Paint;
4190N/Aimport java.awt.PaintContext;
4190N/Aimport java.awt.RenderingHints;
4190N/Aimport java.awt.Rectangle;
4190N/Aimport java.awt.geom.AffineTransform;
4190N/Aimport java.awt.geom.Line2D;
4190N/Aimport java.awt.geom.Rectangle2D;
4190N/Aimport java.awt.image.BufferedImage;
4190N/Aimport java.awt.image.ColorModel;
4190N/A
4190N/Apublic class PgramUserBoundsTest {
4190N/A static final int MinX = 10;
4190N/A static final int MinY = 20;
4190N/A static final int MaxX = 30;
4190N/A static final int MaxY = 50;
4190N/A static AffineTransform identity = new AffineTransform();
4190N/A
4190N/A public static void main(String argv[]) {
4190N/A BufferedImage bimg =
4190N/A new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
4190N/A Graphics2D g2d = bimg.createGraphics();
4190N/A g2d.setPaint(new BoundsCheckerPaint(MinX, MinY, MaxX, MaxY));
4190N/A testAll(g2d);
4190N/A g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
4190N/A RenderingHints.VALUE_ANTIALIAS_ON);
4190N/A testAll(g2d);
4190N/A }
4190N/A
4190N/A static void testAll(Graphics2D g2d) {
4190N/A g2d.setTransform(identity);
4190N/A g2d.translate(100, 100);
4190N/A testPrimitives(g2d);
4190N/A
4190N/A g2d.setTransform(identity);
4190N/A g2d.scale(10, 10);
4190N/A testPrimitives(g2d);
4190N/A
4190N/A g2d.setTransform(identity);
4190N/A g2d.rotate(Math.PI/6);
4190N/A testPrimitives(g2d);
4190N/A }
4190N/A
4190N/A static void testPrimitives(Graphics2D g2d) {
4190N/A testLine(g2d);
4190N/A testRect(g2d);
4190N/A }
4190N/A
4190N/A static void testLine(Graphics2D g2d) {
4190N/A testLine(g2d, MinX, MinY, MaxX, MaxY);
4190N/A testLine(g2d, MaxX, MinY, MinX, MaxY);
4190N/A testLine(g2d, MinX, MaxY, MaxX, MinY);
4190N/A testLine(g2d, MaxX, MaxY, MinX, MinY);
4190N/A }
4190N/A
4190N/A static void testRect(Graphics2D g2d) {
4190N/A g2d.fillRect(MinX, MinY, MaxX - MinX, MaxY - MinY);
4190N/A g2d.fill(new Rectangle(MinX, MinY, MaxX - MinX, MaxY - MinY));
4190N/A }
4190N/A
4190N/A static void testLine(Graphics2D g2d, int x1, int y1, int x2, int y2) {
4190N/A g2d.drawLine(x1, y1, x2, y2);
4190N/A g2d.draw(new Line2D.Double(x1, y1, x2, y2));
4190N/A }
4190N/A
4190N/A static class BoundsCheckerPaint implements Paint {
4190N/A private Color c = Color.WHITE;
4190N/A private Rectangle2D expectedBounds;
4190N/A
4190N/A public BoundsCheckerPaint(double x1, double y1,
4190N/A double x2, double y2)
4190N/A {
4190N/A expectedBounds = new Rectangle2D.Double();
4190N/A expectedBounds.setFrameFromDiagonal(x1, y1, x2, y2);
4190N/A }
4190N/A
4190N/A public int getTransparency() {
4190N/A return c.getTransparency();
4190N/A }
4190N/A
4190N/A public PaintContext createContext(ColorModel cm,
4190N/A Rectangle deviceBounds,
4190N/A Rectangle2D userBounds,
4190N/A AffineTransform xform,
4190N/A RenderingHints hints)
4190N/A {
4190N/A System.out.println("user bounds = "+userBounds);
4190N/A if (!userBounds.equals(expectedBounds)) {
4190N/A throw new RuntimeException("bounds fail to match");
4190N/A }
4190N/A return c.createContext(cm, deviceBounds, userBounds, xform, hints);
4190N/A }
4190N/A }
4190N/A}