Menu.java

/*
 * Copyright © 2011 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */   

package com.nokia.example.racer.views;

import com.nokia.example.racer.Main;
import com.nokia.example.racer.MainNonTouch;
import com.nokia.example.racer.helpers.ImageLoader;
import com.nokia.example.racer.helpers.KeyCodes;
import com.nokia.example.racer.views.components.ExitItem;
import com.nokia.example.racer.views.components.MenuItem;
import com.nokia.example.racer.views.components.SettingsItem;
import com.nokia.example.racer.views.components.StartItem;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

public class Menu extends GameCanvas{

    private Image background;
    private Image shader;
    

    private Timer mTimer;
    private Graphics g;

    private StartItem startitem;
    private SettingsItem settingsitem;
    private ExitItem exititem;
    private MenuItem [] items;
    private int currentActiveItem = 0;
    public Menu() {
        super(false);
        try {
            ImageLoader loader = ImageLoader.getInstance();
            background = loader.loadImage("/splash_screen.png");
            shader = loader.loadImage("/shader.png");
        }catch(IOException io) {

        }
        setFullScreenMode(true);
        items = new MenuItem[3];
        startitem = new StartItem();
        startitem.setPosition(0, 100);
        settingsitem = new SettingsItem();
        settingsitem.setPosition(0, 169);
        try {
            Class.forName("com.nokia.mid.ui.gestures.GestureInteractiveZone");
            exititem = new ExitItem(Main.getInstance());
        }catch(ClassNotFoundException e) {
            exititem = new ExitItem(MainNonTouch.getInstance());
        }
        
        exititem.setPosition(0, 238);
        items[0] = startitem;
        items[1] = settingsitem;
        items[2] = exititem;
    }

    public void showNotify() {

        mTimer = new Timer();
        g = getGraphics();
        TimerTask ui = new TimerTask() {
          public void run()  {
              render(g);
              flushGraphics();
          }
        };
        mTimer.schedule(ui, 0, 30);
    }

    public void hideNotify() {
        mTimer.cancel();
    }

    private void render(Graphics g) {
        int anchor = g.TOP|g.LEFT;
        g.drawImage(background, 0, 0, anchor);
        g.drawImage(shader, 0, 0, anchor);

        startitem.render(g);
        settingsitem.render(g);
        exititem.render(g);
    }

    protected void pointerPressed(int x, int y) {
        startitem.pointerPressed(x, y);
        settingsitem.pointerPressed(x, y);
        exititem.pointerPressed(x, y);
    }

    protected void pointerReleased(int x, int y) {
        startitem.pointerReleased(x,y);
        settingsitem.pointerReleased(x,y);
        exititem.pointerReleased(x,y);
    }

    protected void keyPressed(int keyCode) {
        switch(keyCode) {
            case KeyCodes.UP:
                items[currentActiveItem].setInactive();
                if(currentActiveItem == 0) {
                    currentActiveItem = 2;
                }else {
                    currentActiveItem--;
                }
                items[currentActiveItem].setActive();
                break;
            case KeyCodes.DOWN:
                items[currentActiveItem].setInactive();
                if(currentActiveItem == 2) {
                    currentActiveItem = 0;
                }else {
                    currentActiveItem++;
                }
                items[currentActiveItem].setActive();
                break;
            case KeyCodes.MIDDLE_SOFT_KEY:
                items[currentActiveItem].onSelected();
                break;
        }

    }



}