Path: blob/master/test/jdk/javax/swing/JComponent/6989617/bug6989617.java
41153 views
/*1* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 698961727* @summary Enable JComponent to control repaintings of its children28* @author Alexander Potochkin29* @run main bug698961730*/3132import javax.swing.*;33import java.awt.*;3435public class bug6989617 {36private static MyPanel panel;37private static JButton button;38private static JFrame frame;3940public static void main(String... args) throws Exception {41try {42Robot robot = new Robot();43SwingUtilities.invokeAndWait(new Runnable() {44public void run() {45frame = new JFrame();46frame. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);47panel = new MyPanel();4849button = new JButton("Hello");50panel.add(button);51frame.add(panel);5253frame.setSize(200, 300);54frame.setVisible(true);55}56});57// Testing the panel as a painting origin,58// the panel.paintImmediately() must be triggered59// when button.repaint() is called60robot.waitForIdle();61SwingUtilities.invokeAndWait(new Runnable() {62public void run() {63panel.resetPaintRectangle();64button.repaint();65}66});67robot.waitForIdle();68SwingUtilities.invokeAndWait(new Runnable() {69public void run() {70Rectangle pr = panel.getPaintRectangle();71if (!pr.getSize().equals(button.getSize())) {72throw new RuntimeException("wrong size of the dirty area");73}74if (!pr.getLocation().equals(button.getLocation())) {75throw new RuntimeException("wrong location of the dirty area");76}77}78});79// Testing the panel as NOT a painting origin80// the panel.paintImmediately() must NOT be triggered81// when button.repaint() is called82robot.waitForIdle();83SwingUtilities.invokeAndWait(new Runnable() {84public void run() {85panel.resetPaintRectangle();86panel.setPaintingOrigin(false);87if (panel.getPaintRectangle() != null) {88throw new RuntimeException("paint rectangle is not null");89}90button.repaint();91}92});93robot.waitForIdle();94SwingUtilities.invokeAndWait(new Runnable() {95public void run() {96if(panel.getPaintRectangle() != null) {97throw new RuntimeException("paint rectangle is not null");98}99System.out.println("Test passed...");100}101});102} finally {103if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());104}105}106107static class MyPanel extends JPanel {108private boolean isPaintingOrigin = true;109private Rectangle paintRectangle;110111{112setLayout(new GridBagLayout());113}114115public boolean isPaintingOrigin() {116return isPaintingOrigin;117}118119public void setPaintingOrigin(boolean paintingOrigin) {120isPaintingOrigin = paintingOrigin;121}122123public void paintImmediately(int x, int y, int w, int h) {124super.paintImmediately(x, y, w, h);125paintRectangle = new Rectangle(x, y, w, h);126}127128public Rectangle getPaintRectangle() {129return paintRectangle == null? null: new Rectangle(paintRectangle);130}131132public void resetPaintRectangle() {133this.paintRectangle = null;134}135}136}137138139