Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Java ME (J2ME) > Прокрутка меню


Автор: Zamuta 21.3.2006, 23:59
Код

package hello;

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



public class Kernel extends MIDlet {
    protected Display display;
    private Image splashLogo;
    private boolean isSplash = true;
    private MainMenuScreen mainMenuScreen;
    private SettingsScreen settingsScreen;
    private HighScoreScreen highScoreScreen;
    private HelpScreen helpScreen;
    private AboutScreen aboutScreen;    
    public Kernel() {
   
        }
    public void startApp() {
        display = Display.getDisplay(this);
        mainMenuScreen = new MainMenuScreen(this);
        settingsScreen = new SettingsScreen(this);
        highScoreScreen = new HighScoreScreen(this);
        helpScreen = new HelpScreen(this);
        aboutScreen = new AboutScreen(this);
        if(isSplash) {
            isSplash = false;
            try {
                splashLogo = Image.createImage("/splash.png");
                new SplashScreen(display, mainMenuScreen, splashLogo,3000);
                } catch(Exception ex) {
                    mainMenuScreenShow();
                    }
            } else {
            mainMenuScreenShow();
            }
        }
    public Display getDisplay() {
        return display;
        }
    public void pauseApp() {
        }
    public void destroyApp(boolean unconditional) {
        System.gc();
        notifyDestroyed();
        }
    private Image createImage(String filename) {
        Image image = null;
        try {
            image = Image.createImage(filename);
            } catch (Exception e) {
                }return image;
                }
    public void mainMenuScreenShow() {
        display.setCurrent(mainMenuScreen);
        }
    public void settingsScreenShow() {
        display.setCurrent(settingsScreen);
        }
    public void highScoreScreenShow() {
        display.setCurrent(highScoreScreen);
        }
    public void helpScreenShow() {
        display.setCurrent(helpScreen);
        }
    public void aboutScreenShow() {
        display.setCurrent(aboutScreen);
        }
    public void mainMenuScreenQuit() {
        destroyApp(true);
        }
    }




Код

package hello;


import javax.microedition.lcdui.*;



public class MainMenuScreen extends List implements CommandListener {
  
    private Kernel midlet;
    private Command selectCommand = new Command("Select", Command.ITEM,1);
    private Command exitCommand = new Command("Exit", Command.EXIT,1);
    private Alert alert;
    public MainMenuScreen(Kernel midlet) {
       
        super("Name List",Choice.IMPLICIT);
        this.midlet = midlet;


      

    
        append(Locale.get( "menu.StartGame"),null);
        
        append(Locale.get( "menu.LoadGame"),null);
      
        append(Locale.get( "menu.Highscore"), null);
   
        append(Locale.get( "menu.Quit"),null);
       
        append(Locale.get( "menu.Help"),null);
        addCommand(exitCommand);
        addCommand(selectCommand);
        setCommandListener(this);
        }
    public void commandAction(Command c, Displayable d) {
        if (c == exitCommand) {
            midlet.mainMenuScreenQuit();
            } else if (c == selectCommand) {
            processMenu();
            } else {
            processMenu();
            }
        }
    
    private void processMenu() {
        try {
               List down = (List)midlet.display.getCurrent();
            switch (down.getSelectedIndex()) {
                case 0: scnNewGame(); break;
                case 1: scnSettings(); break;
                case 2: scnHighScore(); break;
                case 3: scnHelp(); break;
                case 4: scnAbout(); break;
                };
                } catch (Exception ex) {
                    // Proper Error Handling should be done here
                    System.out.println("processMenu::"+ex);
                    }
        }
    private void scnNewGame() {
        midlet.mainMenuScreenShow();
        }
    private void scnSettings() {
        midlet.settingsScreenShow();
        }
    private void scnHighScore() {
        midlet.highScoreScreenShow();
        }
    private void scnHelp() {
        midlet.helpScreenShow();
        }
    private void scnAbout() {
        midlet.aboutScreenShow();
        }
    }




Во вемя работы на телефоне (проверял на SE K700), создаётся впечатление что строчка перепрыгивает на следующий пункт меню автоматически, при однократном движении джойстика, т. е. нужна небольшая задержка. Кто знает как сделать?

Автор: javastic 22.3.2006, 11:33
Никак. List работает с той задержкой которая в нём определена.

Автор: DEMOVERSION 22.3.2006, 12:47
а Thread.sleep() попробовать?

Автор: Zamuta 26.3.2006, 05:30
Не буду создавать новую тему, спрошу здесь...


public static Image createImage(String name) - что такое здесь name.
throws IOException

В доках пишут:

Creates an immutable image from decoded image data obtained from the named resource. The name parameter is a resource name as defined by Class.getResourceAsStream(name). The rules for resolving resource names are defined in the Application Resource Files section of the java.lang package documentation.

Что такое имя ресурсов?

Автор: batigoal 26.3.2006, 10:25
Может, это просто путь к файлу так хитро обозвали?

Автор: redrick 26.3.2006, 13:43
The rules for resolving resource names are defined in the Application Resource Files section of the java.lang package documentation.

читаем:
Application Resource Files

Application resource files are accessed using getResourceAsStream(String name) in java.lang.Class . In the MIDP specification, getResourceAsStream is used to allow resource files to be retrieved from the MIDlet Suite's JAR file.

Resource names refer to the contents of the MIDlet Suite JAR file. Absolute pathnames, beginning with "/" are fully qualified file names within the jar file.

Relative pathnames, not beginning with "/" are relative to the class upon which getResourceAsStream is called. Relative names are converted to absolute by prepending a "/" followed by the fully qualified package with "." characters converted to "/" and a separator of "/". The resulting string is reduced to canonical form by applying as many times as possible the following:

* All occurences of "/./" are replaced with "/".
* All occurences of "/segment/../" are replaced with "/" where segment does not contain "/".

The canonical resource name is the absolute pathname of the resource within the JAR.

In no case can the path extend outside the JAR file, and resources outside the JAR file MUST NOT be accessible. For example, using "../../" does NOT point outside the JAR file. If there are any remaining "." or ".." characters they are treated literally in locating the resource. No resource can exist with that name so null is returned from Class.getResourceAsStream. Also, devices MUST NOT allow classfiles to be read from the JAR file as resources, but all other files MUST be accessible.



Lamer George, ну вощем да =) нро все ж написано =)

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)