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