1 /*
2  * Copyright (c) 2014, 2018, 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 /* @test
25  * @key headful
26  * @bug 8041982
27  * @summary Use of animated icon in JLayer causes CPU spin
28  * @author Alexander Potochkin
29  */
30 
31 import javax.swing.*;
32 import javax.swing.plaf.LayerUI;
33 import java.awt.*;
34 import java.beans.PropertyChangeEvent;
35 
36 public class bug8041982 extends JFrame {
37 
bug8041982()38     public bug8041982() {
39         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
40         add(new JLayer<>(new JPanel(), new BusyLayer()));
41         setSize(200, 300);
42         setVisible(true);
43     }
44 
main(String... args)45     public static void main(String... args) throws Exception {
46         SwingUtilities.invokeLater(new Runnable() {
47             public void run() {
48                 new bug8041982().setVisible(true);
49             }
50         });
51         Thread.sleep(5000);
52     }
53 
54     private class BusyLayer extends LayerUI<JComponent> {
55         private volatile boolean animated = true;
56         private Icon icon = new ImageIcon(bug8041982.class.getResource("duke.gif"));
57         private int imageUpdateCount;
58 
59         @Override
paint(Graphics g, JComponent c)60         public void paint(Graphics g, JComponent c) {
61             super.paint(g, c);
62             if (isAnimated()) {
63                 icon.paintIcon(c, g, c.getWidth() / 2 - icon.getIconWidth() /
64                         2,
65                         c.getHeight() / 2 - icon.getIconHeight() / 2);
66             }
67         }
68 
isAnimated()69         public boolean isAnimated() {
70             return animated;
71         }
72 
setAnimated(boolean animated)73         public void setAnimated(boolean animated) {
74             if (this.animated != animated) {
75                 this.animated = animated;
76                 firePropertyChange("animated", !animated, animated);
77             }
78         }
79 
80         @Override
applyPropertyChange(PropertyChangeEvent evt, JLayer l)81         public void applyPropertyChange(PropertyChangeEvent evt, JLayer l) {
82             // this will be called when the busy flag is changed
83             l.repaint();
84         }
85 
86         @Override
imageUpdate(Image img, int infoflags, int x, int y, int w, int h, JLayer<? extends JComponent> l)87         public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h, JLayer<? extends JComponent> l) {
88             System.out.println("imageUpdate " + imageUpdateCount);
89             if (imageUpdateCount++ == 100) {
90                 setAnimated(false);
91             } else if (imageUpdateCount > 100) {
92                 throw new RuntimeException("Test failed");
93             }
94             return isAnimated() && super.imageUpdate(img, infoflags, x, y, w, h, l);
95         }
96     }
97 }
98 
99