Paint


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:

  • The Color class implements the Paint interface. To fill shapes with a solid color, simply call setPaint with a Color object, such as

     g2.setPaint(Color.red); 

  • The GradientPaint class varies colors by interpolating between two given color values (see Figure 7-16).

    Figure 7-16. Gradient paint


  • The TexturePaint class fills an area with repetitions of an image (see Figure 7-17).

    Figure 7-17. Texture paint


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

The ImageIO class was added in JDK version 1.4. If you have an older version of the JDK, use the following code instead:

 Image image = Toolkit.getDefaultToolkit().getImage("blue-ball.gif"); MediaTracker tracker = new MediaTracker(this); tracker.addImage(image, 0); try { tracker.waitForID(0); } catch (InterruptedException e) {} bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null),    BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = bufferedImage.createGraphics(); g2.drawImage(image, 0, 0, null); 


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.java
   1. 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 

  • void setPaint(Paint s)

    sets the paint of this graphics context to the given object that implements the Paint interface.


 java.awt.GradientPaint 1.2 

  • GradientPaint(float x1, float y1, Color color1, float x2, float y2, Color color2)

  • GradientPaint(float x1, float y1, Color color1, float x2, float y2, Color color2, boolean cyclic)

  • GradientPaint(Point2D p1, Color color1, Point2D p2, Color color2)

  • GradientPaint(Point2D p1, Color color1, Point2D p2, Color color2, boolean cyclic)

    construct a gradient paint object that fills shapes with color such that the start point is colored with color1, the end point is colored with color2, and the colors in between are linearly interpolated. Colors are constant along lines that are perpendicular to the line joining the start and the end point. By default, the gradient paint is not cyclic; that is, points beyond the start and end points are colored with the same color as the start and end point. If the gradient paint is cyclic, then colors continue to be interpolated, first returning to the starting point color and then repeating indefinitely in both directions.

    Parameters:

    x1, y1, or p1

    The start point

     

    color1

    The color to use for the start point

     

    x2, y2, or p2

    The end point

     

    color2

    The color to use for the end point

     

    cyclic

    true if the color change pattern repeats, false if the colors beyond the start and end point are constant



 java.awt.TexturePaint 1.2 

  • TexturePaint(BufferedImage texture, Rectangle2D anchor)

    creates a texture paint object.

    Parameters:

    texture

    The texture to use for filling shapes

     

    anchor

    The anchor rectangle that defines the tiling of the space to be painted; it is repeated indefinitely in x- and y-directions, and the texture image is scaled to fill each tile




    Core JavaT 2 Volume II - Advanced Features
    Building an On Demand Computing Environment with IBM: How to Optimize Your Current Infrastructure for Today and Tomorrow (MaxFacts Guidebook series)
    ISBN: 193164411X
    EAN: 2147483647
    Year: 2003
    Pages: 156
    Authors: Jim Hoskins

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net