import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.rmi.*;
import java.util.Vector;






public class PersisTest extends Frame
implements MouseListener, ActionListener
{
    Vector      displayList;
    String      pathname;
    Button      clearBtn, saveBtn, restoreBtn, quitBtn;


    public static void main(String args[])
    {
        if (args.length == 0)
        {
            System.out.println("Usage: java PersisTest filename");
            System.exit(0);
        }

        PersisTest that = new PersisTest(args[0]);
        that.show();
    }


    public PersisTest(String pathname)
    {
        this.pathname = pathname;
        displayList = new Vector();

        // Handle our own mouse clicks.
        addMouseListener(this);

        // Build the GUI. Make this object a listener for all actions.
        setLayout(new BorderLayout());
        Panel pan = new Panel();
        clearBtn = new Button("Clear");
        clearBtn.addActionListener(this);
        pan.add(clearBtn);
        saveBtn = new Button("Save");
        saveBtn.addActionListener(this);
        pan.add(saveBtn);
        restoreBtn = new Button("Restore");
        restoreBtn.addActionListener(this);
        pan.add(restoreBtn);
        quitBtn = new Button("Quit");
        quitBtn.addActionListener(this);
        pan.add(quitBtn);
        add("North", pan);

        resize(350, 200);
    }


    public void paint(Graphics g)
    {
        // Clear to white.
        g.setColor(Color.white);
        g.fillRect(0, 0, getSize().width, getSize().height);

        // Traverse display list, drawing 1 rect for each 2 points
        // in the vector.
        g.setColor(Color.black);
        int i = 0;
        while (i < displayList.size())
        {
            Point p0 = (Point)(displayList.elementAt(i++));
            Point p1 = (Point)(displayList.elementAt(i++));
            int x = Math.min(p0.x, p1.x);
            int y = Math.min(p0.y, p1.y);
            int w = Math.abs(p0.x - p1.x);
            int h = Math.abs(p0.y - p1.y);
            g.drawRect(x, y, w, h);
        }
    }


    public void mousePressed(MouseEvent e)
    {
        // Store x and y in display list vector.
        Point p = new Point(e.getX(), e.getY());
        displayList.addElement(p);
    }


    public void mouseReleased(MouseEvent e)
    {
        // Store x and y in display list vector, and request repaint.
        Point p = new Point(e.getX(), e.getY());
        displayList.addElement(p);
        repaint();
    }


    // Unused methods of MouseListener interface.
    public void mouseClicked(MouseEvent e) { }
    public void mouseEntered(MouseEvent e) { }
    public void mouseExited(MouseEvent e)  { }


    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == clearBtn)
        {
            // Repaint with an empty display list.
            displayList = new Vector();
            repaint();
        }
        else if (e.getSource() == saveBtn)
        {
            // Write display list vector to an object output stream.
            try
            {
                FileOutputStream fos = new FileOutputStream(pathname);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(displayList);
                oos.flush();
                oos.close();
                fos.close();
            }
            catch (Exception ex)
            {
                System.out.println("Trouble writing display list vector");
            }
        }
        else if (e.getSource() == restoreBtn)
        {
            // Read a new display list vector from an object input stream.
            try
            {
                FileInputStream fis = new FileInputStream(pathname);
                ObjectInputStream ois = new ObjectInputStream(fis);
                displayList = (Vector)(ois.readObject());
                ois.close();
                fis.close();
                repaint();
            }
            catch (Exception ex)
            {
                System.out.println("Trouble reading display list vector");
            }
        }
        else if (e.getSource() == quitBtn)
        {
            hide();
            dispose();
            System.exit(0);
        }
    }
}