标题:在手机模拟器中显示不出来
只看楼主
大傻蛋
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2008-3-17
 问题点数:0 回复次数:1 
在手机模拟器中显示不出来
这是我照着书本做的第一个小游戏:
{ '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();
 }
}
搜索更多相关主题的帖子: 器中 手机 import javax microedition 
2008-03-17 16:24
prelover
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-6-16
得分:0 
主要错误在你的package srcpackge中:
public void Start(){
        m_bRunning = true;
        Thread thread = new Thread();
                   thread.start();                
          }


第三句应该是这样:
Thread thread = new Thread(this);
你写掉了this
2008-03-24 18:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-203842-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.371646 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved