Color Help
Help is available for each task,
or you can go straight to the Choice
source code to see a
solution.
Task 1
Create an applet called FontTest. In the init method, ask for and store in an instance variable, the list of available fonts.
import java.awt.*;
public class FontTest extends java.applet.Applet {
String[] fontList;
public void init() {
fontList = Toolkit.getDefaultToolkit().getFontList();
}
...
}
Task 2
In the paint method, construct a new Font from each name in the font list. Set the font of the graphics context passed in as an argument and get the font metrics (getFontMetrics). Draw the String 'fontList[i] + " 16 point Bold"' at 10 pixels over and n pixels below the last String where n is the height of the current font. Use a bold weight and 16 points.
public class FontTest extends java.applet.Applet {
...
public void paint(Graphics g) {
Font theFont;
FontMetrics fm;
int fontHeight = 0;
// for each font name in the font list
for (int i = 0; i < fontList.length; i++) {
// create a new Font with current name
// bold weight and 16 point.
theFont = new Font(fontList[i], Font.BOLD, 16);
// set the font of the graphics context
g.setFont(theFont);
// get the dimensions of the fonts
fm = getFontMetrics(theFont);
// drop down the height of this font
fontHeight += fm.getHeight();
// actually draw the string
g.drawString(fontList[i] + " 16 point Bold", 10, fontHeight);
}
}
}
|