使用GameCanvas制作星空效果[3]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

    protected void startApp() throws MIDletStateChangeException
    {
        if (display == null)
        {
            display = Display.getDisplay(this);
            initMIDlet();
        }
    }
}
/*
 * License
 *
 * Copyright 1994-2004 Sun Microsystems, Inc. All Rights Reserved.
 */

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...

本文关键:使用GameCanvas制作星空效果
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top