|
| 1 | +/* |
| 2 | + * Copyright (c) 2000, 2025, 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 | + * @bug 4247610 |
| 27 | + * @summary Tests an unnecessary repaint issue |
| 28 | + * @key headful |
| 29 | + * @run main bug4247610 |
| 30 | + */ |
| 31 | + |
| 32 | +import java.awt.Dimension; |
| 33 | +import java.awt.FlowLayout; |
| 34 | +import java.awt.Graphics; |
| 35 | +import java.awt.Point; |
| 36 | +import java.awt.Robot; |
| 37 | +import java.awt.event.InputEvent; |
| 38 | +import javax.swing.JButton; |
| 39 | +import javax.swing.JDesktopPane; |
| 40 | +import javax.swing.JFrame; |
| 41 | +import javax.swing.JInternalFrame; |
| 42 | +import javax.swing.JLabel; |
| 43 | +import javax.swing.JPanel; |
| 44 | +import javax.swing.SwingUtilities; |
| 45 | + |
| 46 | +import java.util.concurrent.atomic.AtomicInteger; |
| 47 | +import java.util.Random; |
| 48 | + |
| 49 | +public class bug4247610 { |
| 50 | + |
| 51 | + private static JFrame frame; |
| 52 | + private static JButton damager; |
| 53 | + private static volatile Point loc; |
| 54 | + private static volatile Dimension size; |
| 55 | + private static volatile boolean traced; |
| 56 | + private static volatile boolean failed; |
| 57 | + |
| 58 | + public static void main(String[] args) throws Exception { |
| 59 | + Robot robot = new Robot(); |
| 60 | + SwingUtilities.invokeAndWait(() -> { |
| 61 | + frame = new JFrame("bug4247610"); |
| 62 | + JDesktopPane pane = new JDesktopPane(); |
| 63 | + |
| 64 | + JInternalFrame jif = new JInternalFrame( |
| 65 | + "Damager", true, true, true, true); |
| 66 | + InternalFramePanel ifp = new InternalFramePanel(); |
| 67 | + damager = new JButton("Damage!"); |
| 68 | + ifp.add(damager); |
| 69 | + jif.setContentPane(ifp); |
| 70 | + jif.setBounds(0, 0, 300, 300); |
| 71 | + jif.setVisible(true); |
| 72 | + pane.add(jif); |
| 73 | + |
| 74 | + jif = new JInternalFrame("Damagee", true, true, true, true); |
| 75 | + JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); |
| 76 | + final JLabel damagee = new JLabel(""); |
| 77 | + panel.add(damagee); |
| 78 | + jif.setContentPane(panel); |
| 79 | + jif.setBounds(60, 220, 300, 100); |
| 80 | + jif.setVisible(true); |
| 81 | + pane.add(jif); |
| 82 | + |
| 83 | + final Random random = new Random(); |
| 84 | + |
| 85 | + damager.addActionListener((e) -> { |
| 86 | + System.out.println("trace paints enabled"); |
| 87 | + traced = true; |
| 88 | + damagee.setText(Integer.toString(random.nextInt())); |
| 89 | + }); |
| 90 | + frame.setContentPane(pane); |
| 91 | + frame.setSize(500, 500); |
| 92 | + frame.setLocationRelativeTo(null); |
| 93 | + frame.setVisible(true); |
| 94 | + }); |
| 95 | + robot.waitForIdle(); |
| 96 | + robot.delay(1000); |
| 97 | + SwingUtilities.invokeAndWait(() -> { |
| 98 | + loc = damager.getLocationOnScreen(); |
| 99 | + size = damager.getSize(); |
| 100 | + }); |
| 101 | + robot.mouseMove(loc.x + size.width / 2, loc.y + size.height / 2); |
| 102 | + robot.waitForIdle(); |
| 103 | + robot.delay(200); |
| 104 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 105 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 106 | + if (failed) { |
| 107 | + throw new RuntimeException("Failed: unnecessary repaint occured"); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + static class InternalFramePanel extends JPanel { |
| 113 | + final AtomicInteger repaintCounter = new AtomicInteger(0); |
| 114 | + InternalFramePanel() { |
| 115 | + super(new FlowLayout()); |
| 116 | + setOpaque(true); |
| 117 | + } |
| 118 | + |
| 119 | + public synchronized void paintComponent(Graphics g) { |
| 120 | + super.paintComponent(g); |
| 121 | + repaintCounter.incrementAndGet(); |
| 122 | + System.out.println("repaintCounter " + repaintCounter.intValue()); |
| 123 | + if (traced) { |
| 124 | + failed = true; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments