1  import java.awt.*;
  2  import java.awt.geom.*;
  3  import javax.swing.*;
  4  
  5  public class AreaDemo extends JApplet {
  6    public AreaDemo() {
  7      add(new ShapePanel());
  8    }
  9  
 10    class ShapePanel extends JPanel {
 11      @Override
 12      protected void paintComponent(Graphics g) {
 13        super.paintComponent(g);
 14    
 15        Graphics2D g2d = (Graphics2D)g; // Get Graphics2D
 16    
 17        // Create two shapes
 18        Shape shape1 = new Ellipse2D.Double(0, 0, 50, 50);
 19        Shape shape2 = new Ellipse2D.Double(25, 0, 50, 50);
 20        g2d.translate(10, 10); // Translate to a new origin     
 21        g2d.draw(shape1); // Draw the shape
 22        g2d.draw(shape2); // Draw the shape
 23        
 24        Area area1 = new Area(shape1); // Create an area
 25        Area area2 = new Area(shape2);
 26        area1.add(area2); // Add area2 to area1
 27        g2d.translate(100, 0); // Translate to a new origin     
 28        g2d.draw(area1); // Draw the outline of the shape in the area
 29  
 30        g2d.translate(100, 0); // Translate to a new origin     
 31        g2d.fill(area1); // Fill the shape in the area
 32  
 33        area1 = new Area(shape1);
 34        area1.subtract(area2); // Subtract area2 from area1
 35        g2d.translate(100, 0); // Translate to a new origin     
 36        g2d.fill(area1); // Fill the shape in the area
 37  
 38        area1 = new Area(shape1);
 39        area1.intersect(area2); // Intersection of area2 with area1
 40        g2d.translate(100, 0); // Translate to a new origin     
 41        g2d.fill(area1); // Fill the shape in the area
 42  
 43        area1 = new Area(shape1);
 44        area1.exclusiveOr(area2); // Exclusive or of area2 with area1
 45        g2d.translate(100, 0); // Translate to a new origin     
 46        g2d.fill(area1); // Fill the shape in the area
 47      }
 48    }
 49  
 50    /** Main method */
 51    public static void main(String[] args) {
 52      AreaDemo applet = new AreaDemo();
 53      applet.init();
 54      applet.start();
 55      JFrame frame = new JFrame();
 56      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 57      frame.setTitle("Test");
 58      frame.getContentPane().add(applet, BorderLayout.CENTER);
 59      frame.setSize(450, 160);
 60      frame.setVisible(true);
 61    }
 62  }