Java Reference
In-Depth Information
Example 7−1: UnicodeDisplay.java (continued)
// Draw the headings in a special font
g.setFont(headingfont);
// Draw 0..F on top
for(int i=0; i < 16; i++) {
String s = Integer.toString(i, 16);
g.drawString(s, x0 + i*charspacing, y0-20);
}
// Draw column down left.
for(int i = 0; i < 16; i++) {
int j = start + i*16;
String s = Integer.toString(j, 16);
g.drawString(s, 10, y0+i*lineheight);
}
// Now draw the characters
g.setFont(font);
char[] c = new char[1];
for(int i = 0; i < 16; i++) {
for(int j = 0; j < 16; j++) {
c[0] = (char)(start + j*16 + i);
g.drawChars(c, 0, 1, x0 + i*charspacing, y0+j*lineheight);
}
}
}
/** Custom components like this one should always have this method */
public Dimension getPreferredSize() {
return new Dimension(x0 + 16*charspacing,
y0 + 16*lineheight);
}
}
}
Character Encodings
Text representation has traditionally been one of the most difficult problems of
internationalization. Java, however, solves this problem quite elegantly and hides
the difficult issues. Java uses Unicode internally, so it can represent essentially any
character in any commonly used written language. As I noted earlier, the remain-
ing task is to convert Unicode to and from locale-specific encodings. Java includes
quite a few internal byte-to-char and char-to-byte converters that handle convert-
ing locale-specific character encodings to Unicode and vice versa. While the con-
verters themselves are not public , they are accessible through the
InputStreamReader and OutputStreamWriter classes, which are character streams
included in the java.io package.
Any program can automatically handle locale-specific encodings simply by using
these character stream classes to do their textual input and output. Note that the
FileReader and FileWriter classes use these streams to automatically read and
write text files that use the platform's default encoding.
Search WWH ::




Custom Search