1 /* 2 * Copyright (c) 2013, 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 25 import java.awt.Graphics; 26 import java.awt.Toolkit; 27 import java.awt.image.BufferedImage; 28 import java.lang.reflect.InvocationTargetException; 29 30 import javax.swing.JButton; 31 import javax.swing.JFrame; 32 import javax.swing.SwingUtilities; 33 34 import sun.awt.SunToolkit; 35 36 /** 37 * @test 38 * @bug 8009919 39 * @author Sergey Bylokhov 40 */ 41 public final class JButtonPaintNPE { 42 43 private static JFrame frame; 44 45 public static void main(final String[] args) 46 throws InvocationTargetException, InterruptedException { 47 SwingUtilities.invokeAndWait(() -> { 48 frame = new JFrame(); 49 frame.add(new JButton() { 50 @Override 51 protected void paintComponent(final Graphics g) { 52 Graphics gg = new BufferedImage(getWidth(), getHeight(), 53 BufferedImage.TYPE_INT_ARGB).createGraphics(); 54 super.paintComponent(gg); 55 gg.dispose(); 56 } 57 }); 58 frame.setSize(300, 300); 59 frame.setLocationRelativeTo(null); 60 frame.setVisible(true); 61 }); 62 sleep(); 63 SwingUtilities.invokeAndWait(() -> { 64 if (frame != null) { 65 frame.dispose(); 66 } 67 }); 68 } 69 70 private static void sleep() { 71 try { 72 ((SunToolkit) Toolkit.getDefaultToolkit()).realSync(); 73 Thread.sleep(1000); 74 } catch (final InterruptedException ignored) { 75 } 76 } 77 }