Java Reference
In-Depth Information
class PointerDemoCanvas extends Canvas {
String eventType = "Press Pointer!";
int x;
int y;
public void pointerPressed (int x, int y) {
eventType = "Pointer Pressed";
this.x = x;
this.y = y;
repaint();
}
public void pointerReleased (int x, int y) {
eventType = "Pointer Released";
this.x = x;
this.y = y;
repaint();
}
public void pointerDragged (int x, int y) {
eventType = "Pointer Repeated";
this.x = x;
this.y = y;
repaint();
}
public void paint (Graphics g) {
g.setGrayScale (255);
g.fillRect (0, 0, getWidth(), getHeight());
g.setGrayScale (0);
g.drawString (eventType + " " +x +"/"+y,
0, 0, Graphics.TOP|Graphics.LEFT);
g.drawLine (x-4, y, x+4, y);
g.drawLine (x, y-4, x, y+4);
}
}
Foreground and Background Notifications
For several reasons, the Canvas may move into the background—for example, if the display is set to
another displayable object or if the device displays a system dialog. In these cases, the Canvas is
notified by the hideNotify() method. When the Canvas becomes visible (again), the
corresponding counterpart, showNotify() , is called.
Javagochi Example
Now that you are familiar with the Canvas object and the basic drawing methods of the Graphics
class, you are ready to develop a small interactive application, the Javagochi .
As you can see in the following code, the MIDlet implementation of Javagochi is already finished,
but the Face class is missing:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Javagochi extends MIDlet {
Search WWH ::




Custom Search