Monday, 28 May 2012

Combine Multiple Shapes in Java using 2DGraphics

Constructive area geometry is a fancy name for combining shapes. Two or more shapes can be combined using different rules or operations, much the way numbers can be combined in an equation. The 2D API supports four different operations for combining the areas of two shapes:
addition (union) - The addition of two shapes is the area covered by one or both of the shapes.
intersection - The intersection of two shapes is the area that is covered by both shapes simultaneously.
subtraction - The result of subtracting one shape from another is the area covered by one that is not covered by the other.
exclusive or - The exclusive or operator is the inverse of the intersection operator. In other words, the exclusive or of two shapes is the area that is covered by one or the other of the shapes. But it does not include the area covered by both.
Shapes looking After combining
 The following interactive example, CombiningShapes, demonstrates the four area operators. It displays two shapes and a combo box that allows you to choose which area operator will be used to combine the shapes. All of this is accomplished in two methods and one constructor. The main()
method sets up a frame window to contain the example. CombiningShapes's constructor creates the two shapes and a combo box that holds the area operator names. Finally, the paint()method combines the two shapes according to the selected operator and draws the result.Screenshot of the output of code written is shown below
Screenshots of output in different cases combined together

DOWNLOAD source Java file from Mediafire
DOWNLOAD ApplicationFrame source code from Mediafire as without this program won't compile.
You can also view code from below.

import java.awt.*;
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("CombiningShapes v1.0");
    f.add(new CombiningShapes());
    f.setSize(220, 220);
    f.center();
    f.setVisible(true);
}
private Shape mShapeOne, mShapeTwo;
private JComboBox mOptions; 


public CombiningShapes() {
   // Create the two shapes, a circle and a square.
   mShapeOne = new Ellipse2D.Double(40, 20, 80, 80);
   mShapeTwo = new Rectangle2D.Double(60, 40, 80, 80);
   setBackground(Color.white);
   setLayout(new BorderLayout());
  
   // Create a panel to hold the combo box.
   JPanel controls = new JPanel();

   // Create the combo box with the names of the area operators.
   mOptions = new JComboBox( new String[] { "outline", "add", "intersection",
        "subtract", "exclusive or" });

   // Repaint ourselves when the selection changes.
   mOptions.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent ie) {
       repaint();
      }
   });

   controls.add(mOptions);
   add(controls, BorderLayout.SOUTH);
}

public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
   
    // Retrieve the selection option from the combo box.
    String option = (String)mOptions.getSelectedItem();
    if (option.equals("outline")) {

       // Just draw the outlines and return.
       g2.draw(mShapeOne);
       g2.draw(mShapeTwo);
       return;
    }

    // Create Areas from the shapes.
    Area areaOne = new Area(mShapeOne);
    Area areaTwo = new Area(mShapeTwo);

    // Combine the Areas according to the selected option.
    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);

    // Fill the resulting Area.
    g2.setPaint(Color.orange);
    g2.fill(areaOne);
    // Draw the outline of the resulting Area.
    g2.setPaint(Color.black);
    g2.draw(areaOne);
  }

Hope this helps .

No comments:

Post a Comment