import java.io.*;
import marimba.channel.*;
import marimba.gui.*;
import marimba.io.*;

public class circles extends ApplicationPlayerFrame {
    static final String stateFile = "drawing.dat";

    public void start() {
        super.start();
        readState();
    }

    public void stop() {
        writeState();
        super.stop();
    }

    /**
     * write current state to disk
     */
    public void writeState() {
	    File f = new File(context.getDataDirectory(), stateFile);
	    GroupBoxWidget gb = (GroupBoxWidget) util.getWidget("canvas");
	    Widget wList[] = gb.widgets;

        try {
	        FastOutputStream fos = new FastOutputStream(f);
            for(int i = 0;i < wList.length;i++) {
                if(wList[i] instanceof OvalWidget) {
                    fos.writeObject(wList[i]);
                }
            }
	        fos.close();
	    }
    	catch(IOException e) {
	        e.printStackTrace();
	    }
    }

    /**
     * read last state from disk
     */
    public void readState() {
	    File f = new File(context.getDataDirectory(), stateFile);
	    if (f.exists() == false) {
    	    return;
	    }

	    GroupBoxWidget gb = (GroupBoxWidget) util.getWidget("canvas");

	    try {
	        FastInputStream fis = new FastInputStream(f);
            while(fis.available() > 0) {
                gb.add((OvalWidget) fis.readObject());
            }
	        fis.close();
	    }
	    catch(IOException e) {
	        //e.printStackTrace();
	    }
    }

}