-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindowManager.java
176 lines (144 loc) · 4.01 KB
/
WindowManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import java.util.HashMap;
/**
Manages all the panels
@author Kush Banker and Jack Basinet
@version 11.19.19
*/
public class WindowManager implements ActionListener
{
private ScoreManager scoreManager;
private JFrame window;
private JPanel currentPanel;
public WindowManager()
{
scoreManager = new ScoreManager();
window = new JFrame("Space Invaders");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
currentPanel = new MenuPanel(this);
window.add(currentPanel);
window.pack();
window.setVisible(true);
SoundHandler.playSong("sound/New-Hope.wav");
}
public void updatePanel(JPanel panel)
{
window.remove(currentPanel);
currentPanel = panel;
window.add(currentPanel);
window.pack();
window.setVisible(true);
}
public void saveScore(String name, int score)
{
try
{
scoreManager.addScore(name, score);
} catch(IOException e) {
e.printStackTrace();
}
}
public HashMap<Integer, String> getHighScores()
{
return scoreManager.getScores();
}
public void setWindowCursor()
{
// create new blank cursor
BufferedImage cursorImg = null;
BufferedImage cursorImgFile = null;
try
{
cursorImgFile = ImageIO.read(new File("img/pointer1.jpg"));
} catch (IOException e) {}
cursorImg = new BufferedImage(5, 5, BufferedImage.TYPE_INT_ARGB);
Graphics2D mouseG = cursorImg.createGraphics();
mouseG.drawImage(cursorImgFile, 0, 0, null);
Cursor targetCursor = Toolkit.getDefaultToolkit().createCustomCursor( cursorImg, new Point(0, 0), "targeter cursor");
// set cursor to window
window.getContentPane().setCursor(targetCursor);
}
public void resetWindowCursor()
{
window.getContentPane().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
if(command.equals("startGame"))
{
this.updatePanel(new SpaceInvaders(this, window));
this.setWindowCursor();
SoundHandler.playSong("sound/start.wav");
}
else if(command.equals("helpScreen"))
{
this.updatePanel(new HelpPanel(this));
this.resetWindowCursor();
}
else if(command.equals("menuScreen"))
{
if(currentPanel instanceof EndPanel) { SoundHandler.playSong("sound/New-Hope.wav"); }
this.updatePanel(new MenuPanel(this));
this.resetWindowCursor();
}
else if(command.equals("mute"))
{
SoundHandler.mute();
if(currentPanel instanceof MenuPanel)
{
((MenuPanel) currentPanel).toggleMuteImage();
}
}
else if(command.equals("info"))
{
this.updatePanel(new InfoPanel(this));
this.resetWindowCursor();
}
}
/**
Stores and retrieves the scores
@author Kush Banker and Jack Basinet
@version 11.20.19
*/
class ScoreManager
{
public static final String FILE_NAME = "scores.ser";
private HashMap<Integer, String> scores;
public ScoreManager()
{
scores = new HashMap<Integer, String>();
try
{
scores = this.readScores();
} catch(Exception e) {}
}
public void addScore(String name, int score) throws IOException
{
FileOutputStream fos = new FileOutputStream(FILE_NAME, false);
ObjectOutputStream oos = new ObjectOutputStream(fos);
scores.put(score, name);
oos.writeObject(scores);
fos.close();
}
public HashMap<Integer, String> getScores()
{
return scores;
}
//To convert the object that was read to a hashmap
@SuppressWarnings("unchecked")
public HashMap<Integer, String> readScores() throws IOException, ClassNotFoundException
{
FileInputStream fis = new FileInputStream(FILE_NAME);
ObjectInputStream ois = new ObjectInputStream(fis);
scores = (HashMap<Integer, String>) ois.readObject();
ois.close();
return scores;
}
}
}