1 /* 2 * Copyright (c) 2011, 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 * @test 26 * @key headful 27 * @bug 6989617 28 * @summary Enable JComponent to control repaintings of its children 29 * @author Alexander Potochkin 30 * @run main bug6989617 31 */ 32 33 import sun.awt.SunToolkit; 34 35 import javax.swing.*; 36 import java.awt.*; 37 38 public class bug6989617 { 39 private static MyPanel panel; 40 private static JButton button; 41 42 public static void main(String... args) throws Exception { 43 SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 44 SwingUtilities.invokeAndWait(new Runnable() { 45 public void run() { 46 JFrame frame = new JFrame(); 47 frame. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 48 panel = new MyPanel(); 49 50 button = new JButton("Hello"); 51 panel.add(button); 52 frame.add(panel); 53 54 frame.setSize(200, 300); 55 frame.setVisible(true); 56 } 57 }); 58 // Testing the panel as a painting origin, 59 // the panel.paintImmediately() must be triggered 60 // when button.repaint() is called 61 toolkit.realSync(); 62 SwingUtilities.invokeAndWait(new Runnable() { 63 public void run() { 64 panel.resetPaintRectangle(); 65 button.repaint(); 66 } 67 }); 68 toolkit.realSync(); 69 SwingUtilities.invokeAndWait(new Runnable() { 70 public void run() { 71 Rectangle pr = panel.getPaintRectangle(); 72 if (!pr.getSize().equals(button.getSize())) { 73 throw new RuntimeException("wrong size of the dirty area"); 74 } 75 if (!pr.getLocation().equals(button.getLocation())) { 76 throw new RuntimeException("wrong location of the dirty area"); 77 } 78 } 79 }); 80 // Testing the panel as NOT a painting origin 81 // the panel.paintImmediately() must NOT be triggered 82 // when button.repaint() is called 83 toolkit.realSync(); 84 SwingUtilities.invokeAndWait(new Runnable() { 85 public void run() { 86 panel.resetPaintRectangle(); 87 panel.setPaintingOrigin(false); 88 if (panel.getPaintRectangle() != null) { 89 throw new RuntimeException("paint rectangle is not null"); 90 } 91 button.repaint(); 92 } 93 }); 94 toolkit.realSync(); 95 SwingUtilities.invokeAndWait(new Runnable() { 96 public void run() { 97 if(panel.getPaintRectangle() != null) { 98 throw new RuntimeException("paint rectangle is not null"); 99 } 100 System.out.println("Test passed..."); 101 } 102 }); 103 } 104 105 static class MyPanel extends JPanel { 106 private boolean isPaintingOrigin = true; 107 private Rectangle paintRectangle; 108 109 { 110 setLayout(new GridBagLayout()); 111 } 112 113 public boolean isPaintingOrigin() { 114 return isPaintingOrigin; 115 } 116 117 public void setPaintingOrigin(boolean paintingOrigin) { 118 isPaintingOrigin = paintingOrigin; 119 } 120 121 public void paintImmediately(int x, int y, int w, int h) { 122 super.paintImmediately(x, y, w, h); 123 paintRectangle = new Rectangle(x, y, w, h); 124 } 125 126 public Rectangle getPaintRectangle() { 127 return paintRectangle == null? null: new Rectangle(paintRectangle); 128 } 129 130 public void resetPaintRectangle() { 131 this.paintRectangle = null; 132 } 133 } 134 }