ImageView.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.sudokumaster;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class ImageView extends View {
    private Image image;
    private Listener listener;

    public ImageView(Image image) {
        this(image, null);
    }

    public ImageView(Image image, Listener listener) {
        super(0, 0, image.getWidth(), image.getHeight());
        this.image = image;
        this.listener = listener;
    }

    protected void paint(Graphics g) {
        g.drawImage(image, left + width/2, top + height/2, Graphics.HCENTER | Graphics.VCENTER);
    }

    public boolean handlePointerEvent(int type, int x, int y) {
        if(!isVisible() || listener == null) return false;
        if(hits(x, y)) {
            if(type == POINTER_RELEASED) listener.onClick();
            return true;
        }
        return false;
    }

    public static interface Listener {
        void onClick();
    }
}