DisplayCanvas.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.overlay;
//
// Source File Name:   DisplayCanvas.java
/**
 * DisplayCanvas displays image captured in CameraCanvas
 */
import java.io.IOException;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

class DisplayCanvas extends Canvas
    implements CommandListener
{
    /** Parent MIDlet */
    private final VideoOverlayMIDlet midlet;

    /** Image on Canvas */
    private Image image;

    /** Back command to go back to VideoCanvas */
    private Command backCommand = new Command("Back", Command.BACK, 0);

    /** Background color of DisplayCanvas */
    private final int BACKGROUND_COLOR = 0x000000; // Black

    /** Back button location x */
    private int exitX = 0;
    /** Back button location y */
    private int exitY = 0;
    /** Back button icon */
    private Image back_icon;

    /** Canvas full screen mode flag */
    private boolean fsMode;

    /**
     * DisplayCanvas(OverlayMIDlet midlet)
     * DisplayCanvas displays image captured in CameraCanvas
     */
    public DisplayCanvas(VideoOverlayMIDlet midlet)
    {
        image = null;
        this.midlet = midlet;
        addCommand(backCommand);
        setCommandListener(this);
        createImage();
    }

    public void paint(Graphics g)
    {
        g.setColor(BACKGROUND_COLOR);
        g.fillRect(0, 0, getWidth(), getHeight());
        if(image != null)
            g.drawImage(image, getWidth()/2, getHeight()/2,
                        Graphics.VCENTER | Graphics.HCENTER);
        // In fullscreen mode, drawing exit button
        if(fsMode)
        {
            // Landscape
            exitX = image.getWidth();
            exitY = image.getHeight() - (back_icon.getHeight()+20);

            g.drawImage(back_icon,
                        (exitX+getWidth())/2, exitY,
                        Graphics.TOP | Graphics.LEFT);
        }
    }

    /**
     * setImage(byte pngImage[])
     * Creating image from pngImage[]
     */
    void setImage(byte pngImage[])
    {
        image = Image.createImage(pngImage, 0, pngImage.length);
    }

    /**
     * setMode(boolean mode)
     * Setting fsMode value
     */
    void setMode(boolean mode)
    {
        fsMode = mode;
    }

    /**
     * Creating image from resources for Back button
     */
    public void createImage()
    {
        try
        {
            back_icon = Image.createImage("/buttons/back.png");
        }
        catch(IOException ex)
        {
            back_icon = null;
        }
    }

    public void commandAction(Command c, Displayable d)
    {
        if(c == backCommand)
        {
            image = null;
            midlet.displayCanvasBack(true);
        }
    }
    /**
     * pointerReleased(int x, int y)
     * Pointer events in ControlBar
     */
    protected void pointerReleased(int x, int y)
    {
        // In full screen mode, exit is allowed
        if(fsMode)
        {
            if(y >= exitY  && x > exitX)
            {
                // Closing displayCanvas
                image = null;
                midlet.displayCanvasBack(true);
                return;
            }
        }
    }

}