<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    饒榮慶 -- 您今天UCWEB了嗎?--http://www.ucweb.com

    3G 手機開發(fā)網(wǎng)

       :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      99 Posts :: 1 Stories :: 219 Comments :: 0 Trackbacks
    http://www.3geye.net/bbs/thread-164-1-1.html

    MIDP2.0中提供了游戲開發(fā)專用的API,比如GameCanvas等類。他們位于javax.microedition.lcdui.game包內(nèi)。本文介紹GameCanvas的基本使用方法并實現(xiàn)一種滾動星空的效果。您可以參考Game Canvas Basic獲得更詳細的信息。

        GameCanvas是Canvas的子類,因此他同樣繼承了Canvas類的一些特性,比如showNotify()方法會在Canvas被顯示在屏幕的時候調(diào)用,而hideNotify()會在Canvas離開屏幕的時候被調(diào)用。我們可以把他們當(dāng)作{8}來使用,用于初始化和銷毀資源。比如
        // 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;
        }

    在游戲開發(fā)中最重要的就是接受用戶觸發(fā)的事件然后重新繪制屏幕,通常我們使用getKeyStates()方法判斷哪個鍵被按下了,然后繪制屏幕,調(diào)用flushGraphics()。在GameCanvas中,系統(tǒng)事實上已經(jīng)為我們實現(xiàn)了雙緩沖技術(shù),因此每次我們繪制的時候就是在off- screen上繪制的。結(jié)束后通過flushGraphics把它復(fù)制到屏幕上去。下面是典型的接受事件、處理邏輯、繪制屏幕的代碼。
     // Get the Graphics object for the off-screen buffer
    Graphics g = getGraphics();

    while (true) {
          // Check user input and update positions if necessary
          int keyState = getKeyStates();
          if ((keyState & LEFT_PRESSED) != 0) {
              sprite.move(-1, 0);
          }
          else if ((keyState & RIGHT_PRESSED) != 0) {
              sprite.move(1, 0);
          }

    // Clear the background to white
    g.setColor(0xFFFFFF);
    g.fillRect(0,0,getWidth(), getHeight());

          // Draw the Sprite
          sprite.paint(g);

          // Flush the off-screen buffer
          flushGraphics();
    }

        下面開始實現(xiàn)我們滾動星空的效果,其實設(shè)計的思想非常簡單。我們啟動一個線程,使用copyArea()方法把屏幕的內(nèi)容往下復(fù)制一個像素的距離。然后繪畫第一個空白的直線,隨機的在直線上繪畫點兒,這樣看起來就像星空一樣了。邏輯代碼如下:
        // 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.currentThread().sleep(sleepTime);
                } catch (InterruptedException e)
                {
                }
            }
        }

    下面給出源代碼
    /*
     * License
     *
     * Copyright 1994-2004 Sun Microsystems, Inc. All Rights Reserved.
     *
     */

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

    public class GameCanvasTest extends MIDlet implements CommandListener
    {

        private Display display;

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

        public GameCanvasTest()
        {
        }

        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();
            }
        }
    }
    /*
     * 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...

                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;
        }
    }






    爬蟲工作室 -- 專業(yè)的手機軟件開發(fā)工作室
    3G視線 -- 專注手機軟件開發(fā)
    posted on 2007-09-23 21:30 3G工作室 閱讀(1042) 評論(0)  編輯  收藏 所屬分類: j2me
    主站蜘蛛池模板: 国产成人精品日本亚洲专区6| 久久精品免费观看| 1区2区3区产品乱码免费| 亚洲精品成人片在线观看| 亚洲三级视频在线观看| 免费萌白酱国产一区二区三区 | 亚洲精品无码久久不卡| 亚洲日韩中文字幕一区| 免费观看美女用震蛋喷水的视频| 亚洲人成影院在线观看 | 卡一卡二卡三在线入口免费| 亚洲人成7777影视在线观看| 成全在线观看免费观看大全| a级亚洲片精品久久久久久久| 成人久久久观看免费毛片| 日本高清在线免费| 国产精品亚洲综合久久| 日本免费一本天堂在线| 亚洲国产精品久久久久秋霞影院 | 国产亚洲综合色就色| 国精产品一区一区三区免费视频| 中文字幕不卡亚洲| baoyu116.永久免费视频| 久久国产精品亚洲综合| 一个人免费观看视频www| 狠狠色伊人亚洲综合网站色| 卡1卡2卡3卡4卡5免费视频| 国产综合成人亚洲区| 亚洲av中文无码乱人伦在线咪咕| 亚洲av无码专区首页| 国产成人免费午夜在线观看| 84pao强力永久免费高清| 久久精品国产亚洲av日韩| 99在线免费观看视频| 亚洲精品乱码久久久久久久久久久久| 亚洲午夜精品一区二区麻豆| 91精品免费观看| 亚洲人成高清在线播放| 亚洲第一成年免费网站| 亚洲av永久无码精品天堂久久| 亚洲视频在线免费播放|