J2ME Canvas with Threading
What we shall do here is the following
1) Create a Midlet
2) Create a Canvas class which implements the threading
3) Load a PNG image
4) Render the image through the thread.
Configuration.
You would need to download the Netbean IDE. When I did this program the version I had was NetBean IDE 6.1. You can go to their website to download it. It would suite for other programming languages too, including the Set Top Box.
The program.

The Midlet:
Just like an applet for the browser, the midlet is an application running in the mobile platform using J2ME.
So to run an application on a mobile device you first need a Midlet class.
This is the midlet class.
For better understanding I am keeping this class as small as possible. As you can see, this is where the application first initializes. The startApp function is invoked and all dram happens from then on. Here we create an object for the MyGameCanvas class defined below and trigger the thread within it.
The MyGameCanvas class:
This class loads an image (a.png) and repaints the scene by rendering the image in a different location with a delay of 20ms. It is self explanatory as you can see. The thread runs asynchronously and hence you could take advantage of this by playing a small progress bar animation while waiting for a request from the server.
Also note that here we make use of the Canvas class. And hence the functions paint() and repaint(). If we extended GameCanvas instead then we would make use of flushGraphics().
The use of GameCanvas shall be explained in the next blog.
1) Create a Midlet
2) Create a Canvas class which implements the threading
3) Load a PNG image
4) Render the image through the thread.
Configuration.
You would need to download the Netbean IDE. When I did this program the version I had was NetBean IDE 6.1. You can go to their website to download it. It would suite for other programming languages too, including the Set Top Box.
The program.
The Midlet:
Just like an applet for the browser, the midlet is an application running in the mobile platform using J2ME.
So to run an application on a mobile device you first need a Midlet class.
This is the midlet class.
package MyApp;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class MyApp extends MIDlet {
public void startApp() {
MyGameCanvas myApp = new MyGameCanvas();
myApp.start();
Display.getDisplay(this).setCurrent(d);
}
}
For better understanding I am keeping this class as small as possible. As you can see, this is where the application first initializes. The startApp function is invoked and all dram happens from then on. Here we create an object for the MyGameCanvas class defined below and trigger the thread within it.
The MyGameCanvas class:
package MyApp;
import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.lang.Runnable.*;
public class MyGameCanvas extends Canvas implements Runnable
{
private Image vImage;
private boolean vRenderLoop;
private int vXPos;
private int vWidth;
private int vHeight;
MyGameCanvas()
{
//super(true);
vXPos=0;
vWidth=getWidth();
vHeight=getHeight();
System.out.println("Constructor called");
vImage=null;
vRenderLoop=false;
try
{
vImage=Image.createImage("/im/a.png");
System.out.println("Loaded the image");
} catch(IOException exp)
{
System.out.println("Image not found" + exp.getMessage());
}
}
public void paint(Graphics g)
{
g.setColor(0xffffff);
g.fillRect(0,0,vWidth,vHeight);
vXPos++;
if(vImage!=null)
{
g.drawImage(vImage, vXPos, 50, Graphics.TOP | Graphics.LEFT);
vXPos++;
if((vXPos + vImage.getWidth())> vWidth)
vXPos=0;
}
}
public void start()
{
System.out.println("start invoked");
vRenderLoop=true;
Thread t = new Thread(this);
t.start();
}
public void stop()
{
vRenderLoop=false;
}
public void render(Graphics g)
{
}
public void run()
{
System.out.println("yeah yeah");
while(vRenderLoop)
{
try { Thread.sleep(20);}
catch (InterruptedException ie)
{
stop();
}
repaint();
}
}
}
This class loads an image (a.png) and repaints the scene by rendering the image in a different location with a delay of 20ms. It is self explanatory as you can see. The thread runs asynchronously and hence you could take advantage of this by playing a small progress bar animation while waiting for a request from the server.
Also note that here we make use of the Canvas class. And hence the functions paint() and repaint(). If we extended GameCanvas instead then we would make use of flushGraphics().
The use of GameCanvas shall be explained in the next blog.
Comments