Path: blob/master/test/jdk/javax/swing/AbstractButton/AnimatedIcon/AnimatedIcon.java
41154 views
/*1* Copyright (c) 2015, 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*/2223import java.awt.image.BufferedImage;24import java.awt.image.ImageObserver;2526import javax.swing.ImageIcon;27import javax.swing.JButton;28import javax.swing.SwingUtilities;2930import static java.awt.image.BufferedImage.TYPE_INT_RGB;3132/**33* @test34* @bug 657330535* @summary Animated icon should animate when the JButton is pressed.36* @author Sergey Bylokhov37*/38public final class AnimatedIcon {3940public static void main(final String[] args) throws Exception {41SwingUtilities.invokeAndWait(() -> {42final BufferedImage bi = new BufferedImage(1, 1, TYPE_INT_RGB);43final ImageIcon icon = new ImageIcon(bi);44final JButton button = new JButton(icon);45// Default icon is set => imageUpdate should return true for it46isAnimated(bi, button);47button.getModel().setPressed(true);48button.getModel().setArmed(true);49isAnimated(bi, button);50button.getModel().setPressed(false);51button.getModel().setArmed(false);52button.getModel().setSelected(true);53isAnimated(bi, button);54button.getModel().setSelected(false);55button.getModel().setRollover(true);56button.setRolloverEnabled(true);57isAnimated(bi, button);58button.getModel().setSelected(true);59isAnimated(bi, button);60// Default icon is not set => imageUpdate should return true for61// other icons if any62button.setIcon(null);63button.setPressedIcon(icon);64button.getModel().setPressed(true);65button.getModel().setArmed(true);66isAnimated(bi, button);67});68}6970private static void isAnimated(BufferedImage bi, JButton button) {71if (!button.imageUpdate(bi, ImageObserver.SOMEBITS, 0, 0, 1, 1)) {72throw new RuntimeException();73}74}75}767778