J2ME Frame Animation2

This example illustrates same as in the previous example, but here we are creating four blocks and set five frame manually.

J2ME Frame Animation2

J2ME Frame Animation2

     

This example illustrates same as in the previous example, but here we are creating four blocks and set five frame manually. The whole example shows you to create the frame and blocks. In this example user can set the frame and blocks manually on the run time. In the source code we extend the FrameAnimation class, which is also used in the previous application. For the movement of the blocks we create the moveAllBlocks() function, the source code is as follows:

 

 

 

 

 

public synchronized void moveAllBlocks(){
  for (int i = 0, count = blocks.length; i < count; i++){
  Block block = blocks[i];
  repaint(block.x, block.y, SIZE, SIZE);
  blocks[i].move();
  repaint(block.x, block.y, SIZE, SIZE);
  }
}

The Application is as follows:

 

FrameAnimation2.java

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class FrameAnimation2 extends FrameAnimation {
  protected FrameAnimation.AnimationCanvas createAnimationCanvas(){
  return new AnimationCanvas2();
  }
  
  class AnimationCanvas2 extends FrameAnimation.AnimationCanvas{
  protected void paint(Graphics g){
  int clipX = g.getClipX();
  int clipY = g.getClipY();
  int clipWidth = g.getClipWidth();
  int clipHeight = g.getClipHeight();
  g.setColor(background);
  g.fillRect(clipX, clipY, clipWidth, clipHeight);
  g.setColor(foreground);
  synchronized (this){
  for (int i = 0, count = blocks.length; i < count; i++) {
  g.fillRect(blocks[i].x, blocks[i].y, SIZE, SIZE);
  }
  }
  }
  public synchronized void moveAllBlocks(){
  for (int i = 0, count = blocks.length; i < count; i++){
  Block block = blocks[i];
  repaint(block.x, block.y, SIZE, SIZE);
  blocks[i].move();
  repaint(block.x, block.y, SIZE, SIZE);
  }
  }
  }
} 

 

Download Source Code