Path: blob/master/test/jdk/javax/swing/JLayer/8041982/bug8041982.java
41153 views
/*1* Copyright (c) 2014, 2018, 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* @key headful25* @bug 804198226* @summary Use of animated icon in JLayer causes CPU spin27* @author Alexander Potochkin28*/2930import javax.swing.*;31import javax.swing.plaf.LayerUI;32import java.awt.*;33import java.beans.PropertyChangeEvent;3435public class bug8041982 extends JFrame {3637public bug8041982() {38setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);39add(new JLayer<>(new JPanel(), new BusyLayer()));40setSize(200, 300);41setVisible(true);42}4344public static void main(String... args) throws Exception {45SwingUtilities.invokeLater(new Runnable() {46public void run() {47new bug8041982().setVisible(true);48}49});50Thread.sleep(5000);51}5253private class BusyLayer extends LayerUI<JComponent> {54private volatile boolean animated = true;55private Icon icon = new ImageIcon(bug8041982.class.getResource("duke.gif"));56private int imageUpdateCount;5758@Override59public void paint(Graphics g, JComponent c) {60super.paint(g, c);61if (isAnimated()) {62icon.paintIcon(c, g, c.getWidth() / 2 - icon.getIconWidth() /632,64c.getHeight() / 2 - icon.getIconHeight() / 2);65}66}6768public boolean isAnimated() {69return animated;70}7172public void setAnimated(boolean animated) {73if (this.animated != animated) {74this.animated = animated;75firePropertyChange("animated", !animated, animated);76}77}7879@Override80public void applyPropertyChange(PropertyChangeEvent evt, JLayer l) {81// this will be called when the busy flag is changed82l.repaint();83}8485@Override86public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h, JLayer<? extends JComponent> l) {87System.out.println("imageUpdate " + imageUpdateCount);88if (imageUpdateCount++ == 100) {89setAnimated(false);90} else if (imageUpdateCount > 100) {91throw new RuntimeException("Test failed");92}93return isAnimated() && super.imageUpdate(img, infoflags, x, y, w, h, l);94}95}96}979899100