ListItem.java

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

import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class ListItem {

    private String text;
    private Image background;
    private int x;
    private int y;
    public static final int WIDTH = 240;
    public static final int HEIGHT = 40;

    public ListItem(String text) {
        this.text = text;
        loadImages();
    }

    private void loadImages() {
        InputStream in = getClass().getResourceAsStream("/listitem_bg.png");
        try {
            if (in != null) {
                background = Image.createImage(in);
            }
        }
        catch (IOException e) {
            throw new RuntimeException(
                    "Unable to instantiate Listitem class. Imageloading problem.");
        }
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            }
            catch (IOException e) {
            }
        }
    }

    public void render(Graphics g) {
        g.drawImage(background, x, y, g.LEFT | g.TOP);
        g.setColor(0x000000);//black
        g.drawString(
                text,
                (x + (WIDTH / 2)),
                y + (HEIGHT / 2),
                g.HCENTER | g.BASELINE);
    }

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