Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>java.awt.geom.Area can combine multiple shapes with methods add, subtract, exclusiveOr, and intersect. It's a ready-made class for CompositeShape.</p> <p>It seems extremely weird that you've been asked to recreate it as "CompositeShape", because Area already does what you want.</p> <p>The solution could be as simple as </p> <p>class CompositeShape extends java.awt.geom.Area {}</p> <p>and you're done.</p> <p><strong>Or</strong>, the fact that you've been given a hint about PathIterator, it might be that you're being encouraged to manage the added shapes in a list manually, then implement all the methods of the Shape interface in terms of iterating over the other shapes.</p> <p>E.g., getBounds() needs to return the rectangular bounds of the shape, so get the rectangular bounds of the first, then use Rectangle.union to join it with the bounds of the others.</p> <p>And for getPathIterator(), return a new inner class implementing PathIterator that will iterate over all the shapes in your collection, and iterate the path segments of each of their getPathIterator methods, returning each path segment.</p> <p>It all sounds unnecessary in practice, since the needed class already exists. I think you should get clarification on what is wanted. Good luck.</p> <hr> <p>To clarify what I said about the implementation of getPathIterator, return something like this. I didn't test this. This assumes your list is called <code>shapes</code>.</p> <pre><code>public PathIterator getPathIterator(final AffineTransform at, final double flatness) { return new PathIterator() { private PathIterator currentPathIterator; private Iterator&lt;Shape&gt; shapeIterator = shapes.iterator(); { nextShape(); } private void nextShape() { if (shapeIterator.hasNext()) { currentPathIterator = shapeIterator.next().getPathIterator(at, flatness); } else { currentPathIterator = null; } } public int getWindingRule() { return WIND_NON_ZERO; } public boolean isDone() { for (;;) { if (currentPathIterator == null) return true; if (!currentPathIterator.isDone()) return false; nextShape(); } } public void next() { currentPathIterator.next(); } public int currentSegment(float[] coords) { return currentPathIterator.currentSegment(coords); } public int currentSegment(double[] coords) { return currentPathIterator.currentSegment(coords); } }; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload