Site hosted by Angelfire.com: Build your free website today!
 

Drawing Editor Revisit

import graphicalobject.*;
import graphicalobject.impl.*;
import java.io.*;
import drawing.Drawing;

/**
 * @author jack
 */
public class DrawingWriter {
    private Drawing drawing = new Drawing();

    public void addElement(GraphicElement obj) {
        drawing.addElement(obj);
    }
    public void display() {
        drawing.display();
    }
    public void save(String fname) throws IOException {
        FileOutputStream os = new FileOutputStream(
            System.getProperty("user.home")+ File.separator + fname);
        ObjectOutputStream oos = new ObjectOutputStream(os);
        oos.writeObject(drawing);
        oos.close();
    }
    public void load(String fname)
        throws IOException, ClassNotFoundException {
        FileInputStream os = new FileInputStream(
            System.getProperty("user.home")+ File.separator + fname);
        ObjectInputStream ois = new ObjectInputStream(os);
        drawing = (Drawing)ois.readObject();
        ois.close();
    }

    public static void main(String[] args) {
        DrawingWriter editor = new DrawingWriter();
        GraphicElement elem;
        elem = new Circle(10);
        elem.move(new Coordinate(5,5));
        editor.addElement(elem);
        elem = new Rectangle(5,10);
        elem.move(new Coordinate(10,10));
        editor.addElement(elem);
        editor.display();
        try {
            editor.save("drawing.obj");
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            editor.load("drawing.obj");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        editor.display();
    }
}