Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home J2me J2ME Canvas Repaint

Related Tutorials


 
 

Share on Google+Share on Google+

J2ME Canvas Repaint

Advertisement
In J2ME repaint is the method of the canvas class, and is used to repaint the entire canvas class. To define the repaint method in you midlet follow the given syntax.

J2ME Canvas Repaint

     

In J2ME repaint is the method of the canvas class, and is used to repaint the entire canvas class. To define the repaint method in you midlet follow the given syntax.

public final void repaint()

Output of the repaint method example

 

 

 

 

Source Code of UsingRepaintMethod.java

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

public class UsingRepaintMethod extends MIDlet{
  private Display  display;
  private GameActionCanvas canvas;

  public UsingRepaintMethod(){
  display = Display.getDisplay(this);
  canvas  = new GameActionCanvas(this);
  }

  protected void startApp(){
  display.setCurrentcanvas );
  }

  protected void pauseApp(){ }

  protected void destroyAppboolean unconditional ){
  notifyDestroyed();
  }

  public void exitMIDlet(){
  destroyApp(true);
  }
}

class GameActionCanvas extends Canvas implements CommandListener{
  private Command exit;
  private String keyValue = null;
  private UsingRepaintMethod midlet;

  public GameActionCanvas(UsingRepaintMethod midlet){
  this.midlet = midlet;

  exit = new Command("Exit", Command.EXIT, 1);
  addCommand(exit);
  setCommandListener(this);
  
  
  protected void paint(Graphics g){
  g.setColor(00255);
  g.fillRect(00, getWidth(), getHeight());

  if (keyValue != null){
  g.setColor(25500);
  g.drawString(keyValue, getWidth()/2, getHeight()/2
  Graphics.TOP | Graphics.HCENTER
);
  }
  }

  public void commandAction(Command c, Displayable d){
  if (c == exit)
  midlet.exitMIDlet();
  }

  protected void keyPressed(int keyCode){
  switch (getGameAction(keyCode)){
  case FIRE:
  case UP: 
  case DOWN:
  case LEFT:
  case RIGHT:
  case GAME_A:
  case GAME_B:
  case GAME_C:
  case GAME_D:
  default:
  keyValue = getKeyName(keyCode);
  }  
  repaint();
  }
}

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 20, 2008

Related Tutorials

Discuss: J2ME Canvas Repaint  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com