Beruflich Dokumente
Kultur Dokumente
Chapter 04
Low-Level User Interface
4.1 Objectives
After finishing this lesson, the student should be able to:
understand low-level event handling in MIDP
draw and display text, images, lines, rectangles and arcs
specify color, font and stroke for drawing operations
understand and utilize the Canvas and the Graphics classes
4.2 Introduction
In the previous lesson, we discussed how to make user interfaces like lists, forms and
input fields. Those are high-level user interfaces and the programmer does not have to
worry about painting the screen pixels or positioning screen text. All the program has to
specify are the type of components and the element labels. The system would handle
screen painting, scrolling and layout.
One drawback of using only the high-level UI components is that the program does not
have full control of the screen. There are times when we want to draw lines, animate
images and have pixel-level control of the screen.
In this lesson, we will be dealing directly with drawing on the screen. We will be studying
the Canvas class, which will be the backdrop of our drawing. We will also delve into the
Graphics class, which has methods for drawing lines, rectangles, arcs and text. We will
also discuss fonts, colors and images.
4.3 Canvas
The Canvas is a subclass of Displayable. It is an abstract class that must be extended or
subclassed before an application can make use of its functionalities.
The Canvas can be mixed with the high-level Displayable subclass Screen. The program
can switch to and from a Canvas and a Screen.
J.E.D.I.
The Canvas defines empty event-handling methods. Applications must override them in
order to handle events.
The Canvas class defines an abstract method called paint(). Applications using the Canvas
class must provide an implementation for the paint() method.
4.3.1
The coordinate system of the Canvas is zero-based. The x and y coordinates start with 0.
The upper left hand corner of the Canvas is coordinate (0,0). The x-coordinates increase
from left to right, while the y-coordinates increase from top to bottom. The methods
getWidth() and getHeight() return the width and height of the Canvas, respectively.
The lower right corner of the screen has coordinates (getWidth() -1, getHeight()-1). Any
changes to the available paintable size of the Canvas is reported to the application by the
sizeChanged() method. The available size of the Canvas may change if there is a switch
between normal and full-screen modes or addition/removal of components like
Commands.
4.3.2
"Hello, world!"
J.E.D.I.
J.E.D.I.
addCommand(exitCommand);
setCommandListener(this);
}
J.E.D.I.
With the "Hello, world!" midlet, we defined a class that extends Canvas.
class HelloCanvas extends Canvas implements CommandListener {
Then we added a command ("Exit") and set the command listener.
addCommand(exitCommand);
setCommandListener(this);
We create a command listener by implementing the CommandListener class. This means
creating a class that has a commandAction method.
class HelloCanvas extends Canvas implements CommandListener {
...
4.3.3
Commands
Just like the List, TextBox and Form, the Canvas can have Commands attached and can
listen for command events. The steps for adding a Command to a Canvas are the same:
1.
J.E.D.I.
2.
Use addCommand() to attach the command to the Canvas (or Form, List, TextBox)
addCommand(exitCommand);
3.
Use setCommandListener() to register which class gets command events for
commands in this Displayable
setCommandListener(this);
4.
Create a commandListener by implementing the class CommandListener and
providing a commandAction() method
4.3.4
Key Events
The Canvas subclasses can listen for key events by overriding these methods:
keyPressed(int keyCode)
keyRepeated(int keyCode)
keyReleased(int keyCode)
4.3.5
Game Actions
Each key code may be mapped to a game action. A key code may be mapped to at most
one game action. The Canvas class defines these game actions: UP, DOWN, LEFT, RIGHT,
FIRE, GAME_A, GAME_B, GAME_C, GAME_D. The program can translate a key code into a
game action using the getGameAction(keyCode).
J.E.D.I.
The getKeyCode(int gameAction) method returns the key code associated with a game
action. A game action can have more than one key code associated with it. If there are
more than one key codes associated with a game action, only one key code will be
returned.
An application should use getGameAction(int keyCode) instead of directly using the
defined key codes. Normally, if a program wants to listen for an "UP" key, it would use
KEY_NUM2 or the device-specific key code for the UP button. Programs using this method
are not portable since devices may have different key layouts and different key codes.
KEY_NUM2 might be the "UP key" for one device, but it might also be the "LEFT key" for
another device. getGameAction() would always return UP, independent of which key was
pressed as long as it is the "UP" key that is in the context of the device's key layout.
4.3.6
Pointer Events
Aside from key events, MIDP programs can also handle pointer events. This is true if the
device has a pointer and it is implemented in the Java system of the device.
The method hasPointerEvents() returns true if the device supports pointer press and
release events. The method hasPointerMotionEvents() returns true if the device supports
pointer motion events (dragging).
The events that may be generated by pointer activity are: pointerPressed, pointerReleased
and pointerDragged. An application must override these methods in order to be notified
when these events occur. The (x,y) coordinates of the event (where the pointer was
pressed/released/dragged) are specified in these callback methods.
protected void pointerPressed(int x, int y)
protected void pointerReleased(int x, int y)
protected void pointerDragged(int x, int y)
4.4 Graphics
The Graphics class is the main class for drawing text, images, lines, rectangles and arcs.
It has methods for specifying color, font and stroke.
4.4.1
Color
The Display class has methods for determining if the device has color support, and the
number of colors or gray levels supported by the device.
J.E.D.I.
To set the color to be used for the next Graphics method(s), use setColor() method.
setColor() has two forms:
4.4.2
Fonts
A Font has three attributes: face, style and size. Fonts are not created by the application.
Instead, the application queries the system for certain font attributes and the system
returns a font that matches these attributes. The system does not guarantee that it will
return a font with all the requested attributes. If the system does not have a font that
matches the request, it will return a font that is the closest match with respect to the
J.E.D.I.
request attributes.
The Font is a separate class. As stated above, the application does not create the Font
objects. Instead, the static methods getFont() and getDefaultFont() are used to request a
font from the system:
public
static
getFont(int fontSpecifier)
STYLE_BOLD | STYLE_ITALIC
The font size can be: SIZE_SMALL, SIZE_MEDIUM, or SIZE_LARGE
The following methods return specific font attributes:
public
public
public
public
public
public
public
4.4.3
int getStyle()
int getFace()
int getSize()
boolean isPlain()
boolean isBold()
boolean isItalic()
boolean isUnderlined()
Stroke style
The method setStrokeStyle(int style) sets the stroke style that will be used for drawing
lines, arcs, rectangles and rounded rectangles. Stroke style does not affect text, image
and fill drawing operations.
J.E.D.I.
4.4.4
Clipping
A clipping area is a rectangular area in the current Graphics object. Any graphics
operations will only affect pixels inside the clip area. Pixels outside the clipping area are
not affected by any graphics operations.
public void
sets the current clip area to the
setClip(int x, int y, int width, int height) rectangle specified by the
corrdinates
public int getClipX()
4.4.5
Anchor Points
Text are drawn relative to an anchor point. The method drawString() expects an (x,y)
coordinate relative to an anchor point:
10
J.E.D.I.
4.4.6
Drawing Text
public void
drawString(String str,
int x,
int y,
int anchor)
public void
drawSubstring(String str,
int offset,
int len,
int x,
int y,
int anchor)
public void
drawChar(char character,
int x,
int y,
int anchor)
public void
11
J.E.D.I.
public void
drawString(String str,
int x,
int y,
int anchor)
drawChars(char[] data,
int offset,
int length,
int x,
int y,
int anchor)
Here are some methods of Font that are useful in drawing text:
12
J.E.D.I.
4.4.7
Drawing Lines
13
J.E.D.I.
// line from the upper left corner to the lower right corner of the screen
g.drawLine(0, 0, getWidth()-1, getHeight()-1);
4.4.8
0);
Drawing Rectangles
14
J.E.D.I.
Figure 6: Output of using the same parameters for drawRect and fillRect
// set the pen color to black
g.setColor(0, 0, 0);
15
J.E.D.I.
4.4.9
Drawing Arcs
16
J.E.D.I.
x,
y,
width,
height,
startAngle,
arcAngle)
x,
y,
width,
height,
startAngle,
arcAngle)
g.setColor(255, 0, 0);
g.drawArc(18, 18, 50, 50, 0, 360); // draw a circle
g.setColor(0,
255, 0);
g.setColor(0, 0, 255);
g.fillArc(100, 200, 80, 100,
0, 90);
17
J.E.D.I.
try {
Image image = Image.createImage("/jedi.png");
g.drawImage(image,
getWidth()/2, getHeight()/2,
Graphics.VCENTER | Graphics.HCENTER);
} catch (Exception e){}
18
J.E.D.I.
4.5 Exercises
4.5.1
Key Codes
Create a MIDlet that will display the codes and names of the keys pressed by the user. Use
a Canvas and place the code and name at the center of the display.
19
J.E.D.I.
20