Criando uma animação usando Timer - J2ME

Aprenda a fazer animações em dispositivos móveis.

Criando uma animação usando Timer - J2ME

Uma animação é criada em dispositivos móveis através do método repaint() da classe Canvas. A imagem é apresentada na tela em intervalos regulares de tempo em posições diferentes para mostrar o efeito da animação, neste caso, o movimento.

O código abaixo faz uma animação de alguns segundos e depois deixa a imagem parada na tela.

<B>import </B></FONT>javax.microedition.midlet.*;</FONT><BR><B>import </B></FONT>javax.microedition.lcdui.*;</FONT><BR></FONT><BR><B>public class </B></FONT>Animation </FONT><B>extends </B></FONT>MIDlet </FONT><B>implements </B></FONT>CommandListener</FONT><BR>{</FONT><BR>  </FONT><B>public </B></FONT>Display display;</FONT><BR>  </FONT><B>public </B></FONT>Animation</FONT>() {}</FONT><BR></FONT><BR>  </FONT><B>public </B></FONT><B>void </B></FONT>startApp</FONT>() </FONT><BR>  </FONT>{</FONT><BR>    </FONT>AnimationCanvas animation=</FONT><B>new </B></FONT>AnimationCanvas</FONT>(</FONT><B>this</B></FONT>)</FONT>;</FONT><BR>    </FONT>animation.startAnimation</FONT>()</FONT>;</FONT><BR>    </FONT>Display.getDisplay</FONT>(</FONT><B>this</B></FONT>)</FONT>.setCurrent</FONT>(</FONT>animation</FONT>)</FONT>;</FONT><BR>  </FONT>}</FONT><BR></FONT><BR>  </FONT><B>public </B></FONT><B>void </B></FONT>pauseApp</FONT>() {}</FONT><BR></FONT><BR>  </FONT><B>public </B></FONT><B>void </B></FONT>destroyApp</FONT>( </FONT><B>boolean </B></FONT>unconditional </FONT>) {}</FONT><BR></FONT><BR>  </FONT><B>public </B></FONT><B>void </B></FONT>exit</FONT>()</FONT><BR>  </FONT>{</FONT><BR>    </FONT>destroyApp</FONT>(</FONT><B>true</B></FONT>)</FONT>;</FONT><BR>    </FONT>notifyDestroyed</FONT>()</FONT>;</FONT><BR>  </FONT>}</FONT><BR></FONT><BR>  </FONT><B>public </B></FONT><B>void </B></FONT>commandAction</FONT>(</FONT>Command c, Displayable s</FONT>){}</FONT><BR>}</FONT><BR></FONT><BR></FONT><BR><B>import </B></FONT>javax.microedition.lcdui.*;</FONT><BR></FONT><BR><B>public class </B></FONT>AnimationCanvas </FONT><B>extends </B></FONT>Canvas </FONT><B>implements </B></FONT>Runnable, </FONT><BR> </FONT>CommandListener</FONT><BR>{</FONT><BR>  </FONT><B>private </B></FONT>Animation midlet;</FONT><BR>  </FONT><B>int </B></FONT>height;</FONT><BR>  </FONT><B>int </B></FONT>width;</FONT><BR>  </FONT><B>int </B></FONT>pad = </FONT>3</FONT>;</FONT><BR>  </FONT><B>boolean </B></FONT>finishedAnimation = </FONT><B>false</B></FONT>;</FONT><BR>  </FONT><B>int </B></FONT>deltaY = </FONT>0</FONT>;</FONT><BR></FONT><BR>  </FONT><B>private </B></FONT>Command  startCommand;</FONT><BR>  </FONT><B>private </B></FONT>Command exitCommand;</FONT><BR>  </FONT><B>private </B></FONT>Command aboutCommand;</FONT><BR></FONT><BR></FONT><BR>  </FONT><B>public </B></FONT>AnimationCanvas</FONT>(</FONT>Animation midlet</FONT>)</FONT><BR>  </FONT>{</FONT><BR>    </FONT><B>this</B></FONT>.midlet=midlet;</FONT><BR>    </FONT>height = getHeight</FONT>()</FONT>;</FONT><BR>    </FONT>width = getWidth</FONT>()</FONT>;</FONT><BR></FONT><BR>    </FONT>startCommand = </FONT><B>new </B></FONT>Command</FONT>(</FONT>"Iniciar"</FONT>,Command.BACK,</FONT>1</FONT>)</FONT>;</FONT><BR>    </FONT>exitCommand = </FONT><B>new </B></FONT>Command</FONT>(</FONT>"Sair"</FONT>,Command.SCREEN,</FONT>0</FONT>)</FONT>;</FONT><BR>    </FONT>aboutCommand = </FONT><B>new </B></FONT>Command</FONT>(</FONT>"Sobre"</FONT>,Command.SCREEN,</FONT>3</FONT>)</FONT>;</FONT><BR>  </FONT>}</FONT><BR></FONT><BR>  </FONT><B>public </B></FONT><B>void </B></FONT>paint</FONT>(</FONT>Graphics g</FONT>)</FONT><BR>  </FONT>{</FONT><BR>    </FONT>g.setColor</FONT>(</FONT>0xFFFFFF</FONT>)</FONT>;</FONT><BR>    </FONT>g.fillRect</FONT>(</FONT>0</FONT>,</FONT>0</FONT>,width,height</FONT>)</FONT>;</FONT><BR>    </FONT>g.setColor</FONT>(</FONT>0</FONT>,</FONT>255</FONT>,</FONT>0</FONT>)</FONT>;</FONT><BR>    </FONT>g.drawRoundRect</FONT>(</FONT>0</FONT>,</FONT>0</FONT>,width-</FONT>1</FONT>,height-</FONT>1</FONT>,</FONT>10</FONT>,</FONT>10</FONT>)</FONT>;</FONT><BR>  </FONT><BR>    </FONT><B>if </B></FONT>(</FONT>finishedAnimation</FONT>)</FONT><BR>    </FONT>{</FONT><BR>      </FONT>Form form = </FONT><B>new </B></FONT>Form</FONT>(</FONT>"Animação"</FONT>)</FONT>;</FONT><BR>      </FONT>form.append</FONT>(</FONT>"Animação Completa\n"</FONT>)</FONT>;</FONT><BR>      </FONT>form.addCommand</FONT>(</FONT>exitCommand</FONT>)</FONT>;</FONT><BR>      </FONT>form.setCommandListener</FONT>(</FONT><B>this</B></FONT>)</FONT>;</FONT><BR>      </FONT>Display.getDisplay</FONT>(</FONT>midlet</FONT>)</FONT>.setCurrent</FONT>(</FONT>form</FONT>)</FONT>;</FONT><BR></FONT><BR>    </FONT>}</FONT><BR>    </FONT><B>else</B></FONT><BR>    </FONT>{</FONT><BR>      </FONT>Image img=</FONT><B>null</B></FONT>;</FONT><BR>      </FONT><B>try </B></FONT><BR>      </FONT>{</FONT><BR>        </FONT>img = Image.createImage</FONT>(</FONT>"/logo.png"</FONT>)</FONT>;</FONT><BR>      </FONT>}</FONT><BR>      </FONT><B>catch</B></FONT>(</FONT>Exception e</FONT>){}</FONT><BR>      </FONT><B>int </B></FONT>ih = img.getHeight</FONT>()</FONT>;</FONT><BR>      </FONT><B>int </B></FONT>iw = img.getWidth</FONT>()</FONT>;</FONT><BR>      </FONT><B>int </B></FONT>imgX = </FONT>(</FONT>width-</FONT>2</FONT>)</FONT>/</FONT>2</FONT>;</FONT><BR>      </FONT><B>int </B></FONT>imgY = </FONT>(</FONT>height-</FONT>2</FONT>)</FONT>/</FONT>2</FONT>;</FONT><BR>      </FONT>g.drawImage</FONT>(</FONT>img,imgX,imgY+deltaY,Graphics.VCENTER|Graphics.HCENTER</FONT>)</FONT>;</FONT><BR>        </FONT><BR>      </FONT>removeCommand</FONT>(</FONT>exitCommand</FONT>)</FONT>;</FONT><BR>    </FONT>}</FONT><BR>  </FONT>}</FONT><BR></FONT><BR>  </FONT><B>public </B></FONT><B>void </B></FONT>startAnimation</FONT>()</FONT><BR>  </FONT>{</FONT><BR>    </FONT><B>try</B></FONT><BR>    </FONT>{</FONT><BR>      </FONT>finishedAnimation = </FONT><B>false</B></FONT>;</FONT><BR>      </FONT>Thread t = </FONT><B>new </B></FONT>Thread</FONT>(</FONT><B>this</B></FONT>)</FONT>;</FONT><BR>      </FONT>t.start</FONT>()</FONT>;</FONT><BR>    </FONT>}</FONT><BR>    </FONT><B>catch</B></FONT>(</FONT>Exception e</FONT>){}</FONT><BR>  </FONT>}</FONT><BR></FONT><BR>  </FONT><B>public </B></FONT><B>void </B></FONT>run</FONT>()</FONT><BR>  </FONT>{</FONT><BR>    </FONT><B>try</B></FONT><BR>    </FONT>{</FONT><BR>      </FONT><B>int </B></FONT>count = </FONT>0</FONT>;</FONT><BR>      </FONT><B>int </B></FONT>sleep_time = </FONT>200</FONT>;</FONT><BR>      </FONT><B>while </B></FONT>(</FONT><B>true</B></FONT>)</FONT><BR>      </FONT>{</FONT><BR>        </FONT>Thread.sleep</FONT>(</FONT>sleep_time</FONT>)</FONT>;</FONT><BR>        </FONT>count++;</FONT><BR>        </FONT><B>if </B></FONT>(</FONT>count > </FONT>20</FONT>) </FONT><BR>        </FONT>{</FONT><BR>          </FONT>finishedAnimation = </FONT><B>true</B></FONT>;</FONT><BR>          </FONT>Thread.sleep</FONT>(</FONT>sleep_time</FONT>)</FONT>;</FONT><BR>          </FONT>repaint</FONT>()</FONT>;</FONT><BR>          </FONT><B>break</B></FONT>;</FONT><BR>        </FONT>}</FONT><BR>        </FONT><B>if </B></FONT>(</FONT>count <=</FONT>5 </FONT>) </FONT><BR>          </FONT>deltaY += </FONT>5</FONT>;</FONT><BR>        </FONT><B>else if </B></FONT>(</FONT>count ></FONT>5 </FONT>&& count <=</FONT>15</FONT>)</FONT><BR>          </FONT>deltaY -= </FONT>5</FONT>;</FONT><BR>        </FONT><B>else </B></FONT><BR>          </FONT>deltaY +=</FONT>5</FONT>;</FONT><BR>        </FONT>repaint</FONT>()</FONT>;</FONT><BR>      </FONT>}</FONT><BR>    </FONT>}</FONT><BR>    </FONT><B>catch</B></FONT>(</FONT>Exception e</FONT>){}</FONT><BR>  </FONT>}</FONT><BR></FONT><BR>  </FONT><B>public </B></FONT><B>void </B></FONT>commandAction</FONT>(</FONT>Command c,Displayable d</FONT>)</FONT><BR>  </FONT>{</FONT><BR>    </FONT><B>if </B></FONT>(</FONT>c == exitCommand</FONT>){</FONT><BR>      </FONT>midlet.exit</FONT>()</FONT>;</FONT>}</FONT><BR>  </FONT>}</FONT><BR>}</FONT>

Artigos relacionados