Item.java
/*
* Copyright © 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.racer.views.components;
import com.nokia.example.racer.helpers.ImageLoader;
import com.nokia.example.racer.views.View;
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/*
* This is an implementation for an image item. For clickable image item, see
* Button
*/
public class Item {
protected Image image;
protected int x = 0;
protected int y = 0;
public Item() {
}
public Item(String imgUrl) {
init(imgUrl);
}
/**
* Initializes the image item. Loads its image.
*
* @param imgUrl Path to the image
*/
public final void init(String imgUrl) {
ImageLoader loader = ImageLoader.getInstance();
try {
image = loader.loadImage(imgUrl);
}
catch (IOException io) {
}
}
public void render(Graphics g) {
int anchor = Graphics.TOP | Graphics.LEFT;
g.drawImage(image, x, y, anchor);
}
/**
* Sets absolute position related to the screen
*
* @param x x coordinate of the position
* @param y y coordinate of the position
*/
public void setPosition(int x, int y) {
this.x = x;
this.y = y;
}
/**
* Sets relative position where 0.0 x/y is at left/top border and 1.0 is at
* right/bottom border. The image anchor is centered.
*
* @param x relative x of the position
* @param y relative y of the position
*/
public void setPositionRel(float x, float y) {
this.x = (int) (View.screenWidth * x - image.getWidth() / 2);
this.y = (int) (View.screenHeight * y - image.getHeight() / 2);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Image getImage() {
return image;
}
}