PersisTest.java Source code

Java tutorial

Introduction

Here is the source code for PersisTest.java

Source

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PersisTest extends JFrame implements ActionListener {

    ArrayList displayList = new ArrayList();;

    String pathname;

    JButton clearBtn, saveBtn, restoreBtn, quitBtn;

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

    public PersisTest(String pathname) {
        super("Save Me");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.pathname = pathname;
        // Build the GUI. Make this object a listener for all actions.
        JPanel pan = new JPanel();
        clearBtn = new JButton("Clear");
        clearBtn.addActionListener(this);
        pan.add(clearBtn);
        saveBtn = new JButton("Save");
        saveBtn.addActionListener(this);
        pan.add(saveBtn);
        restoreBtn = new JButton("Restore");
        restoreBtn.addActionListener(this);
        pan.add(restoreBtn);
        quitBtn = new JButton("Quit");
        quitBtn.addActionListener(this);
        pan.add(quitBtn);
        Container c = getContentPane();
        c.add(pan, BorderLayout.NORTH);
        c.add(new ClickPanel(), BorderLayout.CENTER);
        setSize(350, 200);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == clearBtn) {
            // Repaint with an empty display list.
            displayList = new ArrayList();
            repaint();
        } else if (e.getSource() == saveBtn) {
            // Write display list array list 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 (IOException ex) {
                System.err.println("Trouble writing display list array list");
            }
        } else if (e.getSource() == restoreBtn) {
            // Read a new display list array list from an object input stream.
            try {
                FileInputStream fis = new FileInputStream(pathname);
                ObjectInputStream ois = new ObjectInputStream(fis);
                displayList = (ArrayList) (ois.readObject());
                ois.close();
                fis.close();
                repaint();
            } catch (ClassNotFoundException ex) {
                System.err.println("Trouble reading display list array list");
            } catch (IOException ex) {
                System.err.println("Trouble reading display list array list");
            }
        } else if (e.getSource() == quitBtn) {
            System.exit(0);
        }
    }

    class ClickPanel extends JPanel {

        ClickPanel() {
            MouseListener listener = new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
                    // Store x and y in display list array list.
                    Point p = new Point(e.getX(), e.getY());
                    displayList.add(p);
                }

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

        public void paintComponent(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 array list.
            g.setColor(Color.black);
            int i = 0;
            while (i < displayList.size()) {
                Point p0 = (Point) (displayList.get(i++));
                Point p1 = (Point) (displayList.get(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);
            }
        }
    }
}