/**
 *
 * @author Peter Miklosko
 * @homepage http://www.peterscorner.co.uk
 */

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

public class SensorCanvas extends Canvas
{
    private String mMessage = "Press the key!";
    private int mKeyCode;
    private SensorData sd;
    private int xCor, yCor, zCor;
    
    public SensorCanvas(SensorData sData)
    {
        sd = sData;        
    }
    
    public void paint(Graphics g)
    {
        int w = getWidth();
        int h = getHeight();
        
        g.setColor(0xffffff);
        g.fillRect(0, 0, w, h);
        
        int fh = g.getFont().getHeight();
        int y = h / 2 - fh * 2;       
        
        g.setColor(0x000000);
        g.drawString(mMessage, w / 2, y, Graphics.HCENTER | Graphics.BOTTOM);
        y += fh;
               
        if(mKeyCode != 0)
        {     
            // Get sensor data
            xCor = sd.getX();
            yCor = sd.getY();
            zCor = sd.getZ();
            g.drawString("X coordinate: " + xCor, w / 2, y, Graphics.HCENTER | Graphics.BOTTOM);
            y += fh;
            g.drawString("Y coordinate: " + yCor, w / 2, y, Graphics.HCENTER | Graphics.BOTTOM);
            y += fh;
            g.drawString("Z coordiante: " + zCor, w / 2, y, Graphics.HCENTER | Graphics.BOTTOM);            
        }       
    }
    
    protected void keyPressed(int keyCode)
    {
        mMessage = "KeyPressed()";
        if(getGameAction(keyCode) == GAME_A)
        {
            mKeyCode = keyCode;     
            repaint();
        }
    }
    
    protected void keyReleased(int keyCode)
    {
        mMessage = "keyReleased()";
        if(getGameAction(keyCode) == GAME_A)
        {
            mKeyCode = keyCode;     
            repaint();
        }
    }
}

