Graphics Class Basics

The Graphics and Graphics2D classes provide us with standard primitive graphics routines that we can use to draw graphics. Here are some of the most commonly used methods available in the Graphics class.

setColor(Color col)

A Graphics object has a current color state associated with it. When a primitive routine, such as the fillRect method, is invoked, similar to when we used this combination in the paint methods earlier, the rectangle specified is filled with the current color associated with the Graphics object. You can create colors using the java.awt.Color class. Here are a few examples of creating Color objects. The Color class includes common static colors represented by an RGBA (red, green, blue, alpha) integer value, where the alpha value is opaque. The alpha value determines the transparency of the color with respect to what is behind. We can set the color state of the graphics object to blue as follows:


There are various constructors of the Color class for creating your own color objects. An example of this is specifying the RGB value of the color by red, green, and blue values as integer parameters in the range 0 to 255, where (0, 0, 0) is black and (255, 255, 255) is white, or as floating-point parameters in the range 0.0 to 1.0, where (0.0, 0.0, 0.0) is black and (1.0, 1.0, 1.0) is white.

Color col = new Color(255, 0, 0); // red Color col = new Color(1.0, 0.0, 0.0); // red also


The dispose method should be called when you are finished with a Graphics object. This method disposes of the object and releases any system resources it is using. You should call this method when you have finished with any Graphics objects you have created manually. Therefore, you do not need to call this method in component paint methods, such as the paint method we used earlier, as this is handled for you where it is passed a Graphics object that is automatically disposed internally. The finalize method of a Graphics object performs the dispose task, but the garbage collector is not likely to call the finalize method right away, so it's best to do this manually as soon as you are finished with the Graphics object. Note that we will look at the garbage collector in , "Game Programming Techniques."

So in short, if you are manually creating a Graphics object, as we shall see later, and are not being sent one through a paint method, it's best to call dispose after it. We will see about using Graphics objects in examples later in this chapter, and you will get the hang of this as we work through various examples.

drawLine(int x1, int y1, int x2, int y2)

This method simply draws a straight line from the points (x1, y1) to (x2, y2) in the current color state of the Graphics object. Note that clipping is handled for you when drawing to an image using drawing methods in the Graphics class.

drawRect(int x, int y, int width, int height) and fillRect(int x, int y, int width, int height)

These methods draw the outline of and fill a rectangular area specified by their parameters, respectively. Note that if you have an area with location (0, 0) and dimension (10, 10), then drawRect will add a boundary filling pixels at (0, 0) to (10, 10), whereas fillRect will fill all pixels in between, including the area (0, 0) to (9, 9). So if you want to draw a pixel border around your component, you would call:

Graphics.drawRect(0, 0, getWidth()-1, getHeight()-1)

...whereas if you wanted to fill the area, you would simply call:

Graphics.fillRect(0, 0, getWidth(), getHeight()).

drawString(String s, int x, int y)

This method draws a string at the specified location. The drawing of the text depends on the font associated with the Graphics object. Similar to having a current color, the Graphics object has a current font that you may alter accordingly. We will see about using fonts a little later in the chapter.

These methods are probably the most common and will undoubtedly become common knowledge to you as you get used to rendering graphics in Java.