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.

[sourcecode language=”java”]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());
}

}
[/sourcecode]

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