⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blockgamemidlet.java

📁 J2ME MIDP_Example_Applications
💻 JAVA
字号:
// Copyright 2003 Nokia Corporation.
//
// THIS SOURCE CODE IS PROVIDED 'AS IS', WITH NO WARRANTIES WHATSOEVER,
// EXPRESS OR IMPLIED, INCLUDING ANY WARRANTY OF MERCHANTABILITY, FITNESS
// FOR ANY PARTICULAR PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE
// OR TRADE PRACTICE, RELATING TO THE SOURCE CODE OR ANY WARRANTY OTHERWISE
// ARISING OUT OF ANY PROPOSAL, SPECIFICATION, OR SAMPLE AND WITH NO
// OBLIGATION OF NOKIA TO PROVIDE THE LICENSEE WITH ANY MAINTENANCE OR
// SUPPORT. FURTHERMORE, NOKIA MAKES NO WARRANTY THAT EXERCISE OF THE
// RIGHTS GRANTED HEREUNDER DOES NOT INFRINGE OR MAY NOT CAUSE INFRINGEMENT
// OF ANY PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OWNED OR CONTROLLED
// BY THIRD PARTIES
//
// Furthermore, information provided in this source code is preliminary,
// and may be changed substantially prior to final release. Nokia Corporation
// retains the right to make changes to this source code at
// any time, without notice. This source code is provided for informational
// purposes only.
//
// Nokia and Nokia Connecting People are registered trademarks of Nokia
// Corporation.
// Java and all Java-based marks are trademarks or registered trademarks of
// Sun Microsystems, Inc.
// Other product and company names mentioned herein may be trademarks or
// trade names of their respective owners.
//
// A non-exclusive, non-transferable, worldwide, limited license is hereby
// granted to the Licensee to download, print, reproduce and modify the
// source code. The licensee has the right to market, sell, distribute and
// make available the source code in original or modified form only when
// incorporated into the programs developed by the Licensee. No other
// license, express or implied, by estoppel or otherwise, to any other
// intellectual property rights is granted herein.


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


// BlockGameMIDlet manages which screen is currently displayed.
// The MIDlet screen classes never directly call Display.setCurrent()
// themselves. The UI screens always make callbacks to BlockGameMIDlet
// whenever the displayed screen should be changed. This centralizes
// management of the display resource into a single place
// (class BlockGameMIDlet) in the application code.

public class BlockGameMIDlet
    extends MIDlet
{
    private final Dictionary dictionary;
    private final MainMenu mainMenu;
    private final GameEffects gameEffects;

    private GameManager gameManager = null;
    private SettingsScreen settingsScreen = null;


    public BlockGameMIDlet()
    {
        // The dictionary contains the strings used by the user interface.
        // The dictionary might be configured for internationalization
        // support, based on the values of the microedition.locale
        // and microedition.encoding properties. For this reason, all
        // MIDlet UI screens use the same configured dictionary to get
        // any string values they need.
        dictionary =
            new Dictionary(System.getProperty("microedition.locale"),
                           System.getProperty("microedition.encoding"));

        gameEffects = makeGameEffects();
        mainMenu = new MainMenu(this, dictionary, gameEffects);
    }


    boolean useLimitedFiringRate()
    {
        // The .jad must set the property BlockGame-UseLimitedFiringRate
        // to be "true" to limit the firing rate (e.g. on devices that
        // support multiple key presses). Otherwise an unlimited
        // firing rate is used.

        String property = getAppProperty("BlockGame-UseLimitedFiringRate");
        return (((property != null) && property.equals("true")));
    }


    // MIDlet methods

    public void startApp()
    {
        Displayable current = Display.getDisplay(this).getCurrent();

        if (current == null)
        {
            // Use a splash screen, the first time we are called.
            // The main menu screen will be shown after the splash.
            SplashScreen splashScreen = new SplashScreen(this, mainMenu);

            Display.getDisplay(this).setCurrent(splashScreen);
            splashScreen.start(); // disappear after a fixed time
        }
        else
        {
            Display.getDisplay(this).setCurrent(current);
            if ((gameManager != null) &&
                (current == gameManager.getCanvas()))
            {
                gameManager.resume();
            }
        }
    }


    public void pauseApp()
    {
        if (gameManager != null)
        {
            gameManager.pause();
        }
    }


    public void destroyApp(boolean unconditional)
    {
        if (gameManager!= null)
        {
            gameManager.stop();
        }
    }


    // User Interface screen callbacks


    // GameManager callback

    void gameManagerMainMenu(boolean isGameOver)
    {
        // If the game is over, remove the
        // 'Continue' command from the MainMenu.
        if (isGameOver)
        {
            mainMenu.deleteContinue();
        }
        else
        {
            // highlight 'Continue' in the main menu
            mainMenu.selectContinue();
        }

        Display.getDisplay(this).setCurrent(mainMenu);
    }


    // MainMenu callbacks

    void mainMenuContinue()
    {
        Display.getDisplay(this).setCurrent(gameManager.getCanvas());
    }


    void mainMenuNewGame()
    {
        // gameManager uses the canvas for repaints,
        // determining canvas width, height, etc.
        Canvas closeableCanvas = makeCloseableCanvas();
        gameManager = new GameManager(this, dictionary,
                                      gameEffects, closeableCanvas);
        // The canvas delegates drawing of the game and handling of
        // keyPressed, keyReleased or 'closePressed' events to gameManager.
        ((SettableDelegate)closeableCanvas).setDelegate(gameManager);
        gameManager.start();

        // Set the display to be the game manager's canvas.
        Display.getDisplay(this).setCurrent(closeableCanvas);

        // After the game manager's canvas is displayed and
        // the game is running, the 'Continue' option will be
        // needed from the main menu.
        mainMenu.addContinue();
    }


    void mainMenuSettings()
    {
        if (settingsScreen == null)
        {
            settingsScreen = new SettingsScreen(this, dictionary, mainMenu);
        }
        Display.getDisplay(this).setCurrent(settingsScreen);
    }


    void mainMenuInstructions()
    {
        String title = dictionary.getString(Dictionary.LABEL_INSTRUCTIONS);
        String back = dictionary.getString(Dictionary.LABEL_BACK);
        String text = dictionary.getString(Dictionary.TEXT_INSTRUCTIONS);
        if (useLimitedFiringRate())
        {
            text += dictionary.getString(Dictionary.TEXT_INSTRUCTIONS_GAUGE);
        }
        TextScreen screen = new TextScreen(this, title, text, back);
        Display.getDisplay(this).setCurrent(screen);
    }


    void mainMenuAbout()
    {
        String name = getAppProperty("MIDlet-Name");
        String version = dictionary.getString(Dictionary.LABEL_VERSION) +
                         " " + getAppProperty("MIDlet-Version");
        String vendor = getAppProperty("MIDlet-Vendor");

        String about = name + "\n" + version + "\n" + vendor;
        String title = dictionary.getString(Dictionary.LABEL_ABOUT);
        String back = dictionary.getString(Dictionary.LABEL_BACK);
        TextScreen aboutScreen = new TextScreen(this, title, about, back);
        Display.getDisplay(this).setCurrent(aboutScreen);
    }


    void mainMenuExit()
    {
        destroyApp(false);
        notifyDestroyed();
    }


    // SettingEditor callbacks

    void settingEditorSave(String name, boolean isOn)
    {
        // update game setting and settings screen
        if (name.equals(dictionary.getString(Dictionary.LABEL_VIBRATION)))
        {
            Settings.setUseVibration(isOn);
            settingsScreen.setUseVibration(isOn);
        }
        else if (name.equals(dictionary.getString(Dictionary.LABEL_SOUND)))
        {
            Settings.setUseSound(isOn);
            settingsScreen.setUseSound(isOn);
        }

        Alert confirm = new Alert(null,
            (name + " " + settingsScreen.onOffString(isOn)),
            null, AlertType.INFO); // no title
        confirm.setTimeout(4000); // show Alert for 4 seconds

        Display.getDisplay(this).setCurrent(confirm, settingsScreen);
    }


    void settingEditorBack()
    {
        Display.getDisplay(this).setCurrent(settingsScreen);
    }


    // SettingsScreen callbacks

    void settingsScreenBack(Displayable last)
    {
        settingsScreen = null; // It can be garbage collected now.

        if ((gameManager != null) && (last == gameManager.getCanvas()))
        {
            gameManager.resume();
        }
        Display.getDisplay(this).setCurrent(last);
    }


    void settingsScreenEdit(String name, boolean isOn)
    {
        Display.getDisplay(this).setCurrent(
            new SettingEditor(this, dictionary, name, isOn));
    }


    // SplashScreen callback

    void splashScreenDone(Displayable next)
    {
        Display.getDisplay(this).setCurrent(next);
    }


    // TextScreen callbacks (i.e. About + Instructions)

    void textScreenClosed()
    {
        Display.getDisplay(this).setCurrent(mainMenu);
    }


    // Factory-like methods
    //
    // The Nokia vendor-specific APIs FullCanvas, Sound, and DeviceControl
    // are used if available. If not, use an alternative based on
    // the capabilities of standard MIDP instead.

    private GameEffects makeGameEffects()
    {
        try
        {
            Class.forName("com.nokia.mid.sound.Sound");
            Class.forName("com.nokia.mid.ui.DeviceControl");

            // If no exception was thrown, use the vendor-specific APIs.
            Class clas = Class.forName("NokiaGameEffects");
            return (GameEffects) clas.newInstance();
        }
        catch (Exception e)
        {
            // The vendor-specific APIs are not available,
            // so use the 'stub' GameEffects instead.
            return new GameEffects();
        }
    }


    private Canvas makeCloseableCanvas()
    {
        try
        {
            Class.forName("com.nokia.mid.ui.FullCanvas");

            // If no exception was thrown, use a 'closeable Canvas'
            // based on the vendor-specific FullCanvas class.
            Class clas = Class.forName("NokiaCloseableCanvas");
            return (Canvas) clas.newInstance();
        }
        catch (Exception e)
        {
            // If the vendor-specific FullCanvas API isn't available,
            // use the default CloseableCanvas (derived from Canvas).
            String closeLabel = dictionary.getString(dictionary.LABEL_BACK);
            return new CloseableCanvas(closeLabel);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -