Search code examples
javagraphicsshapesoutline

Java: Making an outline of multiple shapes


Say I drew two circles 30 pixels radius and 20 pixels apart. You'd get a cross-over of lines. How can I prevent this crossover?

I've tried looking at various graphics filtering but I haven't found anything suitable.

(This question is not limited to 2 circles)


Solution

  • You can use java.awt.geom.Area class to do the operations. It has add(), intersect(), subtract() methods.

    Create one Area (sum of both ovals) and subtract another Area (intersection of both ovals).


    Working code:

    int x = 200; int y = 200;
    Ellipse2D.Double first = new Ellipse2D.Double(x,y,75,75);
    Ellipse2D.Double second = new Ellipse2D.Double(x+25,y,75,75);
    Area circles = new Area(first);
    circles.add(new Area(second));
    
    graphics2D.draw(circles);
    

    Two Circles