Hi i've written the following code using j2me wireless toolkit 1.04.02 and it runs on the emulated phone, but when i download it to the 7600 the screen doesn't refresh and the ball doesn't move:
/*--------------------------------------------------
* moving2.java
*
*-------------------------------------------------*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class moving2 extends MIDlet
{
private Display display; // The display
private TextCanvas canvas; // Canvas to display text
public moving2()
{
display = Display.getDisplay(this);
canvas = new TextCanvas(this);
}
protected void startApp()
{
display.setCurrent(canvas);
}
protected void pauseApp()
{
display = null;
}
protected void destroyApp( boolean unconditional )
{ }
public void exitMIDlet()
{
destroyApp(true);
notifyDestroyed();
}
}
/*--------------------------------------------------
* Class TextCanvas
*
* Draw text
*-------------------------------------------------*/
class TextCanvas extends Canvas implements CommandListener
{
private Command cmExit;
private moving2 midlet;
private Image offscreen = null;
private int height;
private int width;
private int x=5;
private int y=5;
private int z=1;
private int a=1;
public TextCanvas(moving2 midlet)
{
this.midlet = midlet;
// Create commands & listen for events
cmExit = new Command("Exit", Command.EXIT, 1);
addCommand(cmExit);
setCommandListener(this);
height = getHeight();
width = getWidth();
if( !isDoubleBuffered() ){
offscreen = Image.createImage( width, height );
}
}
/*--------------------------------------------------
* Draw text
*-------------------------------------------------*/
protected void paint( Graphics g ){
Graphics saved = g;
if( offscreen != null ){
g = offscreen.getGraphics();
}
g.setColor(0, 255, 0);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(255, 255, 0);
g.fillRoundRect (x, y, 10, 10, 10, 10);
g.setColor(255, 0,0);
g.fillRect(10, 10, 25, 7);
x+=z;
y+=a;
repaint();
if ((x + 10)> getWidth()) // Ball bounces off applet wall if its centre x-
// position is 2 x radius distance away from the right edge of the applet
{
z = -z; //does this by changing speed of ball to negative figure
}
else if (x < 0) // Ball bounces off applet wall if its centre x-position
// is radius distance away from the left edge of the applet
{
z = -z; //does this by changing speed of ball back to positive figure
}
if( g != saved ){
saved.drawImage( offscreen, 0, 0,
Graphics.LEFT | Graphics.TOP );
}
if ((y + 10)> getHeight()) // Ball bounces off applet wall if its centre x-
// position is 2 x radius distance away from the right edge of the applet
{
a = -a; //does this by changing speed of ball to negative figure
}
else if (y < 0) // Ball bounces off applet wall if its centre x-position
// is radius distance away from the left edge of the applet
{
a = -a; //does this by changing speed of ball back to positive figure
}
if( g != saved ){
saved.drawImage( offscreen, 0, 0,
Graphics.LEFT | Graphics.TOP );
}
}
/*--------------------------------------------------
* Exit midlet
*-------------------------------------------------*/
public void commandAction(Command c, Displayable d)
{
if (c == cmExit)
midlet.exitMIDlet();
}
}
Any help will be much appreciated. Is it just this phone (i haven't tried it on another nokia yet)?

Reply With Quote

