Graphics

The following project file contains examples from the book and can be imported into Eclipse.

This web page provides an overview of Java's capabilities for drawing two-dimensional shapes, controlling colors and controlling fonts. One of Java’s initial appeal was its support for graphics that enabled programmers to visually enhance their applications. Here is an overview of a few of the key classes.

Coordinate System

A coordinate system is a scheme for identifying every point on the screen. The upper-left corner of a GUI component (e.g., a JPanel) has the coordinates (0, 0). A coordinate pair is composed of an x-coordinate (the horizontal coordinate) and a y-coordinate (the vertical coordinate). X-coordinates go from left to right. Y-coordinates go from top to bottom. Coordinate units are measured in pixels. A pixel is a display monitor’s smallest unit of resolution.

The paintComponent Method

A Graphics object manages the graphics for window components like JPanel so that pixels can be drawn on the screen. The Graphics object of a window component like JPanel can be most easily obtained as follows. Create a new class that extends JPanel. Override the paintComponent method in the new class. An instance of this class can then be added to a window.

When you create a GUI-based application, the event-dispatch thread (EDT) is used to process all GUI events. A thread is code that runs concurrently within a Java program. That is, a Java program consists of one or more threads. The first thread is started by running the main method of the program. If this program has no GUI, this might be the only thread of the program. However, when a GUI is displayed, the EDT will be started.

All drawing within GUI components should be performed in the EDT. Whenever the repaint method is called by the main thread, this task will be given to the EDT. Then, the paintComponent method of each component (more precisely, each JComponent) will be called, which is the appropriate place to use a Graphics object to draw on a window.

The Graphics Class

The Graphics class contains methods for drawing, font manipulation, color manipulation, etc. The draw and fill methods of Graphics (e.g, drawOval) will draw figures within a bounding rectangle, illustrated as follows.

To draw an oval, imagine a box just big enough to contain the oval. The drawOval method of Graphics will need four parameters: the x- and y-coordinates of the upper-left corner of the bounding rectangle, and the width and height of the bounding rectangle.

Colors

The Color class declares methods and constants for manipulating colors in a Java program. Every color is created from a red, a green and a blue component. These can be specified as integers in the range from 0 to 255, or floating-point values in the range 0.0 to 1.0. Larger values correspond to more of that particular color.

The getColor method of Graphics returns a Color object representing the current drawing color. The setColor method of Graphics sets the current drawing color. Any Graphics drawing or filling uses the current color.

Fonts

Most font methods and font constants are part of the Font class. Font’s constructor takes three arguments: the font name, font style and font size. The font name can be anything currently supported by the system on which the program is running, such as standard Java fonts Monospaced, SansSerif and Serif. The font style is Font.PLAIN, Font.ITALIC or Font.BOLD. Font styles can be used in combination. For printing, the font size is measured in points, where a point is 1/72 of an inch. In a window, one point typically equals one pixel. The setFont method of Graphics sets the current drawing font so that the drawString method of Graphics will display text in that font.

Examples

chapter13.zip contains the book's examples as an Eclipse project file that can be imported into Eclipse. The main method is in the DemoGraphics class.