1 /*
   2  * Copyright (c) 2012, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 import java.awt.Graphics2D;
  27 import java.awt.Rectangle;
  28 import java.awt.Shape;
  29 import java.awt.image.BufferedImage;
  30 
  31 import sun.java2d.SunGraphics2D;
  32 
  33 /**
  34  * @test
  35  * @bug 8004859
  36  * @summary getClipBounds/getClip should return equivalent bounds.
  37  * @author Sergey Bylokhov
  38  */
  39 public final class Test8004859 {
  40 
  41     private static Shape[] clips = {new Rectangle(0, 0, 1, 1), new Rectangle(
  42             100, 100, -100, -100)};
  43 
  44     private static boolean status = true;
  45 
  46     public static void main(final String[] args) {
  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.scale(2.0, 2.0);
  54         test(g);
  55         g.dispose();
  56         if (!status) {
  57             throw new RuntimeException("Test failed");
  58         }
  59     }
  60 
  61     private static void test(final Graphics2D g) {
  62         for (final Shape clip : clips) {
  63             g.setClip(clip);
  64             if (!g.getClip().equals(clip)) {
  65                 System.err.println("Expected clip: " + clip);
  66                 System.err.println("Actual clip: " + g.getClip());
  67                 status = false;
  68             }
  69             final Rectangle bounds = g.getClipBounds();
  70             if (!clip.equals(bounds)) {
  71                 System.err.println("Expected getClipBounds(): " + clip);
  72                 System.err.println("Actual getClipBounds(): " + bounds);
  73                 status = false;
  74             }
  75             g.getClipBounds(bounds);
  76             if (!clip.equals(bounds)) {
  77                 System.err.println("Expected getClipBounds(r): " + clip);
  78                 System.err.println("Actual getClipBounds(r): " + bounds);
  79                 status = false;
  80             }
  81             if (!clip.getBounds2D().isEmpty() && ((SunGraphics2D) g).clipRegion
  82                     .isEmpty()) {
  83                 System.err.println("clipRegion should not be empty");
  84                 status = false;
  85             }
  86         }
  87     }
  88 }