Week 8 Lab Notes - Graphics & Painting

Recommended Reading:

Be sure to look at the Working With Graphics section of the tutorial to get the dope on Graphics.

Look also at the API documentation on Graphics.

Important Zonker Info:

Zonker has moved. It is now found at the IP address: These changes should be replicated through DNS fairly soon so that the zonker.cs.slcc.edu name should work presently.

Zonker also now has 128mb of RAM. (Now if we could just get a motherboard upgrade...)

Graphics Concepts

A Graphics object in Java represents a Graphics Context. A Graphics Context is all of the information about the painting capabilities specific to a particular drawing area. Manipulations done with a Graphics object will only affect that area as well.

The Graphics object lets you draw points, lines, shapes, change colors, fill regions, change the font, and display text. As a general rule, if you want to display something in your applet and it isn't a widget (button, list, menu, etc.) you're going to use a Graphics object to do it.

As a general rule, all of your painting should happen in your applet's paint() method. If you want to gain access to a Graphics context outside of the paint() method, use the getGraphics() method. This is a member of the Component class, from which all GUI display components are derived.

Look at the API reference for Graphics for all the gory details.

Very often, your drawing is done on a Canvas. We will discuss this guy (and all his pals) when we talk about layout managers and widgets. I just want you to be aware that this guy exists.

The Java docs on Sun's Website really push the Graphics2D stuff. I probably won't be getting into any of that, but you're welcome to peruse it on your own.

Auxiliary Classes

The following are some handy auxiliary classes used when doing graphics:

Example Applet

Here's an example of an applet that implements a variety of Graphics calls.

Here's the source code.

Examples on Zonker

I'll try to put some examples up on Zonker. They'll probably be either in the /usr/local/java/demos directory or /usr/local/java/examples. (Check with me later to be sure.)

3/01/99 - Initial Revision
3/02/99 - Added info about zonker & example applet