Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JComponent/6989617/bug6989617.java
38854 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/* @test24@bug 698961725@summary Enable JComponent to control repaintings of its children26@author Alexander Potochkin27@run main bug698961728*/2930import javax.swing.*;31import java.awt.*;3233public class bug6989617 {34private static MyPanel panel;35private static JButton button;3637public static void main(String... args) throws Exception {38Robot robot = new Robot();39SwingUtilities.invokeAndWait(new Runnable() {40public void run() {41JFrame frame = new JFrame();42frame. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);43panel = new MyPanel();4445button = new JButton("Hello");46panel.add(button);47frame.add(panel);4849frame.setSize(200, 300);50frame.setVisible(true);51}52});53// Testing the panel as a painting origin,54// the panel.paintImmediately() must be triggered55// when button.repaint() is called56robot.waitForIdle();57SwingUtilities.invokeAndWait(new Runnable() {58public void run() {59panel.resetPaintRectangle();60button.repaint();61}62});63robot.waitForIdle();64SwingUtilities.invokeAndWait(new Runnable() {65public void run() {66Rectangle pr = panel.getPaintRectangle();67if (!pr.getSize().equals(button.getSize())) {68throw new RuntimeException("wrong size of the dirty area");69}70if (!pr.getLocation().equals(button.getLocation())) {71throw new RuntimeException("wrong location of the dirty area");72}73}74});75// Testing the panel as NOT a painting origin76// the panel.paintImmediately() must NOT be triggered77// when button.repaint() is called78robot.waitForIdle();79SwingUtilities.invokeAndWait(new Runnable() {80public void run() {81panel.resetPaintRectangle();82panel.setPaintingOrigin(false);83if (panel.getPaintRectangle() != null) {84throw new RuntimeException("paint rectangle is not null");85}86button.repaint();87}88});89robot.waitForIdle();90SwingUtilities.invokeAndWait(new Runnable() {91public void run() {92if(panel.getPaintRectangle() != null) {93throw new RuntimeException("paint rectangle is not null");94}95System.out.println("Test passed...");96}97});98}99100static class MyPanel extends JPanel {101private boolean isPaintingOrigin = true;102private Rectangle paintRectangle;103104{105setLayout(new GridBagLayout());106}107108public boolean isPaintingOrigin() {109return isPaintingOrigin;110}111112public void setPaintingOrigin(boolean paintingOrigin) {113isPaintingOrigin = paintingOrigin;114}115116public void paintImmediately(int x, int y, int w, int h) {117super.paintImmediately(x, y, w, h);118paintRectangle = new Rectangle(x, y, w, h);119}120121public Rectangle getPaintRectangle() {122return paintRectangle == null? null: new Rectangle(paintRectangle);123}124125public void resetPaintRectangle() {126this.paintRectangle = null;127}128}129}130131132