Constructive Area Geometry Operation in Java

By using a sufficient number of lines and curves we can draw essentially any shape. It is easier to describe a shape(rectangles, polygons, or ellipses) by composing it from areas. The Java2D supports four constructive area geometry operations that combine two areas to a new area:1)add 2)subtract   3)intersect   4)exclusiveOr.

 

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

import java.awt.geom.*;

import java.awt.geom.Area;

public class AreaTest

{

public static void main(String aa[])

{

JFrame frame=new AreaTestFramee();

frame.show();

}

}

class AreaTestFramee extends JFrame implements ActionListener

{

public AreaTestFramee()

{

setTitle("AreaTest");

setSize(400,400);

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent wee)

{

System.exit(0);

}

});

Container contentPane=getContentPane();

canvas=new AreaPanel();

contentPane.add(canvas,"Center");

JPanel buttonPanel=new JPanel();

ButtonGroup group=new ButtonGroup();

addButton=new JRadioButton("Addd",true);

buttonPanel.add(addButton);

group.add(addButton);

addButton.addActionListener(this);

subtractButton=new JRadioButton("Subtractt",false);

buttonPanel.add(subtractButton);

group.add(subtractButton);

subtractButton.addActionListener(this);

intersectButton=new JRadioButton("Intersectt",false);

buttonPanel.add(intersectButton);

group.add(intersectButton);

intersectButton.addActionListener(this);

exclusiveOrButton=new JRadioButton("ExclusiveOr",false);

buttonPanel.add(exclusiveOrButton);

group.add(exclusiveOrButton);

exclusiveOrButton.addActionListener(this);

contentPane.add(buttonPanel,"North");

}

public void actionPerformed(ActionEvent aeventt)

{

Object source=aeventt.getSource();

if(source==addButton)

canvas.addAreas();

else if(source==subtractButton)

canvas.subtractAreas();

else if(source==intersectButton)

canvas.intersectAreas();

else if(source==exclusiveOrButton)

canvas.exclusiveOrAreas();

}

private AreaPanel canvas;

private JRadioButton addButton;

private JRadioButton subtractButton;

private JRadioButton intersectButton;

private JRadioButton exclusiveOrButton;

}

class AreaPanel extends JPanel

{

public AreaPanel()

{

area1=new Area(new Ellipse2D.Double(100,100,150,100));

area2=new Area(new Rectangle2D.Double(150,150,150,100));

addAreas();

}

public void paintComponent(Graphics gg)

{

super.paintComponent(gg);

Graphics2D g2=(Graphics2D)gg;

gg.setColor(Color.red);

g2.draw(area1);

gg.setColor(Color.black);

g2.draw(area2);

g2.fill(area);

}

public void addAreas()

{

area=new Area();

area.add(area1);

area.add(area2);

repaint();

}

public void subtractAreas()

{

area=new Area();

area.add(area1);

area.subtract(area2);

repaint();

}

public void intersectAreas()

{

area=new Area();

area.add(area1);

area.intersect(area2);

repaint();

}

public void exclusiveOrAreas()

{

area=new Area();

area.add(area1);

area.exclusiveOr(area2);

repaint();

}

private Area area;

private Area area1;

private Area area2;

}

Constructive Area Geometry operation

Explanation…

The Area class implements the Shape interface(Area a=new Area). You can stroke(stroke is a solid line that is one pixel wide) the boundary of the area with the draw method or paint the interior with the fill method of the Graphics2D class.

In above example it shows the constructive area geometry operations (add, subtract, intersect, exclusiveOr). Select one (addButton, subtractButton, intersectButton, exclusiveOrButton)of the four operations, and see the result of combining an ellipse and a rectangle.

Four constructive area geometry operations..

  1. Add—The combined area(ellipse & rectangle) contains all points that are in the first or the second area.
  2. Subtract—The combined area (ellipse & rectangle)contains all points that are in the first but not the second area.
  3. Intersect—The combined area (ellipse & rectangle)contains all points that are in the first and the second area.
  4. exclusiveOr—The combined area(ellipse & rectangle) contains all points that are in either the first or the second area, but not in both.