HelloCanvas.java
/**
* Copyright (c) 2013 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;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
/**
* A very simple canvas that has a painted background and a single line of
* text in the center. A simple interaction support is implemented: Tapping the
* canvas area toggles the visibility of the text.
*/
public class HelloCanvas extends Canvas {
private static final String CANVAS_TEXT = "Tap screen to hide text!";
private boolean textVisible = true;
/**
* Constructor.
*/
public HelloCanvas() {
}
/**
* Shows the text if hidden.
*/
public void showText() {
if (!textVisible) {
textVisible = true;
repaint();
}
}
/**
* From Canvas.
*
* This method is called when the canvas area is pressed.
*/
protected void pointerPressed(int x, int y) {
// Toggle the text visibility
textVisible = !textVisible;
repaint();
}
/**
* From Canvas.
*
* Paints the background of the canvas blue, and white text to the center of
* the screen.
*/
public void paint(Graphics graphics) {
final int width = getWidth();
final int height = getHeight();
graphics.setColor(0, 51, 240); // Mainly blue
graphics.fillRect(0, 0, width, height);
if (textVisible) {
Font font = graphics.getFont();
graphics.setColor(255, 255, 255); // White
graphics.drawString(CANVAS_TEXT,
width / 2,
(height - font.getHeight()) / 2,
Graphics.TOP | Graphics.HCENTER);
}
}
}