Areas


In the preceding section, you saw how you can specify complex shapes by constructing general paths that are composed of lines and curves. By using a sufficient number of lines and curves, you can draw essentially any shape. For example, the shapes of characters in the fonts that you see on the screen and on your printouts are all made up of lines and cubic curves.

Occasionally, it is easier to describe a shape by composing it from areas, such as rectangles, polygons, or ellipses. The Java 2D API supports four constructive area geometry operations that combine two areas to a new area:

  • add The combined area contains all points that are in the first or the second area.

  • subtract The combined area contains all points that are in the first but not the second area.

  • intersect The combined area contains all points that are in the first and the second area.

  • exclusiveOr The combined area contains all points that are in either the first or the second area, but not in both.

Figure 7-10 shows these operations.

Figure 7-10. Constructive area geometry operations


To construct a complex area, you start out with a default area object.

 Area a = new Area(); 

Then, you combine the area with any shape:

 a.add(new Rectangle2D.Double(. . .)); a.subtract(path); . . . 

The Area class implements the Shape interface. You can stroke the boundary of the area with the draw method or paint the interior with the fill method of the Graphics2D class.

The program in Example 7-2 shows the constructive area geometry operations. Select one of the four operations, and see the result of combining an ellipse and a rectangle with the operation that you selected (see Figure 7-11).

Example 7-2. AreaTest.java
   1. import java.awt.*;   2. import java.awt.event.*;   3. import java.awt.geom.*;   4. import java.util.*;   5. import javax.swing.*;   6.   7. /**   8.    This program demonstrates constructive area geometry   9.    operations.  10. */  11. public class AreaTest  12. {  13.    public static void main(String[] args)  14.    {  15.       JFrame frame = new AreaTestFrame();  16.       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  17.       frame.setVisible(true);  18.    }  19. }  20.  21. /**  22.    This frame contains a set of radio buttons to define  23.    area operations and a panel to show their result.  24. */  25. class AreaTestFrame extends JFrame  26. {  27.    public AreaTestFrame()  28.    {  29.       setTitle("AreaTest");  30.       setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);  31.  32.       area1 = new Area(new Ellipse2D.Double(100, 100, 150, 100));  33.       area2 = new Area(new Rectangle2D.Double(150, 150, 150, 100));  34.  35.       panel = new  36.          JPanel()  37.          {  38.             public void paintComponent(Graphics g)  39.             {  40.                super.paintComponent(g);  41.                Graphics2D g2 = (Graphics2D)g;  42.                g2.draw(area1);  43.                g2.draw(area2);  44.                if (area != null) g2.fill(area);  45.             }  46.          };  47.  48.       add(panel, BorderLayout.CENTER);  49.  50.       JPanel buttonPanel = new JPanel();  51.       ButtonGroup group = new ButtonGroup();  52.  53.       JRadioButton addButton = new JRadioButton("Add", false);  54.       buttonPanel.add(addButton);  55.       group.add(addButton);  56.       addButton.addActionListener(new  57.          ActionListener()  58.          {  59.             public void actionPerformed(ActionEvent event)  60.             {  61.                area = new Area();  62.                area.add(area1);  63.                area.add(area2);  64.                panel.repaint();  65.             }  66.          });  67.  68.       JRadioButton subtractButton = new JRadioButton("Subtract", false);  69.       buttonPanel.add(subtractButton);  70.       group.add(subtractButton);  71.       subtractButton.addActionListener(new  72.          ActionListener()  73.          {  74.             public void actionPerformed(ActionEvent event)  75.             {  76.                area = new Area();  77.                area.add(area1);  78.                area.subtract(area2);  79.                panel.repaint();  80.             }  81.          });  82.  83.       JRadioButton intersectButton = new JRadioButton("Intersect", false);  84.       buttonPanel.add(intersectButton);  85.       group.add(intersectButton);  86.       intersectButton.addActionListener(new  87.          ActionListener()  88.          {  89.             public void actionPerformed(ActionEvent event)  90.             {  91.                area = new Area();  92.                area.add(area1);  93.                area.intersect(area2);  94.                panel.repaint();  95.             }  96.          });  97.  98.       JRadioButton exclusiveOrButton = new JRadioButton("Exclusive Or", false);  99.       buttonPanel.add(exclusiveOrButton); 100.       group.add(exclusiveOrButton); 101.       exclusiveOrButton.addActionListener(new 102.          ActionListener() 103.          { 104.             public void actionPerformed(ActionEvent event) 105.             { 106.                area = new Area(); 107.                area.add(area1); 108.                area.exclusiveOr(area2); 109.                panel.repaint(); 110.             } 111.          }); 112. 113.       add(buttonPanel, BorderLayout.NORTH); 114.    } 115. 116.    private JPanel panel; 117.    private Area area; 118.    private Area area1; 119.    private Area area2; 120. 121.    private static final int DEFAULT_WIDTH = 400; 122.    private static final int DEFAULT_HEIGHT = 400; 123. } 

Figure 7-11. The AreaTest program



 java.awt.geom.Area 

  • void add(Area other)

  • void subtract(Area other)

  • void intersect(Area other)

  • void exclusiveOr(Area other)

    carry out the constructive area geometry operation with this area and the other area and set this area to the result.



    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