Graphics Class

The Windows Foundation Classes for Java (WFC) library contains enough different types of components to solve most of the problems you will encounter in creating apps. You will occasionally, however, create a program in which you need to draw directly into a window. In these cases, you must rely on the Graphics class and the closely related PaintEvent class, both contained in the package. This chapter is devoted to the explanation of those two classes.

This chapter begins by creating a couple of freehand, "Etch-a-Sketch"-like drawing programs. From there, we will move on to examine some of the special considerations that come into play when drawing, rather than writing, text to the screen. The Graphics class includes a set of simple drawing objects in its tool set. Too many different drawing objects exist in this class for me to create example programs for each, but I can demonstrate the use of one of these as an example for the rest.

The chapter ends by discussing one of the most difficult problems that arises while using the Graphics object: combining freehand painting that uses Graphics objects together with WFC controls in the same form. Again, I can't demonstrate every combination, but I will present the solution to one common but sticky problem. The final version of all the sample code in this chapter is in the Windows apps subfolder on the companion CD.

The Graphics class described in this chapter is part of WFC. The Graphics class that is part of the Abstract Windowing Toolkit (AWT) is similar, but not identical.