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