-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveScoreWindow.java
More file actions
95 lines (77 loc) · 2.45 KB
/
SaveScoreWindow.java
File metadata and controls
95 lines (77 loc) · 2.45 KB
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
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.util.ArrayList;
/** Window that pops up when user finishes game and chooses to "start over".
* Gives the user the option of saving his/her score to a file (named
* "SHIP_SCORES_yyyy.mm.dd" in the current working directory). As long as the
* user is playing in the same day, the new scores will just be appended onto
* the end of the text file. If the user has not yet played that day, a new file
* will be created.
*
* @author Gemma Smith <gemma.smith@tufts.edu>
* @version 1.0
* @since 2013-12-09
*/
public class SaveScoreWindow extends JFrame implements ActionListener
{
private JFrame window;
private Main main;
private ArrayList<Score> scores;
private String defaultName;
private MyTextBox newName;
private MyButton save;
private MyButton cancel;
public SaveScoreWindow(Main mn)
{
super("Save Score");
main = mn;
window = this;
setSize(425,100);
setLocation(100,100);
scores = main.getScores();
defaultName = "game"+(scores.size()+1);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setOpaque(true);
panel.setBackground(Color.WHITE);
JLabel title = new JLabel ("Save Score As:");
title.setFont(new Font("SansSerif", Font.BOLD, 12));
title.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(title);
newName = new MyTextBox(defaultName);
panel.add(newName);
JPanel subPanel = new JPanel();
subPanel.setLayout(new BoxLayout(subPanel, BoxLayout.LINE_AXIS));
subPanel.setOpaque(true);
subPanel.setBackground(Color.WHITE);
subPanel.add(Box.createHorizontalGlue());
save = new MyButton("Save", Color.BLACK, 12);
save.addActionListener(this);
subPanel.add(save);
subPanel.add(Box.createRigidArea(new Dimension(10, 0)));
cancel = new MyButton("Don't Save Score", Color.BLACK, 12);
cancel.addActionListener(this);
subPanel.add(cancel);
subPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(subPanel);
add(panel);
setVisible(true);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
}
public void setHint()
{
save.requestFocus();
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == save)
{
main.saveScore(newName.getText());
}
window.setVisible(false);
main.openStartWindow();
window.dispose();
}
}