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.AWTException; 25 import java.awt.Button; 26 import java.awt.Component; 27 import java.awt.EventQueue; 28 import java.awt.Frame; 29 import java.awt.GraphicsEnvironment; 30 31 import javax.swing.JButton; 32 33 /** 34 * @test 35 * @bug 6815345 36 * @key headful 37 * @run main CreateImage 38 * @run main/othervm -Djava.awt.headless=true CreateImage 39 */ 40 public final class CreateImage { 41 42 public static void main(final String[] args) throws Exception { 43 EventQueue.invokeAndWait(CreateImage::test); 44 } 45 46 private static void test() { 47 final JButton jbutton1 = new JButton(); 48 final JButton jbutton2 = new JButton(); 49 50 if (GraphicsEnvironment.isHeadless()) { 51 checkCreateImage(jbutton1, true); 52 checkCreateImage(jbutton2, true); 53 return; 54 } 55 56 final Frame frame = new Frame(); 57 final Button button1 = new Button(); 58 final Button button2 = new Button(); 59 try { 60 // all components are not displayable 61 checkCreateImage(frame, true); 62 checkCreateImage(button1, true); 63 checkCreateImage(button2, true); 64 checkCreateImage(jbutton1, true); 65 checkCreateImage(jbutton2, true); 66 67 // some components added to the non-displayable frame 68 frame.add(button1); 69 frame.add(jbutton1); 70 checkCreateImage(button1, true); 71 checkCreateImage(jbutton1, true); 72 frame.pack(); 73 74 // tests previously added components when the frame is displayable 75 checkCreateImage(frame, false); 76 checkCreateImage(button1, false); 77 checkCreateImage(jbutton1, false); 78 79 // some components added to the displayable frame 80 frame.add(button2); 81 frame.add(jbutton2); 82 checkCreateImage(button2, false); 83 checkCreateImage(jbutton2, false); 84 85 } finally { 86 frame.dispose(); 87 } 88 // tests all components after the frame became non-displayable again 89 checkCreateImage(frame, true); 90 checkCreateImage(button1, true); 91 checkCreateImage(button2, true); 92 checkCreateImage(jbutton1, true); 93 checkCreateImage(jbutton2, true); 94 } 95 96 private static void checkCreateImage(final Component comp, 97 final boolean isNull) { 98 if ((comp.createImage(10, 10) != null) == isNull) { 99 throw new RuntimeException("Image is wrong"); 100 } 101 if ((comp.createVolatileImage(10, 10) != null) == isNull) { 102 throw new RuntimeException("Image is wrong"); 103 } 104 try { 105 if ((comp.createVolatileImage(10, 10, null) != null) == isNull) { 106 throw new RuntimeException("Image is wrong"); 107 } 108 } catch (final AWTException ignored) { 109 // this check is not applicable 110 } 111 } 112 }