📄 examplegamecanvas.java
字号:
/*
* ExampleGameCanvas.java
*
* Created on 2007年3月29日, 下午4:41
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.tan.ui.game;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
/**
*
* @author USER
*/
public class ExampleGameCanvas extends GameCanvas implements Runnable{
private boolean isPlay;
private long delay;//to give thread delay;
private int currentX,currentY;//To hold current position of the "X","Y"
private int width;
private int height;//hold the screen x,y;
private int scnX,scnY;
private Image backgroundImage;
private Sprite backgroundSprite;
private LayerManager layerManager;
/** Creates a new instance of ExampleCanvas */
public ExampleGameCanvas() throws Exception{
super(true);
width = getWidth();
height = getHeight();
currentX = getWidth()/2;
currentY = getHeight()/2;
delay = 20;
backgroundImage = Image.createImage("/background.png");
backgroundSprite = new Sprite(backgroundImage);
layerManager = new LayerManager();
layerManager.append(backgroundSprite);
}
public void run() {
Graphics g = getGraphics();
while(isPlay == true) {
input();
drawScreen(g);
try {
Thread.sleep(delay);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
public void start() {
isPlay = true;
Thread t = new Thread(this);
t.start();
}
public void stop() {
isPlay = false;
}
private void input() {
int keyStates = getKeyStates();
if((keyStates&LEFT_PRESSED)!=0){
if(scnX-1>0)
scnX--;
}
if((keyStates&RIGHT_PRESSED)!=0)
if(scnX+141<backgroundImage.getWidth()){
scnX++;
}
// if((keyStates&UP_PRESSED)!=0){
// currentY = Math.max(0,currentY-1);
// }
// if((keyStates&DOWN_PRESSED)!=0){
// if(currentY+10<height){
// currentY = Math.min(height,currentY+1);
// }
// }
}
private void drawScreen(Graphics g) {
g.setColor(0xffff);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0x0000ff);
// playerSprite.setPosition(currentX,currentY);
// playerSprite.paint(g);
// backgroundSprite.paint(g);
// g.drawString("X",currentX,currentY,Graphics.TOP|Graphics.LEFT);
layerManager.setViewWindow(scnX,scnY,140,140);
layerManager.paint(g,20,20);
flushGraphics();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -