import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;

import javax.swing.*;

public class CombiningShapes extends JComponent {
  public static void main(String[] args) {
    ApplicationFrame f = new ApplicationFrame("Combinaisons de formes");
    f.add(new CombiningShapes());
    f.setSize(400, 400);
		f.setBackground(Color.green);
		f.center();
    f.setVisible(true);
  }
  
  private Shape ellipse, rectangle;
  private JComboBox options;
  
  public CombiningShapes() {
    ellipse = new Ellipse2D.Double(40, 20, 200, 200);
    rectangle = new Rectangle2D.Double(160, 140, 200, 160);
    setLayout(new BorderLayout());
    JPanel controls = new JPanel();
    options = new JComboBox(
      new String[] { "contour", "add", "intersection",
          "subtract", "exclusive or" }
    );
    options.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent ie) {
        repaint();
      }
    });
    controls.add(options);
controls.setBackground(Color.black);
    add(controls, BorderLayout.SOUTH);
  }
  
  public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setStroke(new BasicStroke(2));
    String option = (String)options.getSelectedItem();
    if (option.equals("contour")) {
      g2.draw(ellipse);
      g2.draw(rectangle);
      return;
    }
    Area areaOne = new Area(ellipse);
    Area areaTwo = new Area(rectangle);
    if (option.equals("add")) areaOne.add(areaTwo);
    else if (option.equals("intersection")) areaOne.intersect(areaTwo);
    else if (option.equals("subtract")) areaOne.subtract(areaTwo);
    else if (option.equals("exclusive or")) areaOne.exclusiveOr(areaTwo);   
    g2.setPaint(Color.orange);
    g2.fill(areaOne);
    g2.setPaint(Color.black);
    g2.draw(areaOne);
  }
}
