📄 walkcatcanvas.java
字号:
/**
* 这个类是游戏的显示类,它将负责游戏的屏幕绘制工作
* 这个类是javax.microedition.lcdui.game.GameCanvas的子类
* @author Shengli Lin
*/
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.io.*;
public class WalkCatCanvas
extends javax.microedition.lcdui.game.GameCanvas {
/**
* // 漫步地平面高度
*/
static int groundHeight = 32;
/**
* 屏幕X坐标
*/
static int X;
/**
* 屏幕Y坐标
*/
static int Y;
/**
* 屏幕宽度
*/
static int screenWidth;
/**
* 屏幕高度
*/
static int screenHeight;
/**
* 字体高度
*/
static int fontHeight;
/**
* 缺省字体.
*/
static Font FONT;
/**
* Display 对象
*/
Display display;
/**
* MIDlet 对象
*/
WalkCatMIDlet automation;
/**
* LayerManager对象
*/
WalkManager walkManager;
/**
* 游戏结束标志
*/
static boolean gameOver;
/**
* 地面高度
*/
int GROUNDHEIGHT = 10;
/**
* 画布对象是否调用过paint方法标志
*/
boolean isInitialized = false;
/**
* 游戏画布构造器
*/
public WalkCatCanvas(WalkCatMIDlet midlet) {
super(false);
display = Display.getDisplay(midlet);
automation = midlet;
}
/**
* 启动方法,该方法在MIDlet启动时调用
*/
void start() {
gameOver = false;
display.setCurrent(this);
repaint();
}
/**
* 游戏画布重置
*/
void reset() {
walkManager.reset();
repaint();
}
/**
* 清除按键状态值
*/
void flushKeys() {
getKeyStates();
}
/**
* 屏幕绘图方法,该方法是画布类的核心方法,完成整个的画布的绘制工作。
*/
public void paint(Graphics g) {
if (!isInitialized) {
X = g.getClipX();
Y = g.getClipY();
screenWidth = g.getClipWidth();
screenHeight = g.getClipHeight();
FONT = g.getFont();
fontHeight = FONT.getHeight();
isInitialized = true;
}
// 清除屏幕
g.setColor(0x000000);
g.fillRect(X, Y, screenWidth, screenHeight);
g.setColor(0x00aaffbb);
g.fillRect(X, Y + screenHeight - GROUNDHEIGHT, screenWidth,
screenHeight);
// 如果walkManager对象为空则创建该对象
try {
if (walkManager == null) {
walkManager = new WalkManager(X, Y + fontHeight * 2,
screenWidth,
screenHeight - fontHeight * 2 -
GROUNDHEIGHT);
}
walkManager.paint(g);
}
catch (Exception e) {
e.printStackTrace();
showErrorMsg(g, e);
}
}
/**
* 通知LayerManager移动Layer对象
* 并且刷新显示屏画面
*/
void walk() {
walkManager.walk(1);
// 绘制屏幕
try {
paint(getGraphics());
flushGraphics();
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* 处理键盘事件
*/
public void checkKeys() {
if (!gameOver) {
int keyState = getKeyStates();
if ( (keyState & LEFT_PRESSED) != 0) {}
if ( (keyState & RIGHT_PRESSED) != 0) {}
if ( (keyState & UP_PRESSED) != 0) {}
}
}
/**
* 将异常对象转换错误信息并显示
*/
void showErrorMsg(Exception e) {
showErrorMsg(getGraphics(), e);
flushGraphics();
}
/**
* 将异常对象转换错误信息并显示
*/
void showErrorMsg(Graphics g, Exception e) {
if (e.getMessage() == null) {
showErrorMsg(g, e.getClass().getName());
}
else {
showErrorMsg(g, e.getClass().getName() + ":" + e.getMessage());
}
}
/**
* 将异常对象转换错误信息并显示
*/
void showErrorMsg(Graphics g, String msg) {
// 清除屏幕
g.setColor(0xffffff);
g.fillRect(X, Y, screenWidth, screenHeight);
int msgWidth = FONT.stringWidth(msg);
// 显示错误信息
g.setColor(0x00ff0000);
g.setFont(FONT);
g.drawString(msg, (screenWidth - msgWidth) / 2,
(screenHeight - fontHeight) / 2,
g.TOP | g.LEFT);
gameOver = true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -