1 /*
   2  * Copyright (c) 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.Component;
  25 import java.awt.Frame;
  26 import java.awt.Graphics;
  27 import java.awt.GraphicsConfiguration;
  28 import java.awt.GraphicsDevice;
  29 import java.awt.GraphicsEnvironment;
  30 import java.awt.Image;
  31 import java.awt.Window;
  32 import java.awt.image.BufferedImage;
  33 
  34 import sun.java2d.SunGraphics2D;
  35 
  36 import static java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment;
  37 import static java.awt.Transparency.BITMASK;
  38 import static java.awt.Transparency.OPAQUE;
  39 import static java.awt.Transparency.TRANSLUCENT;
  40 import static java.awt.image.BufferedImage.TYPE_INT_ARGB;
  41 
  42 /**
  43  * @test
  44  * @bug 8134603
  45  * @modules java.desktop/sun.java2d
  46  * @run main/othervm SurfaceDestination
  47  */
  48 public final class SurfaceDestination {
  49 
  50     public static void main(final String[] args) {
  51         final GraphicsEnvironment lge = getLocalGraphicsEnvironment();
  52         final GraphicsDevice dev = lge.getDefaultScreenDevice();
  53         final GraphicsConfiguration config = dev.getDefaultConfiguration();
  54 
  55         test(config.createCompatibleImage(10, 10).getGraphics());
  56         test(config.createCompatibleImage(10, 10, OPAQUE).getGraphics());
  57         test(config.createCompatibleImage(10, 10, BITMASK).getGraphics());
  58         test(config.createCompatibleImage(10, 10, TRANSLUCENT).getGraphics());
  59 
  60         test(new BufferedImage(10, 10, TYPE_INT_ARGB).getGraphics());
  61 
  62         final Window frame = new Frame();
  63         frame.pack();
  64         try {
  65             test(frame.getGraphics());
  66             test(frame.createImage(10, 10).getGraphics());
  67         } finally {
  68             frame.dispose();
  69         }
  70     }
  71 
  72     private static void test(final Graphics graphics) {
  73         try {
  74             if (graphics instanceof SunGraphics2D) {
  75                 final Object dst = ((SunGraphics2D) graphics).getDestination();
  76                 if (!(dst instanceof Image) && !(dst instanceof Component)) {
  77                     throw new RuntimeException("Wrong type:" + dst);
  78                 }
  79             }
  80         } finally {
  81             graphics.dispose();
  82         }
  83     }
  84 }