I have two questions about java.awt.Shape. Suppose I have two Shapes, shape1 and shape2.
-
How can I serialize them in some way so I can save the information to a file and later recreate it on another computer? (
Shapeis notSerializablebut it does have thegetPathIterator()method which seems like you could get the information out but it would be kind of a drag + I’m not sure how to reconstruct aShapeobject afterwards.) -
How can I combine them into a new Shape so that they form a joint boundary? (e.g. if shape1 is a large square and shape2 is a small circle inside the square, I want the combined shape to be a large square with a small circular hole)
I believe you can reconstruct a
Shapefrom path information withjava.awt.geom.Path2D.Double. However, it may not be as efficient as specific implementations.To be serialisable without special work from all classes that have a
Shapeas a field, then you would need to ensure that all constructed shapes serialisable subclasses of the providedShapes, that initialise data in areadObjectmethod. If there are cases where you need to send data to the constructor, then you will need “serial proxies” (I don’t think this is necessary in this case).It might well be better to serialise the underlying model data.
Shapes are generally constructed transiently.