Java:ObjectStream - 忽略1种类型的对象

时间:2017-05-14 11:14:00

标签: java serialization graphics2d

我试图保存到文件并再次加载它 ArrayList <Shape> figures = new ArrayList<>(); 这包含3种类型的对象 - 椭圆,矩形和多边形 - 它们都实现了Shape 我做了这个方法:

public void save()
    {
        try
        {
            FileOutputStream f = new FileOutputStream(new File("E:\\Dropbox\\KursProgramowaniaLaboratoria\\list05\\MyPaint\\out\\production\\MyPaint\\save.txt"));
            ObjectOutputStream o = new ObjectOutputStream(f);

            for(Shape s: figures)
            {
                o.writeObject(s);
            }

            o.writeObject(null);

            o.close();
            f.close();
        }
        catch (FileNotFoundException ex)
        {
            System.out.println("FileNotFound");
        }
        catch (IOException ex)
        {
            System.out.println("IOException");
        }
    }

    public void load()
    {
        try
        {
            FileInputStream f = new FileInputStream(new File("E:\\Dropbox\\KursProgramowaniaLaboratoria\\list05\\MyPaint\\out\\production\\MyPaint\\save.txt"));
            ObjectInputStream o = new ObjectInputStream(f);

            figures.clear();
            Shape s;
            while ((s = (Shape)o.readObject()) != null)
            {
                figures.add(s);
            }

            o.close();
            f.close();
        }
        catch (ClassNotFoundException ex)
        {
            System.out.println("ClassNotFound");
        }
        catch (IOException ex)
        {
            System.out.println("IOException");
        }
        repaint();
    }

但它仅适用于Ellipse和Rectangle - 它似乎忽略了所有Polygon对象:/

import java.awt.*;
import java.awt.geom.Point2D;
import java.util.ArrayList;

public class MyPolygon2D extends Polygon implements Shape
{
    private ArrayList<MyVector> vectors = new ArrayList<>();
    private Point2D.Double center;
    private int size;
    public Color color;

    public MyPolygon2D(ArrayList<Point2D.Double> points, Color color)
    {
        super();

        this.color = color;

        size = points.size();

        for(int i=0; i<size;++i)
        {
            addPoint((int)points.get(i).getX(),(int)points.get(i).getY());
        }

        center();
        setVectors();
    }

    public void scale(double amount, double scale)
    {
        double change = -1*amount*scale;

        for (int i=0;i<size;++i)
        {
            vectors.get(i).x *= (100.0+change)/100.0;
            vectors.get(i).y *= (100.0+change)/100.0;

            Point2D.Double curr = new Point2D.Double(center.getX()+vectors.get(i).x,center.getY()+vectors.get(i).y);

            xpoints[i] = (int)curr.getX();
            ypoints[i] = (int)curr.getY();
        }
        invalidate();
    }

    public void move (Point2D.Double p)
    {
        MyVector change = new MyVector(center,p);
        center = p;

        for(int i=0;i<size;++i)
        {
            xpoints[i] += (int)change.x;
            ypoints[i] += (int)change.y;
        }
        invalidate();
    }

    private void setVectors()
    {
        for(int i=0; i<size;++i)
        {
            vectors.add(new MyVector(center,new Point2D.Double(xpoints[i],ypoints[i])));
        }
    }

    private void center()
    {
        center = new Point2D.Double(getBounds2D().getX()+getBounds2D().getWidth()/2,getBounds2D().getY()+getBounds2D().getHeight()/2);
    }

    private class MyVector
    {
        public double x, y;

        public MyVector(Point2D.Double p1, Point2D.Double p2)
        {
            x=p2.getX()-p1.getX();
            y=p2.getY() - p1.getY();
        }
    }
}

0 个答案:

没有答案