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.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  */
  38 public final class Test8004859 {
  39 
  40     private static Shape[] clips = {new Rectangle(0, 0, -1, -1), new Rectangle(
  41             100, 100, -100, -100)};
  42 
  43     private static boolean status = true;
  44 
  45     public static void main(final String[] args)
  46             throws NoninvertibleTransformException {
  47         final BufferedImage bi = new BufferedImage(300, 300,
  48                                                    BufferedImage.TYPE_INT_RGB);
  49         final Graphics2D g = (Graphics2D) bi.getGraphics();
  50         test(g);
  51         g.translate(2.0, 2.0);
  52         test(g);
  53         g.translate(-4.0, -4.0);
  54         test(g);
  55         g.scale(2.0, 2.0);
  56         test(g);
  57         g.scale(-4.0, -4.0);
  58         test(g);
  59         g.rotate(Math.toRadians(90));
  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.dispose();
  68         if (!status) {
  69             throw new RuntimeException("Test failed");
  70         }
  71     }
  72 
  73     private static void test(final Graphics2D g) {
  74         for (final Shape clip : clips) {
  75             g.setClip(clip);
  76             if (!g.getClip().equals(clip)) {
  77                 System.err.println("Expected clip: " + clip);
  78                 System.err.println("Actual clip: " + g.getClip());
  79                 System.err.println("bounds="+g.getClip().getBounds2D());
  80                 System.err.println("bounds="+g.getClip().getBounds());
  81                 status = false;
  82             }
  83             final Rectangle bounds = g.getClipBounds();
  84             if (!clip.equals(bounds)) {
  85                 System.err.println("Expected getClipBounds(): " + clip);
  86                 System.err.println("Actual getClipBounds(): " + bounds);
  87                 status = false;
  88             }
  89             g.getClipBounds(bounds);
  90             if (!clip.equals(bounds)) {
  91                 System.err.println("Expected getClipBounds(r): " + clip);
  92                 System.err.println("Actual getClipBounds(r): " + bounds);
  93                 status = false;
  94             }
  95             if (!clip.getBounds2D().isEmpty() && ((SunGraphics2D) g).clipRegion
  96                     .isEmpty()) {
  97                 System.err.println("clipRegion should not be empty");
  98                 status = false;
  99             }
 100         }
 101     }
 102 }