2
\$\begingroup\$

I am making a java game using path geometry. Is there any way to save the data stored in a Path2D to a file?

\$\endgroup\$
3
  • \$\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\$
    – elaid
    Dec 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. Kiley
    Dec 10, 2014 at 12:26

1 Answer 1

0
\$\begingroup\$

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.

Java serialization

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.

PathIterator

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .