I am making a java game using path geometry. Is there any way to save the data stored in a Path2D to a file?
\$\begingroup\$ How are you creating the path data to start with? \$\endgroup\$– House ♦Dec 9, 2014 at 22:40
\$\begingroup\$ I'm using the Path2D's moveTo, lineTo, etc. methods to create a shape. @Byte56 \$\endgroup\$– elaidDec 9, 2014 at 22:50
\$\begingroup\$ I don't know Path2D, but the methods you are describing sound the same as the SVG methods, so prehaps you could use the SVG format? (bonus: you can open the file up in image editors!) \$\endgroup\$– T. KileyDec 10, 2014 at 12:26
I have not tried any of these, but the following three solutions should work.
Save the input data
Instead of saving the Path2D object, save the values you have used to create the objects. As you said in the comments that you are creating the Path2D objects yourself using the API, you have the source data and you can control what to do with that.
If the first method sounds too complicated to fit to your application or you just prefer alternative solutions, Java's own serialization mechanism should do the trick. Path2D.Float and Path2D.Double implementations implement the Serializable interface, which means that they can be saved and loaded easily using ObjectOutputStream and ObjectInputStream. There are plenty of tutorials online if needed.
You can also use Shape.getPathIterator() to return an instance of PathIterator. This interface gives you the individual line and curve segments of the Path2D object, which you can save to a file. PathIterator and Path2D use the same types of segments, so this method should allow reconstructing the paths without losing any information. Note that while the getPathIterator() doesn't guarantee to return the exact same shape, checking the source code of Path2D seemed to return the internal model in their exact form. In contrast e.g. Ellipse2D can't be exactly represented with a PathIterator.