604 Graphics (Web hosting ratings) and Java2D Chapter 11 +x (0,
Monday, December 17th, 2007604 Graphics and Java2D Chapter 11 +x (0, 0) X axis Y axis (x, y) +y Fig. 11.2Java coordinate system. Units are measured in pixels. 11. Portability Tip 11.1 Different display monitors have different resolutions (i.e., the density of pixels varies). This can cause graphics to appear to be different sizes on different monitors. 11.2 Graphics Contexts and Graphics Objects A Java graphics context enables drawing on the screen. A Graphics object manages a graphics context by controlling how information is drawn. Graphics objects contain methods for drawing, font manipulation, color manipulation and the like. Every applet we have seen in the text that performs drawing on the screen has used the Graphicsobject g(the argument to the applet s paintmethod) to manage the applet s graphics context. In this chapter, we demonstrate drawing in applications. However, every technique shown here can be used in applets. The Graphics class is an abstract class (i.e., Graphics objects cannot be instantiated). This contributes to Java s portability. Because drawing is performed differently on each platform that supports Java, there cannot be one class that implements drawing capabilities on all systems. For example, the graphics capabilities that enable a PC running Microsoft Windows to draw a rectangle are different from the graphics capabilities that enable a UNIX workstation to draw a rectangle and those are both different from the graphics capabilities that enable a Macintosh to draw a rectangle. When Java is implemented on each platform, a derived class of Graphicsis created that actually implements all the drawing capabilities. This implementation is hidden from us by the Graphics class, which supplies the interface that enables us to write programs that use graphics in a platform-independent manner. Class Component is the superclass for many of the classes in the java.awt package (we discuss class Component in Chapter 12). Component method paint takes a Graphicsobject as an argument. This object is passed to the paintmethod by the system when a paint operation is required for a Component. The header for the paintmethod is public void paint( Graphics g ) The Graphicsobject greceives a reference to an object of the system s derived Graphics class. The preceding method header should look familiar to you it is the same one Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01