Home Segments Top Top Previous Next

736: Mainline

You can both get state information from the graphics context and set state information in the graphics context. For example, if you want to draw a blue line, rather than a black one, you easily can modify the example of Segment 734 by adding a statement that provides the graphics context with an instance of the Color class.

The definition of the Color class includes class variables with values that are instances of the Color class itself. One such class variable is the blue variable; the value of the blue variable is a Color instance associated with the color blue:

You use the setColor method inside the definition of paint to provide the graphics context with a color:

public void paint(Graphics g) {
 // Set color:
 g.setColor(Color.blue);                                
 // Rest of paint definition 
}