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