MenuItem.java

/**
* Copyright (c) 2012-2013 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.explonoid.menu;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.Sprite;

/**
 * Represents a menu item.
 */
public class MenuItem {
    protected final Sprite sprite;
    private volatile boolean selected = false;

    public MenuItem(Sprite sprite) {
        this.sprite = sprite;
    }

    public void setSelected(boolean s) {
        selected = s;
        sprite.setFrame(getFrame());
    }

    protected int getFrame() {
        return selected ? 1 : 0;
    }

    public boolean isSelected() {
        return selected;
    }

    public boolean isVisible() {
        return sprite.isVisible();
    }

    public void setVisibile(boolean bln) {
        sprite.setVisible(bln);
    }

    public int getWidth() {
        return sprite.getWidth();
    }

    public int getHeight() {
        return sprite.getHeight();
    }

    public int getX() {
        return sprite.getX();
    }

    public int getY() {
        return sprite.getY();
    }

    public void setCenter(int x, int y) {
        sprite.setPosition(x - sprite.getWidth()/2, y - sprite.getHeight()/2);
    }

    public void setHorizontalCenter(int x) {
        sprite.setPosition(x - sprite.getWidth()/2, sprite.getY());
    }

    public void setVerticalCenter(int y) {
        sprite.setPosition(sprite.getX(), y - sprite.getHeight()/2);
    }

    public void setPosition(int x, int y) {
        sprite.setPosition(x, y);
    }

    /**
     * Check whether a point is inside the bounding rectangle of the menu item
     */
    public boolean hits(int x, int y) {
        int left = sprite.getX();
        int right = left + sprite.getWidth();
        int top = sprite.getY();
        int bottom = top + sprite.getHeight();
        return x > left && x < right && y > top && y < bottom;
    }

    /**
     * Render menu item
     */
    public void paint(Graphics g) {
        sprite.paint(g);
    }
}