Chapter 12. Graphics and Java 2D™
Chapter 12. Graphics and Java 2D™
To understand graphics contexts and graphics objects. To understand and be able to manipulate colors. To understand and be able to manipulate fonts. To use methods of class Graphics to draw lines, rectangles, rectangles with rounded corners, three-dimensional rectangles, ovals, arcs and polygons. To use methods of class Graphics2D from the Java 2D API to draw lines, rectangles, rectangles with rounded corners, ellipses, arcs and general paths. To be able to specify Paint and Stroke characteristics of shapes displayed with Graphics2D.
[Page 596] Outline 12.1 Introduction 12.2 Graphics Contexts and Graphics Objects 12.3 Color Control 12.4 Font Control 12.5 Drawing Lines, Rectangles and Ovals 12.6 Drawing Arcs 12.7 Drawing Polygons and Polylines 12.8 Java 2D API
12.9 Wrap-Up Summary Terminology Self-Review Exercises Answers to Self-Review Exercises Exercises
12.1. Introduction
In this chapter, we overview several of Java's capabilities for drawing two-dimensional shapes, controlling colors and controlling fonts. One of Java's initial appeals was its support for graphics that enabled programmers to visually enhance their applications. Java now contains many more sophisticated drawing capabilities as part of the Java 2D API. This chapter begins with an introduction to many of Java's original drawing capabilities. Next we present several of the more powerful Java 2D capabilities, such as controlling the style of lines used to draw shapes and the way shapes are filled with color and patterns. [Note: Several concepts covered in this chapter have already been covered in the optional GUI and Graphics Case Study of Chapters 310. So, some material will be repetitive if you read the case study. You do not need to read the case study to understand this chapter.] Figure 12.1 shows a portion of the Java class hierarchy that includes several of the basic graphics classes and Java 2D API classes and interfaces covered in this chapter. Class Color contains methods and constants for manipulating colors. Class JComponent contains method paintComponent, which will be used to draw graphics on a component. Class Font contains methods and constants for manipulating fonts. Class FontMetrics contains methods for obtaining font information. Class Graphics contains methods for drawing strings, lines, rectangles and other shapes. Class Graphics2D, which extends class Graphics, is used for drawing with the Java 2D API. Class Polygon contains methods for creating polygons. The bottom half of the figure lists several classes and interfaces from the Java 2D API. Class BasicStroke helps specify the drawing characteristics of lines. Classes GradientPaint and TexturePaint help specify the characteristics for filling shapes with colors or patterns. Classes GeneralPath, Line2D, Arc2D, Ellipse2D, Rectangle2D and RoundRectangle2D represent several Java 2D shapes. [Note: We begin the chapter by discussion Java's original graphics capabilities, then move on to the Java 2D API. However, it is important to understand that the classes discussed as part of Java's original graphics capabilities are now also considered to be part of the Java 2D API.] Figure 12.1. Classes and interfaces used in this chapter from Java's original graphics capabilities and from the Java 2D API. [Note: Class Object appears here because it is the superclass of the Java class hierarchy.]
To begin drawing in Java, we must first understand Java's coordinate system (Fig. 12.2), which is a scheme for identifying every point on the screen. By default, the upper-left corner of a GUI component (e.g., a window) has the coordinates (0, 0). A coordinate pair is composed of an xcoordinate (the horizontal coordinate) and a y-coordinate (the vertical coordinate). The xcoordinate is the horizontal distance moving right from the left of the screen. The y-coordinate is the vertical distance moving down from the top of the screen. The x-axis describes every horizontal coordinate, and the y-axis describes every vertical coordinate.
[Page 598] Figure 12.2. Java coordinate system. Units are measured in pixels. [View full size image]
Text and shapes are displayed on the screen by specifying coordinates. The coordinates are used to indicate where graphics should be displayed on a screen. Coordinate units are measured in pixels. A pixel is a display monitor's smallest unit of resolution. Portability Tip 12.1 Different display monitors have different resolutions (i.e., the density of the pixels varies). This can cause graphics to appear to be different sizes on different monitors or on the same monitor with different settings.
A Java graphics context enables drawing on the screen. A Graphics object manages a graphics context and draws pixels on the screen that represent text and other graphical object (e.g., lines, ellipses, rectangles and other polygons). Graphics objects contain methods for drawing, font manipulation, color manipulation and the like. Every application we have seen in the text that performs drawing on the screen has used the Graphics object g (the argument to the paintComponent method of a component such as a JPanel) to manage the application's graphics context. Class Graphics is an abstract class (i.e., Graphics objects cannot be instantiated). This contributes to Java's portability. Because drawing is performed differently on every platform that supports Java, there cannot be just one implementation of the drawing capabilities on all systems. For example, the graphics capabilities that enable a PC running Microsoft Windows to draw a rectangle are different from those that enable a Linux workstation to draw a rectangleand they are both different from the graphics capabilities that enable a Macintosh to draw a rectangle.
When Java is implemented on each platform, a subclass of Graphics is created that implements the drawing capabilities. This implementation is hidden from us by class Graphics, which supplies the interface that enables us to use graphics in a platform-independent manner. Class Component is the superclass for many of the classes in the java.awt package. (We discussed class Component in Chapter 11.) Class JComponent, which inherits indirectly from class Component, contains a paintComponent method that can be used to draw graphics. Method paintComponent takes a Graphics object as an argument. This object is passed to the paintComponent method by the system when a lightweight Swing component needs to be repainted. The header for the paintComponent method is
[Page 599]
public void paintComponent( Graphics g )
Parameter g receives a reference to an instance of the system-specific subclass that Graphics extends. The preceding method header should look familiar to youit is the same one we used in some of the applications in Chapter 11. Actually, class JComponent is a superclass of JPanel. Many capabilities of class JPanel are inherited from class JComponent. Method paintComponent is seldom called directly by the programmer because drawing graphics is an event-driven process. When a GUI application executes, the application container calls method paintComponent for each lightweight component as the GUI is displayed. For paintComponent to be called again, an event must occur (such as covering and uncovering the component with another window). If the programmer needs to have paintComponent execute (i.e., if the programmer wants to update the graphics drawn on the Swing component), a call is made to method repaint, which is inherited by all JComponents indirectly from class Component (package java.awt). Method repaint is frequently called by the programmer to request a call to method paintComponent. Method repaint should not be overridden, because it performs some system-dependent tasks. The header for repaint is
public void repaint()