The Graphics class allows you to draw various shapes onto another component.

The first thing to do is import the package:

	import java.awt.Graphics;

Next, you need to get the graphics context of the component.  Let's say we are using a Panel named pnlDraw:

	Graphics g = pnlDraw.getGraphics();

Finally, we draw our shapes be calling the appropriate method on the Graphics variable.

Review the JavaDocs for the following "draw" methods:

Each method takes a number of parameters, mostly to determine the position and size of the shaped to draw.

The "draw" methods draw the shape only.  For example, drawRect will draw an un-filled rectangle.  Most of these methods have a matching "fill" method as well.  For example, fillRect will draw a solid rectangle, filling the inside with the draw color.

You can change the draw color by calling the "setColor()" method of the Graphics variable.  Pass the method one of the following Color object:



Finally, we may want to clear everything off our panel to start again.  For this, we use the "clearRect()" method.  The problem is, we need to know the current width and height of our component.  It may have been in a Frame that was re-sized by the user, so we can't hard-code this.  Instead, we use a class called Dimension.

	private void clearPanel() {
		g = pnlDraw.getGraphics();
		Dimension d = pnlDraw.getSize();
		int w = (int)d.getWidth();
		int h = (int)d.getHeight();
		g.clearRect(0, 0, w, h);