Java Reference
In-Depth Information
Input streams, which are covered on Day 15, “Working with Input and Output,” are
objects that can load data from a source such as a disk file or web address. The following
statements load a font from a file named Verdana.ttf in the same folder as the class file
that uses it:
try {
File ttf = new File(“Verdana.ttf”);
FileInputStream fis = new FileInputStream(ttf);
Font font = Font.createFont(Font.TRUETYPE_FONT, fis);
} catch (IOException ioe) {
System.out.println(“Error: “ + ioe.getMessage());
ioe.printStackTrace();
}
The try - catch block handles input/output errors, which must be considered when data is
loaded from a file. The File , FileInputStream , and IOException classes are part of the
java.io package and are discussed in depth on Day 15.
When a font is loaded with createFont() , the Font object will be 1 point and plain
style. To change the size and style, call the font object's deriveFont( int , int ) method
with two arguments: the desired style and size.
Improving Fonts and Graphics with Antialiasing
If you displayed text using the skills introduced up to this point, the appearance of the
font would look crude compared to what you've come to expect from other software.
Characters would be rendered with jagged edges, especially on curves and diagonal lines.
Java2D can draw fonts and graphics much more attractively using its support for
antialiasing , a rendering technique that smooths out rough edges by altering the color of
surrounding pixels.
This functionality is off by default. To turn it on, call a Graphics2D object's
setRenderingHint() method with two arguments:
A RenderingHint.Key object that identifies the rendering hint being set
n
A RenderingHint.Key object that sets the value of that hint
n
The following code enables antialiasing on a Graphics2D object named comp2D :
comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
By calling this method in the paintComponent() method of a component, you can cause
all subsequent drawing operations to employ antialiasing.
Search WWH ::




Custom Search