1/* 2 * Copyright (c) 2015, 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 24import java.awt.image.BufferedImage; 25import java.awt.image.ImageObserver; 26 27import javax.swing.ImageIcon; 28import javax.swing.JButton; 29import javax.swing.SwingUtilities; 30 31import static java.awt.image.BufferedImage.TYPE_INT_RGB; 32 33/** 34 * @test 35 * @bug 6573305 36 * @summary Animated icon should animate when the JButton is pressed. 37 * @author Sergey Bylokhov 38 */ 39public final class AnimatedIcon { 40 41 public static void main(final String[] args) throws Exception { 42 SwingUtilities.invokeAndWait(() -> { 43 final BufferedImage bi = new BufferedImage(1, 1, TYPE_INT_RGB); 44 final ImageIcon icon = new ImageIcon(bi); 45 final JButton button = new JButton(icon); 46 // Default icon is set => imageUpdate should return true for it 47 isAnimated(bi, button); 48 button.getModel().setPressed(true); 49 button.getModel().setArmed(true); 50 isAnimated(bi, button); 51 button.getModel().setPressed(false); 52 button.getModel().setArmed(false); 53 button.getModel().setSelected(true); 54 isAnimated(bi, button); 55 button.getModel().setSelected(false); 56 button.getModel().setRollover(true); 57 button.setRolloverEnabled(true); 58 isAnimated(bi, button); 59 button.getModel().setSelected(true); 60 isAnimated(bi, button); 61 // Default icon is not set => imageUpdate should return true for 62 // other icons if any 63 button.setIcon(null); 64 button.setPressedIcon(icon); 65 button.getModel().setPressed(true); 66 button.getModel().setArmed(true); 67 isAnimated(bi, button); 68 }); 69 } 70 71 private static void isAnimated(BufferedImage bi, JButton button) { 72 if (!button.imageUpdate(bi, ImageObserver.SOMEBITS, 0, 0, 1, 1)) { 73 throw new RuntimeException(); 74 } 75 } 76} 77