file io - javafx and serializability -
in old awt libraries, point
class , color
class serializable. neither in javafx. save array list of drawable
s file; here interface
import javafx.scene.canvas.graphicscontext; public interface drawable { public void draw(graphicscontext g); }
when attempt to this, bombarded notserializableexcepton
s. best alternate course of action? of drawables know color , size.
use custom serializable form , serialize data need. e.g.
import javafx.scene.canvas.graphicscontext ; import javafx.scene.paint.color ; import javafx.geometry.rectangle2d; import java.io.serializable ; import java.io.objectinputstream ; import java.io.objectoutputstream ; import java.io.ioexception ; public class drawablerect implements drawable, serializable { private transient color color ; private transient rectangle2d bounds ; public drawablerect(color color, rectangle2d bounds) { this.color = color ; this.bounds = bounds ; } @override public void draw(graphicscontext g) { g.setfill(color); g.fillrect(bounds.getminx(), bounds.getminy(), bounds.getwidth(), bounds.getheight()); } private void writeobject(objectoutputstream s) throws ioexception { s.defaultwriteobject(); // write color: s.writedouble(color.getred()); s.writedouble(color.getgreen()); s.writedouble(color.getblue()); s.writedouble(color.getopacity()); // write bounds: s.writedouble(bounds.getminx()); s.writedouble(bounds.getminy()); s.writedouble(bounds.getwidth()); s.writedouble(bounds.getheight()); } private void readobject(objectinputstream s) throws ioexception, classnotfoundexception { s.defaultreadobject(); double r = s.readdouble(); double g = s.readdouble(); double b = s.readdouble(); double opacity = s.readdouble(); color = new color(r,g,b,opacity); double x = s.readdouble(); double y = s.readdouble(); double w = s.readdouble(); double h = s.readdouble(); bounds = new rectangle2d(x,y,w,h); } }
if have fields serializable (or primitive types), don't mark them transient
, , defaultreadobject
, defaultwriteobject
handle them. if have fields not serializable, mark them transient
, serialize data in form can serialized in example.
obviously, since have multiple implementations of interface may need functionality, might benefit create helper class static methods:
public class drawableio { public static void writecolor(color color, objectoutputstream s) throws ioexception { s.writedouble(color.getred()); s.writedouble(color.getgreen()); s.writedouble(color.getblue()); s.writedouble(color.getopacity()); } public static color readcolor(obectinputstream s) throws ioexception { double r = s.readdouble(); double g = s.readdouble(); double b = s.readdouble(); double opacity = s.readdouble(); return new color(r,g,b,opacity); } public static void writebounds(rectangle2d bounds, objectoutputstream s) throws ioexception { s.writedouble(bounds.getminx()); s.writedouble(bounds.getminy()); s.writedouble(bounds.getwidth()); s.writedouble(bounds.getheight()); } public static rectangle2d readbounds(objectinputstream s) throws ioexception { double x = s.readdouble(); double y = s.readdouble(); double w = s.readdouble(); double h = s.readdouble(); return new rectangle2d(x,y,w,h); } }
and of course methods in drawable
implementations reduce like
private void writeobject(objectoutputstream s) throws ioexception { s.defaultwriteobject(); drawableio.writecolor(color, s); drawableio.writebounds(bounds, s); } private void readobject(objectinputstream s) throws ioexception, classnotfoundexception { s.defaultreadobject(); color = drawableio.readcolor(s); bounds = drawableio.readbounds(s); }
Comments
Post a Comment