SurfaceDestination.java revision 14851:980da45565c8
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
24import java.awt.Component;
25import java.awt.Frame;
26import java.awt.Graphics;
27import java.awt.GraphicsConfiguration;
28import java.awt.GraphicsDevice;
29import java.awt.GraphicsEnvironment;
30import java.awt.Image;
31import java.awt.Window;
32import java.awt.image.BufferedImage;
33
34import sun.java2d.SunGraphics2D;
35
36import static java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment;
37import static java.awt.Transparency.BITMASK;
38import static java.awt.Transparency.OPAQUE;
39import static java.awt.Transparency.TRANSLUCENT;
40import 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 */
49public 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}
86