-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMineFrame.java
More file actions
157 lines (136 loc) · 3.71 KB
/
MineFrame.java
File metadata and controls
157 lines (136 loc) · 3.71 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
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
//John Tran
//This class is the frame that contains everything
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class MineFrame extends JFrame
{
private MineModel model;
private MinePanel panel;
//adds a menu and passes the frame to the model
public MineFrame()
{
setSize(1000,1000);
setTitle("Minesweeper");
model = new MineModel();
add(new MinePanel(this));
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
JMenu newMenu = new JMenu("New");
menuBar.add(fileMenu);
fileMenu.add(newMenu);
JMenuItem easy = new JMenuItem("SOME Mines");
JMenuItem med = new JMenuItem("ALOT Mines");
JMenuItem impos = new JMenuItem("MOAR Mines");
newMenu.add(easy);
newMenu.add(med);
newMenu.add(impos);
easy.addActionListener(new ActionHandler("Easy"));
med.addActionListener(new ActionHandler("Medium"));
impos.addActionListener(new ActionHandler("Impossible"));
JMenuItem saveItem = new JMenuItem("Save");
JMenuItem loadItem = new JMenuItem("Load");
JMenuItem quitItem = new JMenuItem("Quit");
fileMenu.add(saveItem);
fileMenu.add(loadItem);
fileMenu.add(quitItem);
saveItem.addActionListener(new ActionHandler("Save"));
loadItem.addActionListener(new ActionHandler("Load"));
quitItem.addActionListener(new ActionHandler("Quit"));
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//when a certain menu item is clicked it either changes the difficulty
//or gives the option to save and load a new or old game
private class ActionHandler implements ActionListener
{
String state;
ActionHandler(String s)
{
state = s;
}
public void actionPerformed(ActionEvent e)
{
if (state.equals("Easy"))
{
getModel().bombs = 10;
model = new MineModel();
getModel().setSquares();
}
if (state.equals("Medium"))
{
getModel().bombs = 30;
model = new MineModel();
getModel().setSquares();
}
if (state.equals("Impossible"))
{
getModel().bombs = 99;
model = new MineModel();
getModel().setSquares();
}
if (state.equals("Save"))
{
JFileChooser save = new JFileChooser();
int response = save.showSaveDialog(null);
if (response == JFileChooser.APPROVE_OPTION)
{
try
{
String path = save.getSelectedFile().getAbsolutePath();
String saveFile = path + ".ser";
System.out.println(saveFile);
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(saveFile));
out.writeObject(getModel());
out.flush();
out.close();
}
catch (IOException f)
{
System.out.println("File error.");
f.printStackTrace();
}
}
}
if (state.equals("Load"))
{
JFileChooser load = new JFileChooser();
int response = load.showOpenDialog(null);
if (response == JFileChooser.APPROVE_OPTION)
{
try
{
String path = load.getSelectedFile().getAbsolutePath();
System.out.println(path);
ObjectInputStream in = new ObjectInputStream(new FileInputStream(path));
model = (MineModel) in.readObject();
repaint();
in.close();
}
catch (IOException f)
{
System.out.println("File error.");
}
catch (ClassNotFoundException g)
{
System.out.println("Class not found");
}
}
}
if (state.equals("Quit"))
{
System.exit(0);
}
repaint();
}
}
//returns model for other classes to access
public MineModel getModel()
{
return model;
}
}