GameCanvas制作动态星空

来源:互联网 发布:cf弹道优化鼠标 编辑:程序博客网 时间:2024/04/28 03:53

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.midlet.*;

public class GameCanvas1 extends MIDlet implements CommandListener
{

    private Display display;

    public static final Command exitCommand = new Command("Exit", Command.EXIT,
            1);

    public GameCanvas1()
    {
    }

    public void commandAction(Command c, Displayable d)
    {
        if (c == exitCommand)
        {
            exitMIDlet();
        }
    }

    protected void destroyApp(boolean unconditional)
            throws MIDletStateChangeException
    {
        exitMIDlet();
    }

    public void exitMIDlet()
    {
        notifyDestroyed();
    }

    public Display getDisplay()
    {
        return display;
    }

    protected void initMIDlet()
    {
        GameCanvas c = new StarField();
        c.addCommand(exitCommand);
        c.setCommandListener(this);

        getDisplay().setCurrent(c);
    }

    protected void pauseApp()
    {
    }

    protected void startApp() throws MIDletStateChangeException
    {
        if (display == null)
        {
            display = Display.getDisplay(this);
            initMIDlet();
        }
    }
}

----------------------------------------------------------------------------------------------------------------------------------------------------

import java.util.Random;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;

// A simple example of a game canvas that displays
// a scrolling star field. Use the UP and DOWN keys
// to speed up or slow down the rate of scrolling.

public class StarField extends GameCanvas implements Runnable
{

    private static final int SLEEP_INCREMENT = 10;

    private static final int SLEEP_INITIAL = 150;

    private static final int SLEEP_MAX = 300;

    private Graphics graphics;

    private Random random;

    private int sleepTime = SLEEP_INITIAL;

    private volatile Thread thread;

    public StarField()
    {
        super(true);

        graphics = getGraphics();
        graphics.setColor(0, 0, 0);
        graphics.fillRect(0, 0, getWidth(), getHeight());
    }

    // The game loop.

    public void run()
    {
        int w = getWidth();
        int h = getHeight() - 1;
        while (thread == Thread.currentThread())
        {
            // Increment or decrement the scrolling interval
            // based on key presses
            int state = getKeyStates();

            if ((state & DOWN_PRESSED) != 0)
            {
                sleepTime += SLEEP_INCREMENT;
                if (sleepTime > SLEEP_MAX)
                    sleepTime = SLEEP_MAX;
            } else if ((state & UP_PRESSED) != 0)
            {
                sleepTime -= SLEEP_INCREMENT;
                if (sleepTime < 0)
                    sleepTime = 0;
            }

            // Repaint the screen by first scrolling the
            // existing starfield down one and painting in
            // new stars...

            graphics.copyArea(0, 0, w, h, 0, 1, Graphics.TOP | Graphics.LEFT);
            graphics.setColor(0, 0, 0);
            graphics.drawLine(0, 0, w, 0);
            graphics.setColor(255, 255, 255);
            for (int i = 0; i < w; ++i)
            {
                int test = Math.abs(random.nextInt()) % 100;
                if (test < 5)
                {
                    graphics.drawLine(i, 0, i, 0);
                }
            }
            flushGraphics();

            // Now wait...

            try
            {
                Thread.sleep(sleepTime);
            } catch (InterruptedException e)
            {
            }
        }
    }

    // When the canvas is shown, start a thread to
    // run the game loop.

    protected void showNotify()
    {
        random = new Random();
        thread = new Thread(this);
        thread.start();
    }
    // When the game canvas is hidden, stop the thread.

    protected void hideNotify()
    {
        thread = null;
    }
}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 纸胶带撕不下来怎么办 拼多多卖不出去怎么办 联想键盘被锁了怎么办 台式电脑打不开机怎么办 文件名中不能用特殊符号怎么办 高铁喷雾扣留后怎么办 高铁没收的东西怎么办 安检被收的东西怎么办 我的律师骗我怎么办 没婆婆生了小孩怎么办 没人帮你带孩子怎么办 亲戚在家里不走怎么办 穷人家的孩子该怎么办 空腹吃李子胃疼怎么办 情侣空间农场谷仓空间不够怎么办 王者荣耀情侣解除对方不同意怎么办 oppo手机进了水怎么办 淘宝卖号被骗了怎么办 淘宝买号被骗了怎么办 后脑偏头疼怎么办最快最有效 脸两边的骨头大怎么办 做b超胎儿老盘腿怎么办 裤子白边染色了怎么办 异地恋要分手了怎么办 异地恋没话题了怎么办 陪婆婆聊天心情超级郁闷怎么办? 他不想理你了怎么办 陌陌看到信息不回怎么办 qq的文档看不了怎么办 怀孕了分手了怎么办啊 qq节日祝福关了怎么办 微信欠款不还怎么办 qq文件记录删除了怎么办 qq漫游记录删了怎么办 换了新手机微信怎么办 新手机了微信怎么办 微信收藏空间已满怎么办 微信收藏空间满了怎么办 qq步数上传不了怎么办 qq发送的文件失效了怎么办 苹果手机微信数据损坏怎么办