Java Reference
In-Depth Information
int y = 0;
g.setGrayScale (255);
g.fillRect (0, 0, getWidth(), getHeight());
g.setGrayScale (0);
for (int size = 0; size < sizes.length; size++) {
for (int face = 0; face < faces.length; face++) {
int x = 0;
for (int style = 0; style < styles.length; style++) {
font = Font.getFont
(faces [face], styles [style], sizes
[size]);
g.setFont (font);
g.drawString
("Test", x+1, y+1, Graphics.TOP |
Graphics.LEFT);
g.drawRect
(x, y, font.stringWidth ("Test")+1,
font.getHeight() + 1);
x += font.stringWidth ("Test")+1;
}
y += font.getHeight() + 1;
}
}
}
}
Figure 3.15 shows the output of the FontDemo example.
Figure 3.15. Output of the FontDemo example.
Images
The Graphics class also provides a method for drawing images. As shown in the final version of
TeleTransfer application, Image s can be predefined and contained in the JAR file of the MIDlet.
The only file format that is mandatory for MIDP is the Portable Network Graphics (PNG) file format.
The PNG format has several advantages over other graphics formats; for example, it is license free and
supports true color images, including a full transparency (alpha) channel. PNG images are always
compressed with a loss-less algorithm. The algorithm is identical to the algorithm used for JAR files,
so the MIDP implementation can save space by using the same algorithm for both purposes.
An image can be loaded from the JAR file using the static method Image.create (String
name ) . The name parameter denotes the filename of the image in the JAR file. Please note that this
create() method may throw an IOException .
The drawImage() method in Graphics requires an Image object, the coordinates, and an integer
denoting the alignment as parameters. The alignment parameter is similar the alignment of
drawString() , except that the BASELINE constant is not supported. An additional alignment
constant available for images only is VCENTER , which forces the image to be vertically centered
 
 
 
Search WWH ::




Custom Search