In the old AWT libraries, the Point
class and the Color
class were serializable. Neither is in JavaFX. I would like to save an array list of Drawable
s to a file; here is the interface
import javafx.scene.canvas.GraphicsContext;
public interface Drawable
{
public void draw(GraphicsContext g);
}
When I attempt to to this, I get bombarded by NotSerializableExcepton
s.
What is the best alternate course of action? All of my drawables know their color and size.
Use a custom serializable form and serialize the data you need. E.g.
If you have fields that are serializable (or primitive types), you don't mark them
transient
, and thedefaultReadObject
anddefaultWriteObject
will handle them. If you have fields that are not serializable, mark themtransient
and serialize the data in a form that can be serialized as in the example.Obviously, since you have multiple implementations of this interface which may all need this functionality, it might benefit you to create a helper class with some static methods:
and then of course the methods in your
Drawable
implementations reduce to something like