序列化 java.awt.geom.Area

发布于 2024-08-29 03:57:47 字数 96 浏览 10 评论 0原文

我需要在套接字中序列化 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 技术交流群。

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

发布评论

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

评论(4

机场等船 2024-09-05 03:57:47

我找到了这个解决方法:

AffineTransform.getTranslateInstance(0,0).createTransformedShape(myArea)

这会产生可以序列化的形状。

I found this workaround:

AffineTransform.getTranslateInstance(0,0).createTransformedShape(myArea)

This results in a shape that can be serialized.

心欲静而疯不止 2024-09-05 03:57:47

使用 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.

段念尘 2024-09-05 03:57:47

从 kieste 的回答中,可以得出这个解决方法。

import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.io.IOException;
import java.io.Serializable;

public class SerialArea extends Area implements Serializable {
    private static final long serialVersionUID = -3627137348463415558L;

    /**
     * New Area
     */
    public SerialArea() {}

    /**
     * New Area From Shape
     */
    public SerialArea(Shape s) {
        super(s);
    }

    /**
     * Writes object out to out.
     * @param out Output
     * @throws IOException if I/O errors occur while writing to the
     *  underlying OutputStream
     */
    private void writeObject(java.io.ObjectOutputStream out)
            throws IOException {
        out.writeObject(AffineTransform.getTranslateInstance(0, 0).
            createTransformedShape(this));
    }
    /**
     * Reads object in from in.
     * @param in Input
     * @throws IOException if I/O errors occur while writing to the
     *  underlying OutputStream
     * @throws ClassNotFoundException if the class of a serialized object
     *  could not be found.
     */
    private void readObject(java.io.ObjectInputStream in)
            throws IOException, ClassNotFoundException {
        add(new Area((Shape) in.readObject()));
    }
}

From kieste's answer, this work-around can be derived.

import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.io.IOException;
import java.io.Serializable;

public class SerialArea extends Area implements Serializable {
    private static final long serialVersionUID = -3627137348463415558L;

    /**
     * New Area
     */
    public SerialArea() {}

    /**
     * New Area From Shape
     */
    public SerialArea(Shape s) {
        super(s);
    }

    /**
     * Writes object out to out.
     * @param out Output
     * @throws IOException if I/O errors occur while writing to the
     *  underlying OutputStream
     */
    private void writeObject(java.io.ObjectOutputStream out)
            throws IOException {
        out.writeObject(AffineTransform.getTranslateInstance(0, 0).
            createTransformedShape(this));
    }
    /**
     * Reads object in from in.
     * @param in Input
     * @throws IOException if I/O errors occur while writing to the
     *  underlying OutputStream
     * @throws ClassNotFoundException if the class of a serialized object
     *  could not be found.
     */
    private void readObject(java.io.ObjectInputStream in)
            throws IOException, ClassNotFoundException {
        add(new Area((Shape) in.readObject()));
    }
}
掐死时间 2024-09-05 03:57:47

从 Java 1.6 开始,似乎有一种更正式的方法可以做到这一点。

您所要做的就是将 Area 对象转换为 Path2D.Double (或 Path2D.Float)对象(这是 Serialized)及其相应的 append 方法,还考虑构造时的缠绕规则(甚至稍后,使用存在的相应设置器)。

要从 Path2D.Double 转换为 Area,只需使用接受 Shape 的 Area 构造函数即可。

import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Path2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Rectangle2D;

public class SerializeArea {

    public static Path2D.Double toPath2D(final Area a) {
        final PathIterator pi = a.getPathIterator(new AffineTransform());
        final Path2D.Double path = new Path2D.Double();
        switch (pi.getWindingRule()) {
            case PathIterator.WIND_EVEN_ODD: path.setWindingRule(Path2D.WIND_EVEN_ODD); break;
            case PathIterator.WIND_NON_ZERO: path.setWindingRule(Path2D.WIND_NON_ZERO); break;
            default: throw new UnsupportedOperationException("Unimplemented winding rule.");
        }
        path.append(pi, false);
        return path;
    }

    public static Area toArea(final Path2D path) {
        return new Area(path);
    }

    public static void main(final String[] args) {
        final Area area = new Area(new Ellipse2D.Double(0, 0, 100, 100));
        area.intersect(new Area(new Rectangle2D.Double(0, 25, 100, 50))); //Creating something like a capsule.

        System.out.println(toArea(toPath2D(area)).equals(area)); //Prints true.
    }
}

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 a Path2D.Double (or Path2D.Float) object (which is Serializable) with its corresponding append 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 to Area, just use the Area's constructor which accepts a Shape.

import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Path2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Rectangle2D;

public class SerializeArea {

    public static Path2D.Double toPath2D(final Area a) {
        final PathIterator pi = a.getPathIterator(new AffineTransform());
        final Path2D.Double path = new Path2D.Double();
        switch (pi.getWindingRule()) {
            case PathIterator.WIND_EVEN_ODD: path.setWindingRule(Path2D.WIND_EVEN_ODD); break;
            case PathIterator.WIND_NON_ZERO: path.setWindingRule(Path2D.WIND_NON_ZERO); break;
            default: throw new UnsupportedOperationException("Unimplemented winding rule.");
        }
        path.append(pi, false);
        return path;
    }

    public static Area toArea(final Path2D path) {
        return new Area(path);
    }

    public static void main(final String[] args) {
        final Area area = new Area(new Ellipse2D.Double(0, 0, 100, 100));
        area.intersect(new Area(new Rectangle2D.Double(0, 25, 100, 50))); //Creating something like a capsule.

        System.out.println(toArea(toPath2D(area)).equals(area)); //Prints true.
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文