The setPaint method of the Graphics2D class lets you select a color that is used for all subsequent drawing operations on the graphics context. To draw in multiple colors, you select a color, draw, then select another color, and draw again. You define colors with the Color class. The java.awt.Color class offers predefined constants for the 13 standard colors listed in Table 7-1.
For example, g2.setPaint(Color.RED); g2.drawString("Warning!", 100, 100); NOTE
You can specify a custom color by creating a Color object by its red, green, and blue components. Using a scale of 0 255 (that is, one byte) for the redness, blueness, and greenness, call the Color constructor like this: Color(int redness, int greenness, int blueness) Here is an example of setting a custom color: g2.setPaint(new Color(0, 128, 128)); // a dull blue-green g2.drawString("Welcome!", 75, 125); NOTE
To set the background color, you use the setBackground method of the Component class, an ancestor of JPanel. MyPanel p = new MyPanel(); p.setBackground(Color.PINK); There is also a setForeground method. It specifies the default color that is used for drawing on the component. TIP
Java gives you predefined names for many more colors in its SystemColor class. The constants in this class encapsulate the colors used for various elements of the user's system. For example, p.setBackground(SystemColor.window) sets the background color of the panel to the default used by all windows on the user's desktop. (The background is filled in whenever the window is repainted.) Using the colors in the SystemColor class is particularly useful when you want to draw user interface elements so that the colors match those already found on the user's desktop. Table 7-2 lists the system color names and their meanings.
java.awt.Color 1.0
java.awt.Graphics 1.0
java.awt.Graphics2D 1.2
java.awt.Component 1.0
Filling ShapesYou can fill the interiors of closed shapes (such as rectangles or ellipses) with a color (or, more generally, the current paint setting). Simply call fill instead of draw: Rectangle2D rect = . . .; g2.setPaint(Color.RED); g2.fill(rect); // fills rect with red color The program in Example 7-5 fills a rectangle in red, then an ellipse with the same boundary in a dull green (see Figure 7-12). Example 7-5. FillTest.java1. import java.awt.*; 2. import java.awt.geom.*; 3. import javax.swing.*; 4. 5. public class FillTest 6. { 7. public static void main(String[] args) 8. { 9. FillFrame frame = new FillFrame(); 10. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 11. frame.setVisible(true); 12. } 13. } 14. 15. /** 16. A frame that contains a panel with drawings 17. */ 18. class FillFrame extends JFrame 19. { 20. public FillFrame() 21. { 22. setTitle("FillTest"); 23. setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 24. 25. // add panel to frame 26. 27. FillPanel panel = new FillPanel(); 28. add(panel); 29. } 30. 31. public static final int DEFAULT_WIDTH = 400; 32. public static final int DEFAULT_HEIGHT = 400; 33. } 34. 35. /** 36. A panel that displays filled rectangles and ellipses 37. */ 38. class FillPanel extends JPanel 39. { 40. public void paintComponent(Graphics g) 41. { 42. super.paintComponent(g); 43. Graphics2D g2 = (Graphics2D) g; 44. 45. // draw a rectangle 46. 47. double leftX = 100; 48. double topY = 100; 49. double width = 200; 50. double height = 150; 51. 52. Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height); 53. g2.setPaint(Color.RED); 54. g2.fill(rect); 55. 56. // draw the enclosed ellipse 57. 58. Ellipse2D ellipse = new Ellipse2D.Double(); 59. ellipse.setFrame(rect); 60. g2.setPaint(new Color(0, 128, 128)); // a dull blue-green 61. g2.fill(ellipse); 62. } 63. } Figure 7-12. Filled rectangles and ellipses |