📄 layerexample.java
字号:
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import java.io.IOException;
/**
* A very basic midlet to run our GameCanvas example.
*/
public class LayerExample extends MIDlet
{
private AnotherGameCanvas myCanvas;
public LayerExample()
{
myCanvas = new AnotherGameCanvas();
}
protected void startApp() throws MIDletStateChangeException
{
Display.getDisplay(this).setCurrent(myCanvas);
myCanvas.start();
}
protected void pauseApp()
{
}
protected void destroyApp(boolean unconditional) throws MIDletStateChangeException
{
}
}
/**
* A class which extends the new MIDP 2 Game Canvas.
*/
class AnotherGameCanvas extends GameCanvas implements Runnable
{
private boolean running;
private int x, y;
private Image orbImage = null;
private Image backgroundTilesImage = null;
private Sprite orbSprite;
private BackgroundLayer backgroundLayer = null;
private LayerManager layerManager = null;
public AnotherGameCanvas()
{
super(true);
x = getWidth() / 2;
y = getHeight() / 2;
try
{
orbImage = Image.createImage("/orb.png");
backgroundTilesImage = Image.createImage("/pipes.png");
}
catch (IOException ioe)
{
System.out.println("unable to load image");
}
layerManager = new LayerManager();
// create the orb sprite and add it to the layoutmanager
orbSprite = new Sprite(orbImage, 16, 16);
layerManager.append(orbSprite);
// add the background layer as well
backgroundLayer = new BackgroundLayer(backgroundTilesImage);
layerManager.append(backgroundLayer);
}
public void start()
{
running = true;
Thread t = new Thread(this);
t.start();
}
public void run()
{
Graphics g = getGraphics();
while (running)
{
// handle the state of keys
int keyStates = getKeyStates();
if ((keyStates & LEFT_PRESSED) != 0) x--;
if ((keyStates & RIGHT_PRESSED) != 0) x++;
if ((keyStates & UP_PRESSED) != 0) y--;
if ((keyStates & DOWN_PRESSED) != 0) y++;
if (x < 0) x = getWidth();
if (x > getWidth()) x = 0;
if (y < 0) y = getHeight();
if (y > getHeight()) y = 0;
// update the sprite position and animation frame
orbSprite.setPosition(x, y);
orbSprite.nextFrame();
// clear the display
g.setColor(0x000000);
g.fillRect(0, 0, getWidth(), getHeight());
// draw the layers
layerManager.paint(g, 0, 0);
// flush the graphics buffer (GameCanvas will take care of painting)
flushGraphics();
// sleep a little
try
{
Thread.sleep(5);
}
catch (InterruptedException ie)
{
}
}
}
}
class BackgroundLayer extends TiledLayer
{
private static final int HORIZONTAL = 1;
private static final int VERTICAL = 2;
private static final int CROSS = 3;
public BackgroundLayer(Image tiles)
{
super(20, 20, tiles, 16, 16);
// draw horizontal lines
for (int y=0; y < 20; y+=5)
fillCells(0, y, 19, 1, HORIZONTAL);
// draw vertical lines
for (int x=0; x < 20; x+=5)
fillCells(x, 0, 1, 19, VERTICAL);
// fill in the cross pieces
for (int y=0; y < 20; y+=5)
for (int x=0; x < 20; x+=5)
setCell(x, y, CROSS);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -