|
| 1 | +/* |
| 2 | + * Copyright (c) 1999, 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 4073822 |
| 27 | + * @summary ScrollPane repaints entire window when scrolling fast |
| 28 | + * @library /java/awt/regtesthelpers |
| 29 | + * @build PassFailJFrame |
| 30 | + * @run main/manual ScrollPaneFlicker |
| 31 | + */ |
| 32 | + |
| 33 | +import java.awt.Button; |
| 34 | +import java.awt.Canvas; |
| 35 | +import java.awt.Checkbox; |
| 36 | +import java.awt.Choice; |
| 37 | +import java.awt.Color; |
| 38 | +import java.awt.Component; |
| 39 | +import java.awt.Dimension; |
| 40 | +import java.awt.Font; |
| 41 | +import java.awt.Frame; |
| 42 | +import java.awt.Graphics; |
| 43 | +import java.awt.Label; |
| 44 | +import java.awt.Menu; |
| 45 | +import java.awt.MenuBar; |
| 46 | +import java.awt.MenuItem; |
| 47 | +import java.awt.Panel; |
| 48 | +import java.awt.Rectangle; |
| 49 | +import java.awt.ScrollPane; |
| 50 | +import java.awt.Scrollbar; |
| 51 | +import java.awt.TextArea; |
| 52 | +import java.awt.TextField; |
| 53 | +import javax.swing.JButton; |
| 54 | +import javax.swing.JCheckBox; |
| 55 | +import javax.swing.JComboBox; |
| 56 | +import javax.swing.JLabel; |
| 57 | +import javax.swing.JList; |
| 58 | +import javax.swing.JPanel; |
| 59 | +import javax.swing.JScrollBar; |
| 60 | +import javax.swing.JTextArea; |
| 61 | +import javax.swing.JTextField; |
| 62 | + |
| 63 | +public class ScrollPaneFlicker { |
| 64 | + public static void main(String[] args) throws Exception { |
| 65 | + String INSTRUCTIONS = """ |
| 66 | + When scrolling a ScrollPane fast(i.e. holding the down/up arrow |
| 67 | + down for a while), the ScrollPane would inexplicably refresh |
| 68 | + the entire window. |
| 69 | +
|
| 70 | + 1. Select a type of ScrollPane content from the content menu. |
| 71 | + 2. Scroll the content using the up/down/left/right arrows on |
| 72 | + the scroll bar. Try scrolling the entire content area using |
| 73 | + the scroll arrows-- from top to bottom and left to right. |
| 74 | + 3. Verify that the entire pane does not refresh when scrolling |
| 75 | + - only the newly exposed areas should be repainting. |
| 76 | + 4. Repeat for all content types. |
| 77 | + """; |
| 78 | + PassFailJFrame.builder() |
| 79 | + .title("Test Instructions") |
| 80 | + .instructions(INSTRUCTIONS) |
| 81 | + .columns(35) |
| 82 | + .testUI(ScrollPaneFlicker::initialize) |
| 83 | + .build() |
| 84 | + .awaitAndCheck(); |
| 85 | + } |
| 86 | + |
| 87 | + static Frame initialize() { |
| 88 | + return new FlickerFrame(); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +class FlickerFrame extends Frame { |
| 93 | + ScrollPane pane; |
| 94 | + |
| 95 | + public FlickerFrame() { |
| 96 | + super("ScrollPane Flicker Test"); |
| 97 | + TextPanel textPanel = new TextPanel(); |
| 98 | + GradientPanel gradientPanel = new GradientPanel(); |
| 99 | + ComponentPanel componentPanel = new ComponentPanel(); |
| 100 | + SwingPanel swingPanel = new SwingPanel(); |
| 101 | + MenuBar menubar = new MenuBar(); |
| 102 | + Menu testMenu = new Menu("Test Options"); |
| 103 | + |
| 104 | + pane = new ScrollPane(); |
| 105 | + pane.getHAdjustable().setUnitIncrement(8); |
| 106 | + pane.getVAdjustable().setUnitIncrement(16); |
| 107 | + pane.add(textPanel); |
| 108 | + add(pane); |
| 109 | + |
| 110 | + testMenu.add(makeContentItem("Text Lines", textPanel)); |
| 111 | + testMenu.add(makeContentItem("Gradient Fill", gradientPanel)); |
| 112 | + testMenu.add(makeContentItem("AWT Components", componentPanel)); |
| 113 | + testMenu.add(makeContentItem("Swing Components", swingPanel)); |
| 114 | + menubar.add(testMenu); |
| 115 | + |
| 116 | + setMenuBar(menubar); |
| 117 | + setSize(400, 300); |
| 118 | + } |
| 119 | + |
| 120 | + public MenuItem makeContentItem(String title, final Component content) { |
| 121 | + MenuItem menuItem = new MenuItem(title); |
| 122 | + menuItem.addActionListener( |
| 123 | + ev -> { |
| 124 | + pane.add(content); |
| 125 | + pane.validate(); |
| 126 | + } |
| 127 | + ); |
| 128 | + return menuItem; |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +class GradientPanel extends Canvas { |
| 133 | + public void paint(Graphics g) { |
| 134 | + // just paint something that'll take a while |
| 135 | + int x, y; |
| 136 | + int width = getSize().width; |
| 137 | + int height = getSize().height; |
| 138 | + int step = 8; |
| 139 | + |
| 140 | + for (x = 0; x < width; x += step) { |
| 141 | + for (y = 0; y < height; y += step) { |
| 142 | + int red = (255 * y) / height; |
| 143 | + int green = (255 * x * y) / (width * height); |
| 144 | + int blue = (255 * x) / width; |
| 145 | + Rectangle bounds = g.getClipBounds(); |
| 146 | + Rectangle fbounds = new Rectangle(x, y, x + step, y + step); |
| 147 | + if (bounds.intersects(fbounds)) { |
| 148 | + Color color = new Color(red, green, blue); |
| 149 | + g.setColor(color); |
| 150 | + g.fillRect(x, y, x + step, y + step); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + public Dimension getPreferredSize() { |
| 157 | + return new Dimension(200, 1000); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +class TextPanel extends Canvas { |
| 162 | + public void paint(Graphics g) { |
| 163 | + Font font = new Font("SanSerif", Font.ITALIC, 12); |
| 164 | + |
| 165 | + g.setFont(font); |
| 166 | + // just paint something that'll take a while |
| 167 | + int x, y; |
| 168 | + int width = getWidth(); |
| 169 | + int height = getHeight(); |
| 170 | + int step = 16; |
| 171 | + |
| 172 | + for (x = y = 0; y < height; y += step) { |
| 173 | + Rectangle bounds = g.getClipBounds(); |
| 174 | + Rectangle tbounds = new Rectangle(x, y - 16, x + width, y); |
| 175 | + if (bounds.intersects(tbounds)) { |
| 176 | + g.drawString(y + " : The quick brown fox jumps over the lazy dog. " + |
| 177 | + "The rain in Spain falls mainly on the plain.", x, y); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + public Dimension getPreferredSize() { |
| 183 | + return new Dimension(640, 1000); |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +class ComponentPanel extends Panel { |
| 188 | + ComponentPanel() { |
| 189 | + add(new Label("Label")); |
| 190 | + add(new Button("Button")); |
| 191 | + add(new Checkbox("Checkbox")); |
| 192 | + Choice c = new Choice(); |
| 193 | + c.add("choice"); |
| 194 | + java.awt.List l = new java.awt.List(); |
| 195 | + l.add("list"); |
| 196 | + add(new Scrollbar()); |
| 197 | + add(new TextField("TextField")); |
| 198 | + add(new TextArea("TextArea")); |
| 199 | + add(new Panel()); |
| 200 | + add(new Canvas()); |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +class SwingPanel extends JPanel { |
| 205 | + SwingPanel() { |
| 206 | + add(new JLabel("JLabel")); |
| 207 | + add(new JButton("JButton")); |
| 208 | + add(new JCheckBox("JCheckBox")); |
| 209 | + JComboBox c = new JComboBox(); |
| 210 | + JList l = new JList(); |
| 211 | + add(new JScrollBar()); |
| 212 | + add(new JTextField("This is a JTextField with some text in it to make it longer.")); |
| 213 | + add(new JTextArea("This is a JTextArea with some text in it to make it longer.")); |
| 214 | + } |
| 215 | +} |
0 commit comments