在手机模拟器中显示不出来
这是我照着书本做的第一个小游戏:{ 'H', 'R', 'T', 'W', 'T' }
{ 'E', 'I', 'H', 'O', 'H' }
{ 'L', 'G', 'I', 'R', 'R' }
{ 'L', 'H', 'N', 'L', 'E' } { 'O', 'T', 'G', 'D', 'E' }
进行上、下、左、右的移动是纵向拼写正确,
但是运行之后,在手机模拟器中显示不出来,白板一个
没有找到那个地方错了!
第一次抄写无从下手!望各位大侠给于指点!
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDletStateChangeException;
import srcpackge.MainCanvas;
public class WordsMidlet extends MIDlet {
//定义MainCanvas的属性,进行两个框架的关联
private static MainCanvas m_MainCanvas;
public WordsMidlet() {
super();
}
//Midlet提供了这三个状态的方法
//开始状态的方法
protected void startApp() throws MIDletStateChangeException {
try {
//根据MainCanvas对象的引用调用相映的方法
m_MainCanvas = new MainCanvas();
} catch (Exception ex) {
ex.printStackTrace();
}
//Display提供显示的方法
Display.getDisplay(this).setCurrent(m_MainCanvas);
}
//暂停状态的方法
protected void pauseApp() {
m_MainCanvas.Stop();
}
//销毁状态的方法
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
m_MainCanvas.Stop();
}
}
==========================================
package srcpackge;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
public class MainCanvas extends GameCanvas implements Runnable {
private boolean m_bRunning;
//定义一个二维数组,存储字母
public char m_achWord[][] = {
{ 'H', 'R', 'T', 'W', 'T' },
{ 'E', 'I', 'H', 'O', 'H' },
{'L', 'G', 'I', 'R', 'R' },
{ 'L', 'H', 'N', 'L', 'E' },
{ 'O', 'T', 'G', 'D', 'E' }
};
//记录当前的行
public int m_nCurLine = 0;
//记录每行的向左偏差位数
public int m_anDis[] = { 0, 1, 2, 3, 4 }
//组合成功的标志
public boolean m_bGameOK = false;
public MainCanvas() {
super(true);
Start();// 启动线程
}
public void Start() {
m_bRunning = true;
Thread thread = new Thread();
thread.start();//启动线程
}
//实现了Runnable,所以要必须run()方法
public void run() {
long t1 = System.currentTimeMillis();
long t2 = t1;
while (m_bRunning) {
t2 = System.currentTimeMillis();
if (t2 - t1 > 100) {
t1 = t2;
Input();
Logic();
Paint();
}
}
}
//停止
public void Stop() {
m_bRunning = false;
}
public void Input() {
if (m_bGameOK)
return;
int KeyStates = getKeyStates();//得到当前按键的状态
//如果按的是方向键的上键,则调整当前行的位置
if ((KeyStates & GameCanvas.UP_PRESSED) != 0)
m_nCurLine--;
//如果按的是方向键的下键,则调整当前行的位置
if ((KeyStates & GameCanvas.DOWN_PRESSED) != 0)
m_nCurLine++;
if (m_nCurLine < 0)
m_nCurLine += 5;
if (m_nCurLine > 4)
m_nCurLine -= 5;
//如果按的是方向键的左键,则当前行的所有字母左移
if ((KeyStates & GameCanvas.LEFT_PRESSED) != 0)
m_anDis[m_nCurLine]++;
/如果按的是方向键的左键,则当前行的所有字母右移
if ((KeyStates & GameCanvas.RIGHT_PRESSED) != 0)
m_anDis[m_nCurLine]--;
if (m_anDis[m_nCurLine] < 0)
m_anDis[m_nCurLine] += 5;
if (m_anDis[m_nCurLine] > 4)
m_anDis[m_nCurLine] -= 5;
}
public void Logic() {
if (m_bGameOK)
return;
m_bGameOK = true;
for (int n = 0; n < 5; n++) {
if (m_anDis[n] != 0) {
m_bGameOK = false;
break;
}
}
}
public void Paint() {
Graphics g = getGraphics();
// 用黑色清屏
g.setColor(0);
g.fillRect(0, 0, getWidth(), getHeight());
// realCol为所显示的字母在原始矩阵中的列数
int realCol = 0;
for (int row = 0; row < 5; row++) {
if (row == m_nCurLine)
g.setColor(0x0000FF00);
else
g.setColor(0xffffffff);
for (int col = 0; col < 5; col++) {
realCol = m_anDis[row] + col;
if (realCol > 4)
realCol = realCol - 5;
g.drawChar(m_achWord[row][realCol], 20 + col * 15, row * 15, 0);
}
}
g.setColor(0xffffff);
if (m_bGameOK)
g.drawString("真棒,完全正确", 20, 80, 0);
else
g.drawString("左右移动字符,使纵向拼写正确", 10, 80, 0);
flushGraphics();
}
}