Graphics Contexts and Graphics Objects
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
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()