Softkey.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.spacemission;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/**
* Custom softkey implementation
*/
public class Softkey {
public final static int SOFTKEY_HEIGHT = 32;
public final static int PLAY = 0;
public final static int PAUSE = 1;
public final static int NEW_GAME = 2;
public final static int EXIT = 3;
private Image background;
private Image backgroundPressed;
private Image label;
private Listener listener;
private boolean pressed;
private boolean visible = true;
private final int type;
private int x = 0;
private int y = 0;
private int width = 0;
private int height = 0;
/**
* Interface for listening softkey events
*/
public interface Listener {
void clicked(int type);
}
public Softkey(String labelPath, int type, Canvas canvas, Listener listener) {
this.type = type;
this.listener = listener;
try {
label = Image.createImage(labelPath);
int displayWidth = canvas.getWidth();
int displayHeight = canvas.getHeight();
Image pattern = Image.createImage("/framework/softkey_background.png");
width = displayWidth / 2;
height = SOFTKEY_HEIGHT;
x = type == EXIT ? width : 0;
y = displayHeight - height;
background = fillImage(pattern, width);
pattern = Image.createImage("/framework/softkey_background_pressed.png");
backgroundPressed = fillImage(pattern, width);
}
catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
/**
* Render the softkey
* @param g Graphics object
*/
public void paint(Graphics g) {
if (visible) {
Image bg = pressed ? backgroundPressed : background;
int offsetY = pressed ? 1 : 0;
g.drawImage(bg, x, y, Graphics.LEFT | Graphics.TOP);
g.drawImage(label, x + width / 2, y + height / 2 + offsetY,
Graphics.HCENTER | Graphics.VCENTER);
}
}
public void pointerPressed(int x, int y) {
if (visible) {
pressed = contains(x, y);
}
}
public void pointerReleased(int x, int y) {
if (!visible) {
return;
}
if (pressed && contains(x, y)) {
listener.clicked(type);
}
pressed = false;
}
/**
* Check whether a point is inside the sofkey bounding box
* @param x
* @param y
*/
public boolean contains(int x, int y) {
return x > this.x && x < this.x + width && y > this.y && y < this.y + height;
}
/**
* Repeats background image horizontally to fill the softkey area
* @param pattern
* @param width
* @return Result background image
*/
private Image fillImage(Image pattern, int width) {
Image img = Image.createImage(width, pattern.getHeight());
Graphics g = img.getGraphics();
int markerX = 0;
while (markerX < width) {
g.drawImage(pattern, markerX, 0, Graphics.LEFT | Graphics.TOP);
markerX += pattern.getWidth();
}
return img;
}
public boolean isVisible() {
return visible;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
public void setPosition(int x, int y) {
this.x = x;
this.y = y;
}
}