graphics.copyArea(0, 0, w, h, 0, 1, Graphics.TOP | Graphics.LEFT);
graphics.setColor(0, 0, 0);
graphics.drawLine(0, 0, w, 0);
graphics.setColor(255, 255, 255);
for (int i = 0; i < w; ++i)
{
int test = Math.abs(random.nextInt()) % 100;
if (test < 5)
{
graphics.drawLine(i, 0, i, 0);
}
}
flushGraphics();
// Now wait...
try
{
Thread.currentThread().sleep(sleepTime);
} catch (InterruptedException e)
{
}
}
}
下面给出源代码
/*
* License
*
* Copyright 1994-2004 Sun Microsystems, Inc. All Rights Reserved.
*
*/
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.midlet.*;
public class GameCanvasTest extends MIDlet implements CommandListener
{
private Display display;
public static final Command exitCommand = new Command("Exit", Command.EXIT,
1);
public GameCanvasTest()
{
}
public void commandAction(Command c, Displayable d)
{
if (c == exitCommand)
{
exitMIDlet();
}
}
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException
{
exitMIDlet();
}
public void exitMIDlet()
{
notifyDestroyed();
}
public Display getDisplay()
{
return display;
}
protected void initMIDlet()
{
GameCanvas c = new StarField();
c.addCommand(exitCommand);
c.setCommandListener(this);
getDisplay().setCurrent(c);
}
protected void pauseApp()
{
}