1 /* 2 * Copyright (c) 2013, 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 import java.awt.Graphics2D; 25 import java.awt.Rectangle; 26 import java.awt.Shape; 27 import java.awt.image.BufferedImage; 28 29 import sun.java2d.SunGraphics2D; 30 31 /** 32 * @test 33 * @bug 8004859 34 * @summary getClipBounds/getClip should return equivalent bounds. 35 * @author Sergey Bylokhov 36 */ 37 public final class Test8004859 { 38 39 private static Shape[] clips = {new Rectangle(0, 0, 1, 1), new Rectangle( 40 100, 100, -100, -100)}; 41 42 private static boolean status = true; 43 44 public static void main(final String[] args) { 45 final BufferedImage bi = new BufferedImage(300, 300, 46 BufferedImage.TYPE_INT_RGB); 47 final Graphics2D g = (Graphics2D) bi.getGraphics(); 48 test(g); 49 g.translate(2.0, 2.0); 50 test(g); 51 g.scale(2.0, 2.0); 52 test(g); 53 g.rotate(Math.toRadians(90)); 54 test(g); 55 g.rotate(Math.toRadians(90)); 56 test(g); 57 g.rotate(Math.toRadians(90)); 58 test(g); 59 g.rotate(Math.toRadians(90)); 60 test(g); 61 g.dispose(); 62 if (!status) { 63 throw new RuntimeException("Test failed"); 64 } 65 } 66 67 private static void test(final Graphics2D g) { 68 for (final Shape clip : clips) { 69 g.setClip(clip); 70 if (!g.getClip().equals(clip)) { 71 System.err.println("Expected clip: " + clip); 72 System.err.println("Actual clip: " + g.getClip()); 73 status = false; 74 } 75 final Rectangle bounds = g.getClipBounds(); 76 if (!clip.equals(bounds)) { 77 System.err.println("Expected getClipBounds(): " + clip); 78 System.err.println("Actual getClipBounds(): " + bounds); 79 status = false; 80 } 81 g.getClipBounds(bounds); 82 if (!clip.equals(bounds)) { 83 System.err.println("Expected getClipBounds(r): " + clip); 84 System.err.println("Actual getClipBounds(r): " + bounds); 85 status = false; 86 } 87 if (!clip.getBounds2D().isEmpty() && ((SunGraphics2D) g).clipRegion 88 .isEmpty()) { 89 System.err.println("clipRegion should not be empty"); 90 status = false; 91 } 92 } 93 } 94 }