import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
import javax.swing.*;

public class FermetureFenetre implements WindowListener {

	private int fenetreX;
	private int fenetreY;
	private JFrame fenetre;

	public FermetureFenetre(JFrame fenetre) {
		super();
		this.fenetre = fenetre;
		this.fenetreX = 300;
		this.fenetreY = 200;
	}

	public void windowActivated(WindowEvent evenement) {

	}   // premier plan

	public void windowClosed(WindowEvent evenement) {

	}        
	public void windowClosing(WindowEvent evenement) {
		try {
			Dimension dims = this.fenetre.getSize();
			FileOutputStream fichier = new FileOutputStream("windowlocation.bin");
			DataOutputStream flux = new DataOutputStream(fichier);

			try {
				flux.writeInt((int) dims.getWidth());
				flux.writeInt((int) dims.getHeight());
			} catch (IOException e2){
				System.out.println("Erreur d'écriture");
			}

			try {
				flux.close();
			} catch (IOException e3) {
				System.out.println("Erreur de fermeture");
			}
		} catch (IOException e) {
			System.out.println("Erreur d'ouverture");
		}
	}      // avant fermeture

	public void windowDeactivated(WindowEvent evenement) {

	}   // arrière-plan

	public void windowDeiconified(WindowEvent evenement) {

	} // restauration

	public void windowIconified(WindowEvent evenement){

	}    // minimisation

	public void windowOpened(WindowEvent evenement) {
		try {
			FileInputStream fichier = new FileInputStream("windowlocation.bin");
			DataInputStream flux = new DataInputStream(fichier);

			try {

				if (flux.available() >= 8) {
					this.fenetreX = flux.readInt();
					this.fenetreY = flux.readInt();
					this.fenetre.setSize(this.fenetreX, this.fenetreY);
				}


			} catch (IOException e5) {
				System.out.println("Erreur de lecture");
			}

			try {
				flux.close();
			} catch (IOException e6) {
				System.out.println("Erreur de fermeture");
			}
			
		} catch (IOException e4) {
			System.out.println("Erreur d'ouverture");
		}
	} 

	public int getX() {
		return this.fenetreX;
	}

	public int getY() {
		return this.fenetreY;
	}
}