Fonts and Graphics


Two classes that deal with fonts are Font and FontMetric The easiest way to change a font for a component or when drawing graphics is to use setFont.
setFont("Font name", font style, point size);

Example Fonts, styles and point sizes
Font nameFont stylePoint size

To view this applet you need a Java-enabled browser


FontMetric class is used to get the "metric" of font. For example the size of a certain character in pixels. The font name, the ascent/descent of a font. This class is only useful if you require such information as you may if you are trying to center text of any font regardless of size. Otherwise the information in this class is rather useless.

Graphics(Section still incomplete)

The AWT includes a Graphics class that allows for drawing of primitives, regions(filled or not), manipulation of fonts and colors. There are classes for manipulating and drawing images, double-buffering, etc.

But the Graphics class is abstract and thus we can not just create an instance of it. So we must get a Graphics instance. How? paint(), update(), etc. methods have one and getGraphics() might return you one that you can use. So instead of overriding your Container's paint() method try to extend a Canvas to draw graphics on rather than manipulating a Panel or Frame.

To view this applet you need a Java-enabled browser

[Advanced graphics material to be added]

[Q & A's]

Q: Are there any fast 2D/3D libraries for Java such as OpenGL, 3DR, QuickDraw, WinG?
A: None that I know of. Many are probably being worked on.

Q: How do you draw an image without flickering?
A: Use double buffering. Check out the answer in the FAQ

[Next] [Prev] [Home]
Nelson Yu
Last modified: Jan 2 1996