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