EscuchaVentana.java
Categorías: CategoryJava | CategoryProgramacion |
1 package savetheearthsk;
2
3 import java.awt.Frame;
4 import java.awt.event.WindowEvent;
5 import java.awt.event.WindowListener;
6 import java.io.FileNotFoundException;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9 import java.io.ObjectOutputStream;
10 import java.util.Iterator;
11 import java.util.Map;
12 import java.util.logging.Level;
13 import java.util.logging.Logger;
14
15 /**
16 * maneja todos los eventos que puede tener una ventana
17 * @author Luisa Fernanda Rueda Herrera
18 * @author George Sebastian Parra Macias
19 */
20 public class EscuchaVentana implements WindowListener {
21
22 /**
23 * se crea el Frame para imprimir los puntajes
24 */
25 Frame frame = new Frame("puntajes");
26 /**
27 * atributo privado
28 */
29 private Map<Integer, Puntaje> puntajes;
30 /**
31 * Atributo privado de tipo Puntaje que guarda el puntaje
32 */
33 private Puntaje puntaje;
34
35 /**
36 * Constructor de EscuchaVentana recibe comoparametros:
37 * @param puntajes es un mapa que guarda los puntajes
38 * @param puntaje es un objeto de Puntaje
39 */
40 public EscuchaVentana(Map<Integer, Puntaje> puntajes, Puntaje puntaje) {
41
42
43 this.puntajes = puntajes;
44 this.puntaje = puntaje;
45
46 }
47
48 /**
49 * metodo que hace funcionar el boton X en la ventana
50 * @param f recibe el evento
51 */
52 public void windowClosing(WindowEvent f) {
53
54 System.out.println("Puntajes\n");
55
56 for (Puntaje p : puntajes.values()) {
57 System.out.println(p);
58 }
59
60 puntajes.put(puntaje.getCantidad(), puntaje);
61
62 try {
63 ObjectOutputStream archivo = new ObjectOutputStream(new FileOutputStream("juego.dat"));
64 archivo.writeObject(puntajes);
65 archivo.close();
66 } catch (FileNotFoundException e) {
67 System.err.println("no existe el archivo");
68 Logger.getLogger(EscuchaVentana.class.getName()).log(Level.SEVERE, null, e);
69 } catch (IOException ex) {
70 Logger.getLogger(EscuchaVentana.class.getName()).log(Level.SEVERE, null, ex);
71 }
72
73 System.exit(0);
74
75 }
76
77 public void windowOpened(WindowEvent e) {
78 }
79
80 public void windowClosed(WindowEvent e) {
81 }
82
83 public void windowIconified(WindowEvent e) {
84 }
85
86 public void windowDeiconified(WindowEvent e) {
87 }
88
89 public void windowActivated(WindowEvent e) {
90 }
91
92 public void windowDeactivated(WindowEvent e) {
93 }
94 }
