Path: blob/master/test/jdk/java/awt/Component/CreateImage/CreateImage.java
41153 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.AWTException;24import java.awt.Button;25import java.awt.Component;26import java.awt.EventQueue;27import java.awt.Frame;28import java.awt.GraphicsEnvironment;2930import javax.swing.JButton;3132/**33* @test34* @bug 681534535* @run main CreateImage36* @run main/othervm -Djava.awt.headless=true CreateImage37*/38public final class CreateImage {3940public static void main(final String[] args) throws Exception {41EventQueue.invokeAndWait(CreateImage::test);42}4344private static void test() {45final JButton jbutton1 = new JButton();46final JButton jbutton2 = new JButton();4748if (GraphicsEnvironment.isHeadless()) {49checkCreateImage(jbutton1, true);50checkCreateImage(jbutton2, true);51return;52}5354final Frame frame = new Frame();55final Button button1 = new Button();56final Button button2 = new Button();57try {58// all components are not displayable59checkCreateImage(frame, true);60checkCreateImage(button1, true);61checkCreateImage(button2, true);62checkCreateImage(jbutton1, true);63checkCreateImage(jbutton2, true);6465// some components added to the non-displayable frame66frame.add(button1);67frame.add(jbutton1);68checkCreateImage(button1, true);69checkCreateImage(jbutton1, true);70frame.pack();7172// tests previously added components when the frame is displayable73checkCreateImage(frame, false);74checkCreateImage(button1, false);75checkCreateImage(jbutton1, false);7677// some components added to the displayable frame78frame.add(button2);79frame.add(jbutton2);80checkCreateImage(button2, false);81checkCreateImage(jbutton2, false);8283} finally {84frame.dispose();85}86// tests all components after the frame became non-displayable again87checkCreateImage(frame, true);88checkCreateImage(button1, true);89checkCreateImage(button2, true);90checkCreateImage(jbutton1, true);91checkCreateImage(jbutton2, true);92}9394private static void checkCreateImage(final Component comp,95final boolean isNull) {96if ((comp.createImage(10, 10) != null) == isNull) {97throw new RuntimeException("Image is wrong");98}99if ((comp.createVolatileImage(10, 10) != null) == isNull) {100throw new RuntimeException("Image is wrong");101}102try {103if ((comp.createVolatileImage(10, 10, null) != null) == isNull) {104throw new RuntimeException("Image is wrong");105}106} catch (final AWTException ignored) {107// this check is not applicable108}109}110}111112113