序列化 java.awt.geom.Area
我需要在套接字中序列化 Area 对象(java.awt.geom.Area)。但它似乎不可序列化。有办法做这样的事情吗?也许通过将其转换为不同的对象?
提前致谢
I have the need to serialize an Area object (java.awt.geom.Area) in a socket. However it doesn't seem to be serializable. Is there a way to do such thing? Maybe by converting it into a different object?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我找到了这个解决方法:
这会产生可以序列化的形状。
I found this workaround:
This results in a shape that can be serialized.
使用 XStream 轻松将其与 XML 相互转换。您不需要对象来实现特定的接口,并且序列化是可定制的。
Use XStream to trivially convert it to/from XML. You don't need your object to implement particular interfaces, and the serialisation is customisable.
从 kieste 的回答中,可以得出这个解决方法。
From kieste's answer, this work-around can be derived.
从 Java 1.6 开始,似乎有一种更正式的方法可以做到这一点。
您所要做的就是将
Area
对象转换为Path2D.Double
(或Path2D.Float
)对象(这是Serialized
)及其相应的append
方法,还考虑构造时的缠绕规则(甚至稍后,使用存在的相应设置器)。要从 Path2D.Double 转换为 Area,只需使用接受 Shape 的 Area 构造函数即可。
Since Java 1.6 seems like there is a more formal way of doing this.
All you have to do is convert the
Area
object to aPath2D.Double
(orPath2D.Float
) object (which isSerializable
) with its correspondingappend
method, taking also into account the winding rule at construction time (or even later, with a corresponding setter that exists).To convert from
Path2D.Double
toArea
, just use theArea
's constructor which accepts aShape
.