Botão Animado em LWUIT - Ginga

Atendendo a pedidos, segue o código do componente para animação de botões para LWUIT - Ginga-J.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package org.gvlabs.sbtvd.ui;
 
import com.sun.dtv.lwuit.Button;
import com.sun.dtv.lwuit.Graphics;
import com.sun.dtv.lwuit.Image;
 
/**
 * Animated Button
 *
 * @author Thiago Galbiatti Vespa
 * @version 1.0
 */
public class AnimatedButton extends Button {
    private int speed = 50;
 
    private Image[] animaImgs;
 
    private int currentImage = 0;
    private long lastInvoke;
    private boolean forward = true;
    private boolean backAndForth = true;
    private boolean loop = true;
    private boolean disableLoop = false;
 
    /**
     * Default constructor
     */
    public AnimatedButton() {
    }
 
    /**
     * Get array of images
     * @return array of images
     */
    public Image[] getAnimaImgs() {
        return animaImgs;
    }
 
    /**
     * Set array of images to be animated
     * @param animaImgs array of images
     */
    public void setAnimaImgs(Image[] animaImgs) {
        this.animaImgs = animaImgs;
        if (animaImgs != null && animaImgs.length > 0) {
            this.setIcon(animaImgs[0]);
        }
    }
 
    /**
     * Get animation speed is ms
     * @return animation speed
     */
    public int getSpeed() {
        return speed;
    }
 
    /**
     * Set animation speed in ms
     * @param speed animation speed
     */
    public void setSpeed(int speed) {
        this.speed = speed;
    }
 
    /**
     * Back and Forth animation
     * @return true if is a back and forth animation
     */
    public boolean isBackAndForth() {
        return backAndForth;
    }
 
    /**
     * Set back and forth animation value
     * @param backAndForth
     */
    public void setBackAndForth(boolean backAndForth) {
        this.backAndForth = backAndForth;
    }
 
    /**
     * Is loop?
     * @return true if is a loop animation
     */
    public boolean isLoop() {
        return loop;
    }
 
    /**
     * true = loop, false = one animation
     * @param loop
     */
    public void setLoop(boolean loop) {
        this.loop = loop;
    }
 
    public boolean animate() {
        if (disableLoop)
            return false;
        long current = System.currentTimeMillis();
        if (current - lastInvoke > speed) {
            lastInvoke = current;
            if (forward) {
                currentImage++;
            } else {
                currentImage--;
            }
            if (currentImage >= animaImgs.length) {
                if (!loop) {
                    disableLoop = true;
                    return false;
                } else if (!backAndForth) {
                    forward = true;
                    currentImage = 0;
                } else {
                    forward = false;
                    currentImage = animaImgs.length - 1;
                }
            } else if (currentImage < 0) {
                forward = true;
                currentImage = 0;
            }
            return true;
        }
        return false;
    }
 
    /* (non-Javadoc)
     * @see com.sun.dtv.lwuit.Button#paint(com.sun.dtv.lwuit.Graphics)
     */
    public void paint(Graphics g) {
        g.drawImage(animaImgs[currentImage], getX(), getY());
    }
 
}

Para utilizar é só atribuir o conjunto de imagens para o método setAnimaImgs e registrar a animação no formulário, por exemplo: form.registerAnimation(animatedButton);

Sobre: Thiago Galbiatti Vespa

Thiago Galbiatti Vespa é mestre em Ciências da Computação e Matemática Computacional pela USP e bacharel em Ciências da Computação pela UNESP. Coordenador de projetos do JavaNoroeste, membro do JCP (Java Community Process), consultor Oracle, arquiteto de software de empresas de médio e grande porte, palestrante de vários eventos e colaborador de projetos open source. Possui as certificações: Oracle Certified Master, Java EE 5 Enterprise Architect – Step 1, 2 and 3; Oracle WebCenter Portal 11g Certified Implementation Specialist; Oracle Service Oriented Architecture Infrastructure Implementation Certified Expert; Oracle Certified Professional, Java EE 5 Web Services Developer; Oracle Certified Expert, NetBeans Integrated Development Environment 6.1 Programmer; Oracle Certified Professional, Java Programmer; Oracle Certified Associate, Java SE 5/SE 6