java/swing:形状问题:序列化和组合
我有两个关于 java.lang. awt.Shape
。假设我有两个 Shape
,shape1
和 shape2
。
如何以某种方式序列化它们,以便将信息保存到文件中,然后在另一台计算机上重新创建它? (
Shape
不是Serializable
,但它确实有getPathIterator()
方法,看起来您可以获取信息,但它有点像拖动+我不确定如何重建Shape
对象。)如何将它们组合成一个新的 Shape,以便它们形成联合边界? (例如,如果 shape1 是一个大正方形,shape2 是正方形内的一个小圆圈,我希望组合后的形状是一个带有小圆孔的大正方形)
I have two questions about java.awt.Shape
. Suppose I have two Shape
s, shape1
and shape2
.
How can I serialize them in some way so I can save the information to a file and later recreate it on another computer? (
Shape
is notSerializable
but it does have thegetPathIterator()
method which seems like you could get the information out but it would be kind of a drag + I'm not sure how to reconstruct aShape
object afterwards.)How can I combine them into a new Shape so that they form a joint boundary? (e.g. if shape1 is a large square and shape2 is a small circle inside the square, I want the combined shape to be a large square with a small circular hole)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您可以使用
java.awt.geom.Path2D.Double
从路径信息重建Shape
。但是,它可能不如特定实现那么有效。要在没有对具有 Shape 作为字段的所有类进行特殊工作的情况下进行序列化,那么您需要确保所有构造的形状都是所提供的 Shape 的可序列化子类,即在
readObject
方法中初始化数据。如果在某些情况下您需要将数据发送到构造函数,那么您将需要“串行代理”(我认为在这种情况下这是不必要的)。序列化底层模型数据可能会更好。
Shape
通常是瞬时构造的。I believe you can reconstruct a
Shape
from path information withjava.awt.geom.Path2D.Double
. However, it may not be as efficient as specific implementations.To be serialisable without special work from all classes that have a
Shape
as a field, then you would need to ensure that all constructed shapes serialisable subclasses of the providedShape
s, that initialise data in areadObject
method. If there are cases where you need to send data to the constructor, then you will need "serial proxies" (I don't think this is necessary in this case).It might well be better to serialise the underlying model data.
Shape
s are generally constructed transiently.我想我已经找到了问题第二部分的答案:
I think I figured out an answer to the 2nd part of my question: