import java.awt.Point;
import java.awt.event.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

import javax.swing.JFrame;

public class CloseListener implements WindowListener {

    @Override
    public void windowActivated(WindowEvent arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void windowClosed(WindowEvent arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void windowClosing(WindowEvent arg0) {
        JFrame window = (JFrame)arg0.getSource();

        try (ObjectOutputStream f = new ObjectOutputStream(new FileOutputStream("save.dat"))) {
            int width = window.getWidth();
            int height = window.getHeight();
            Point location = window.getLocation();

            try {
                f.writeInt(width);
                f.writeInt(height);
                f.writeInt((int)location.getX());
                f.writeInt((int)location.getY());
            } catch (IOException e) {
                System.out.println(e);
            }

            try {
                f.close();
            } catch (IOException e) {
                System.out.println(e);
            }
        } catch (IOException e) {
            System.out.println(e);
        }
    }

    @Override
    public void windowDeactivated(WindowEvent arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void windowDeiconified(WindowEvent arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void windowIconified(WindowEvent arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void windowOpened(WindowEvent arg0) {
        // TODO Auto-generated method stub
        
    }
    
}
