/* * Copyright © 2012 Nokia Corporation. All rights reserved. * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. * Oracle and Java are trademarks or registered trademarks of Oracle and/or its * affiliates. Other product and company names mentioned herein may be trademarks * or trade names of their respective owners. * See LICENSE.TXT for license information. */ package com.nokia.example.drumkit.components; import java.util.Timer; import java.util.TimerTask; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.game.Sprite; public class AnimatedButton extends Button { private long interval; private Timer animator; private Sprite animation; public AnimatedButton(String unpressed_image_url, String pressed_image_url, String disabled_image_url, long interval, Listener listener) { super(unpressed_image_url, pressed_image_url, disabled_image_url, listener); this.interval = interval; this.animation = new Sprite(getPressedImage(), getWidth(), getHeight()); } /** * Render the button * @param g */ public void paint(Graphics g) { if (isPressed() || isSelected()) { if (animator == null) { startAnimation(interval); } animation.paint(g); } else { stopAnimation(); super.paint(g); } } /** * Set the position of top left coordinate of the button * @param x * @param y */ public void setPosition(int x, int y) { animation.setPosition(x, y); super.setPosition(x, y); } /** * Start animation */ private void startAnimation(long interval) { if (animator != null) { stopAnimation(); } animator = new Timer(); animator.schedule(new TimerTask() { public void run() { animation.nextFrame(); } }, 0, interval); } /** * Stop animation */ private void stopAnimation() { if (animator != null) { animator.cancel(); animator = null; animation.setFrame(0); } } }