java/swing:形状问题:序列化和组合

发布于 2024-08-25 10:36:38 字数 541 浏览 3 评论 0原文

我有两个关于 java.lang. awt.Shape。假设我有两个 Shapeshape1shape2

  1. 如何以某种方式序列化它们,以便将信息保存到文件中,然后在另一台计算机上重新创建它? (Shape 不是 Serializable,但它确实有 getPathIterator() 方法,看起来您可以获取信息,但它有点像拖动+我不确定如何重建 Shape 对象。)

  2. 如何将它们组合成一个新的 Shape,以便它们形成联合边界? (例如,如果 shape1 是一个大正方形,shape2 是正方形内的一个小圆圈,我希望组合后的形状是一个带有小圆孔的大正方形)

I have two questions about java.awt.Shape. Suppose I have two Shapes, shape1 and shape2.

  1. 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 not Serializable but it does have the getPathIterator() 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 a Shape object afterwards.)

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过潦 2024-09-01 10:36:38

我相信您可以使用java.awt.geom.Path2D.Double从路径信息重建Shape。但是,它可能不如特定实现那么有效。

要在没有对具有 Shape 作为字段的所有类进行特殊工作的情况下进行序列化,那么您需要确保所有构造的形状都是所提供的 Shape 的可序列化子类,即在 readObject 方法中初始化数据。如果在某些情况下您需要将数据发送到构造函数,那么您将需要“串行代理”(我认为在这种情况下这是不必要的)。

序列化底层模型数据可能会更好。 Shape通常是瞬时构造的。

I believe you can reconstruct a Shape from path information with java.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 provided Shapes, that initialise data in a readObject 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. Shapes are generally constructed transiently.

香橙ぽ 2024-09-01 10:36:38

我想我已经找到了问题第二部分的答案:

Shape shape1, shape2;
shape1 = ...;
shape2 = ...;
Area area = new Area(shape1);
area.subtract(new Area(shape2));
// "area" is now a Shape that is the difference between the two shapes.

I think I figured out an answer to the 2nd part of my question:

Shape shape1, shape2;
shape1 = ...;
shape2 = ...;
Area area = new Area(shape1);
area.subtract(new Area(shape2));
// "area" is now a Shape that is the difference between the two shapes.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文