J2ME,GameCanvas and Thread
And here is the GameCanvas class
Note that I have removed the content from paint() and pumped them into render(Graphics g) instead. Also notice the use of flushGraphics() here. And yes super(true) in the constructor.
package MyApp;
import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.lang.Runnable.*;
public class MyGameCanvas extends GameCanvas implements Runnable
{
private boolean vRenderLoop;
private int vXPos;
private int vWidth;
private int vHeight;
MyGameCanvas()
{
super(true);
System.out.println("Constructor called");
vXPos=0;
vWidth=getWidth();
vHeight=getHeight();
vRenderLoop=false;
}
public void paint(Graphics g)
{
}
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)
{
g.setColor(0xffffff);
g.fillRect(0,0,vWidth,vHeight);
vXPos++;
g.setColor(0x000000);
g.fillRect(vXPos, 50, 100,100);
if(vXPos > (100 + vWidth))
vXPos=0;
}
public void run()
{
System.out.println("yeah yeah");
Graphics g = getGraphics();
while(vRenderLoop)
{
render(g);
flushGraphics();
try { Thread.sleep(10);}
catch (InterruptedException ie)
{
stop();
}
}
}
}
Note that I have removed the content from paint() and pumped them into render(Graphics g) instead. Also notice the use of flushGraphics() here. And yes super(true) in the constructor.
Comments