|
When you fill a shape, its inside is covered with paint. You use the setPaint method to set the paint style to an object with a class that implements the Paint interface. The Java 2D API provides three such classes:
You construct a GradientPaint object by specifying two points and the colors that you want at these two points. g2.setPaint(new GradientPaint(p1, Color.red, p2, Color.blue)); Colors are interpolated along the line joining the two points. Colors are constant along lines that are perpendicular to that joining line. Points beyond an end point of the line are given the color at the end point. Alternatively, if you call the GradientPaint constructor with true for the cyclic parameter, g2.setPaint(new GradientPaint(p1, Color.red, p2, Color.blue, true)); then the color variation cycles and keeps varying beyond the end points. To construct a TexturePaint object, you specify a BufferedImage and an anchor rectangle. The anchor rectangle is extended indefinitely in x- and y-directions to tile the entire coordinate plane. The image is scaled to fit into the anchor and then replicated into each tile. We introduce the BufferedImage class later in this chapter when we discuss images in detail. You create a BufferedImage object by giving the image size and the image type. The most common image type is TYPE_INT_ARGB, in which each pixel is specified by an integer that describes the alpha, or transparency, red, green, and blue values. For example, BufferedImage bufferedImage = new BufferedImage(width,height, BufferedImage.TYPE_INT_ARGB); You then obtain a graphics context to draw into the buffered image. Graphics2D g2 = bufferedImage.createGraphics(); Any drawing operations on g2 now fill the buffered image with pixels. When you are done, you can create your TexturePaint object: g2.setPaint(new TexturePaint(bufferedImage, anchorRectangle)); The program in Example 7-4 lets the user choose between a solid color paint, a gradient paint, and a texture paint. Then, an ellipse is filled with the specified paint. The texture paint uses an image that is read from a GIF file. As you will see later in this chapter, the ImageIO class makes it simple to read a graphics file into a buffered image by calling bufferedImage = ImageIO.read(new File("blue-ball.gif")); NOTE
To show the significance of the anchor rectangle, we specify the anchor to have twice the size of the image: Rectangle2D anchor = new Rectangle2D.Double(0, 0, 2 * bufferedImage.getWidth(), 2 * bufferedImage.getHeight()); paint = new TexturePaint(bufferedImage, anchor); As you can see when you select Texture Paint, the image is scaled to fit the anchor, and it is then replicated to fill the shape. Tiles that meet the boundary of the shape are clipped. Example 7-4. PaintTest.java1. import java.awt.*; 2. import java.awt.event.*; 3. import java.awt.geom.*; 4. import java.awt.image.*; 5. import java.io.*; 6. import java.util.*; 7. import javax.imageio.*; 8. import javax.swing.*; 9. 10. /** 11. This program demonstrates the various paint modes. 12. */ 13. public class PaintTest 14. { 15. public static void main(String[] args) 16. { 17. JFrame frame = new PaintTestFrame(); 18. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 19. frame.setVisible(true); 20. } 21. } 22. 23. /** 24. This frame contains radio buttons to choose the paint mode 25. and a panel that draws a circle in the selected paint mode. 26. */ 27. class PaintTestFrame extends JFrame 28. { 29. public PaintTestFrame() 30. { 31. setTitle("PaintTest"); 32. setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 33. 34. canvas = new PaintPanel(); 35. add(canvas, BorderLayout.CENTER); 36. 37. JPanel buttonPanel = new JPanel(); 38. ButtonGroup group = new ButtonGroup(); 39. 40. JRadioButton colorButton = new JRadioButton("Color", true); 41. buttonPanel.add(colorButton); 42. group.add(colorButton); 43. colorButton.addActionListener(new 44. ActionListener() 45. { 46. public void actionPerformed(ActionEvent event) 47. { 48. canvas.setColor(); 49. } 50. }); 51. 52. JRadioButton gradientPaintButton = new JRadioButton("Gradient Paint", false); 53. buttonPanel.add(gradientPaintButton); 54. group.add(gradientPaintButton); 55. gradientPaintButton.addActionListener(new 56. ActionListener() 57. { 58. public void actionPerformed(ActionEvent event) 59. { 60. canvas.setGradientPaint(); 61. } 62. }); 63. 64. JRadioButton texturePaintButton = new JRadioButton("Texture Paint", false); 65. buttonPanel.add(texturePaintButton); 66. group.add(texturePaintButton); 67. texturePaintButton.addActionListener(new 68. ActionListener() 69. { 70. public void actionPerformed(ActionEvent event) 71. { 72. canvas.setTexturePaint(); 73. } 74. }); 75. 76. add(buttonPanel, BorderLayout.NORTH); 77. } 78. 79. private PaintPanel canvas; 80. private static final int DEFAULT_WIDTH = 400; 81. private static final int DEFAULT_HEIGHT = 400; 82. } 83. 84. /** 85. This panel paints a circle in various paint modes. 86. */ 87. class PaintPanel extends JPanel 88. { 89. public PaintPanel() 90. { 91. try 92. { 93. bufferedImage = ImageIO.read(new File("blue-ball.gif")); 94. } 95. catch (IOException e) 96. { 97. e.printStackTrace(); 98. } 99. setColor(); 100. } 101. 102. public void paintComponent(Graphics g) 103. { 104. super.paintComponent(g); 105. Graphics2D g2 = (Graphics2D) g; 106. g2.setPaint(paint); 107. Ellipse2D circle = new Ellipse2D.Double(0, 0, getWidth(), getHeight()); 108. g2.fill(circle); 109. } 110. 111. /** 112. Paints in a plain color. 113. */ 114. public void setColor() 115. { 116. paint = Color.red; // Color implements Paint 117. repaint(); 118. } 119. 120. /** 121. Sets the paint mode to gradient paint. 122. */ 123. public void setGradientPaint() 124. { 125. paint = new GradientPaint(0, 0, Color.red, 126. (float) getWidth(), (float) getHeight(), Color.blue); 127. repaint(); 128. } 129. 130. /** 131. Sets the paint mode to texture paint. 132. */ 133. public void setTexturePaint() 134. { 135. Rectangle2D anchor = new Rectangle2D.Double(0, 0, 136. 2 * bufferedImage.getWidth(), 137. 2 * bufferedImage.getHeight()); 138. paint = new TexturePaint(bufferedImage, anchor); 139. repaint(); 140. } 141. 142. private Paint paint; 143. private BufferedImage bufferedImage; 144. } java.awt.Graphics2D 1.2
java.awt.GradientPaint 1.2
java.awt.TexturePaint 1.2
|
|