1 /* 2 * Copyright (c) 2016, 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 8139581 28 * @summary AWT components are not drawn after removal and addition to a container 29 * @author Anton Litvinov 30 */ 31 32 import java.awt.Button; 33 import java.awt.Color; 34 import java.awt.Canvas; 35 import java.awt.Frame; 36 import java.awt.Graphics; 37 import java.awt.Panel; 38 import java.util.ArrayList; 39 40 public class ComponentIsNotDrawnAfterRemoveAddTest { 41 private final Frame frame; 42 private final Panel panel; 43 private final ArrayList<Testable> compList = new ArrayList<Testable>(); 44 45 public ComponentIsNotDrawnAfterRemoveAddTest() { 46 frame = new Frame("ComponentIsNotDrawnAfterRemoveAddTest"); 47 frame.setSize(500, 500); 48 frame.setLocation(200, 200); 49 frame.setLayout(null); 50 frame.setBackground(Color.RED); 51 52 panel = new Panel(); 53 panel.setLayout(null); 54 panel.setBounds(25, 100, 455, 295); 55 panel.setBackground(Color.GREEN); 56 57 for (int i = 0; i < 10; i++) { 58 TestCanvas canv1 = new TestCanvas(); 59 canv1.setBounds(i * 45 + 5, 15, 30 + i, 30 + i); 60 panel.add(canv1); 61 compList.add(canv1); 62 63 TestButton btn1 = new TestButton(); 64 btn1.setBounds(i * 45 + 5, 60, 30 + i, 30 + i); 65 panel.add(btn1); 66 compList.add(btn1); 67 68 TestCanvas canv2 = new TestCanvas(); 69 canv2.setBounds(i * 45 + 5, 105, 30 + i, 30 + i); 70 panel.add(canv2); 71 compList.add(canv2); 72 73 TestButton btn2 = new TestButton(); 74 btn2.setBounds(i * 45 + 5, 150, 30 + i, 30 + i); 75 panel.add(btn2); 76 compList.add(btn2); 77 78 TestCanvas canv3 = new TestCanvas(); 79 canv3.setBounds(i * 45 + 5, 195, 30 + i, 30 + i); 80 panel.add(canv3); 81 compList.add(canv3); 82 83 TestButton btn3 = new TestButton(); 84 btn3.setBounds(i * 45 + 5, 240, 30 + i, 30 + i); 85 panel.add(btn3); 86 compList.add(btn3); 87 } 88 89 frame.add(panel); 90 frame.setVisible(true); 91 } 92 93 private void runTest() { 94 try { 95 doSleep(1500); 96 checkTestableComponents(); 97 98 for (int i = 0; i < 5; i++) { 99 System.err.println(String.format("Test iteration #%d:", i)); 100 101 frame.remove(panel); 102 frame.invalidate(); 103 frame.validate(); 104 frame.add(panel); 105 106 doSleep(1500); 107 checkTestableComponents(); 108 } 109 } finally { 110 frame.dispose(); 111 } 112 } 113 114 private void doSleep(long millis) { 115 try { 116 Thread.sleep(millis); 117 } catch (InterruptedException ie) { 118 ie.printStackTrace(); 119 } 120 } 121 122 private void checkTestableComponents() throws RuntimeException { 123 int notDrawnCompsCount = 0; 124 for (Testable comp : compList) { 125 if (!comp.wasPaintCalled()) { 126 notDrawnCompsCount++; 127 } else { 128 comp.resetPaintCalledFlag(); 129 } 130 } 131 if (notDrawnCompsCount > 0) { 132 throw new RuntimeException(String.format( 133 "'paint' method of %d components was not called.", notDrawnCompsCount)); 134 } 135 } 136 137 private interface Testable { 138 boolean wasPaintCalled(); 139 void resetPaintCalledFlag(); 140 } 141 142 private static class TestCanvas extends Canvas implements Testable { 143 private volatile boolean paintWasCalled = false; 144 145 @Override 146 public void paint(Graphics g) { 147 paintWasCalled = true; 148 super.paint(g); 149 g.setColor(Color.BLUE); 150 g.fillRect(0, 0, getWidth(), getHeight()); 151 } 152 153 @Override 154 public boolean wasPaintCalled() { 155 return paintWasCalled; 156 } 157 158 @Override 159 public void resetPaintCalledFlag() { 160 paintWasCalled = false; 161 } 162 } 163 164 private static class TestButton extends Button implements Testable { 165 private volatile boolean paintWasCalled = false; 166 167 @Override 168 public void paint(Graphics g) { 169 paintWasCalled = true; 170 super.paint(g); 171 g.setColor(Color.YELLOW); 172 g.fillRect(0, 0, 15, 15); 173 } 174 175 @Override 176 public boolean wasPaintCalled() { 177 return paintWasCalled; 178 } 179 180 @Override 181 public void resetPaintCalledFlag() { 182 paintWasCalled = false; 183 } 184 } 185 186 public static void main(String[] args) { 187 new ComponentIsNotDrawnAfterRemoveAddTest().runTest(); 188 } 189 }