-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStart.java
More file actions
292 lines (241 loc) · 6.72 KB
/
Start.java
File metadata and controls
292 lines (241 loc) · 6.72 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import editor.*;
import dialogs.*;
import sim.*;
public class Start extends Frame implements Runnable, WindowListener,
ActionListener, ComponentListener,
FocusListener
{
//Font size constants for editor
public static final int SMALL=0;
public static final int MEDIUM=1;
public static final int LARGE=2;
//Windows
public EditorFrame editorFrame;
public SimFrame simFrame;
//Thread object for showing time
private StartThread st;
//Time showing label
private Label dateLine;
//Thread object
private Thread sThread;
//Editor font size
public int size=MEDIUM;
//Showing cursor flag
public boolean showPos=true;
//Printing cells flag
public boolean printCell=true;
//Printing history flag
public boolean printHistory=true;
public int timing=2000;
//Constructor
public Start()
{
//Get screen size
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
//Position Start window
setBounds(10, 10, (screen.width-20), 65);
//Set title
setTitle("Simulator for hypothetical computer");
//Add event listeners
addWindowListener(this);
addComponentListener(this);
addFocusListener(this);
//Set layout to
setLayout(new BorderLayout());
//Set background color
setBackground(SystemColor.control);
//Call openStart method
openStart();
//Add dateLine label for showing current time
dateLine=new Label("");
dateLine.setFont(new Font("Monospaced", Font.PLAIN, 12));
dateLine.setBackground(new Color(160, 255, 255));
add("South", dateLine);
//Add menubar and menus
MenuBar menuBar=new MenuBar();
setMenuBar(menuBar);
Menu exitMenu=new Menu("Exit");
menuBar.add(exitMenu);
MenuItem nowMenu=new MenuItem("Now!");
exitMenu.add(nowMenu);
nowMenu.addActionListener(this);
Menu optMenu=new Menu("Options");
menuBar.add(optMenu);
MenuItem editorMenu=new MenuItem("Editor...");
optMenu.add(editorMenu);
editorMenu.addActionListener(this);
MenuItem simMenu=new MenuItem("Simulator...");
optMenu.add(simMenu);
simMenu.addActionListener(this);
Menu hMenu=new Menu("Help");
menuBar.add(hMenu);
MenuItem helpMenu=new MenuItem("Help");
hMenu.add(helpMenu);
helpMenu.addActionListener(this);
hMenu.addSeparator();
MenuItem aboutMenu=new MenuItem("About");
hMenu.add(aboutMenu);
aboutMenu.addActionListener(this);
//Creates editor window
editorFrame=new EditorFrame(this);
//Creates thread for this window event processing
st=new StartThread(this);
//Creates simulator window
simFrame=new SimFrame(this);
}
//Main method starts this application
public static void main(String args[])
{
Start app=new Start();
app.show();
}
//Shows editor window
public void run()
{
editorFrame.show();
}
//When action is performed (button, menu, ...)
public void actionPerformed(ActionEvent e)
{
//Gets string of action command
String s=e.getActionCommand();
//Compares string and performed action according to the string
if(s.equals("Now!"))
{
byeBye(); //call byeBye method
}
if(s.equals("Editor..."))
{
//Shows editor options dialog window
EditorDialog d=new EditorDialog(this);
d.show();
}
if(s.equals("Simulator..."))
{
//Shows simulator options dialog window
SimDialog d=new SimDialog(this);
d.show();
}
if(s.equals("Help"))
{
//Shows help dialog window
HelpDialog d=new HelpDialog(this);
d.show();
}
if(s.equals("About"))
{
//Shows about dialog window
AboutDialog d=new AboutDialog(this);
d.show();
}
}
//When focus to this window is gained
public void focusGained(FocusEvent e)
{
//Get focus
editorFrame.requestFocus();
simFrame.requestFocus();
}
public void focusLost(FocusEvent e)
{
}
public void componentHidden(ComponentEvent e)
{
}
public void componentMoved(ComponentEvent e)
{
}
public void componentResized(ComponentEvent e)
{
}
//When this window is shown (only once)
public void componentShown(ComponentEvent e)
{
//Strats this, time showing and simulator event processing threads
sThread=new Thread(st);
sThread.start();
Thread thr=new Thread(this);
thr.start();
Thread thr2=new Thread(simFrame);
thr2.start();
}
//When exiting application
private void byeBye()
{
saveStart(); //Save options
editorFrame.saveEditor(); //Save editor
System.exit(0); //Exit
}
public void windowOpened(WindowEvent e)
{
}
//When window is closing via X button
public void windowClosing(WindowEvent e)
{
byeBye();
}
public void windowClosed(WindowEvent e)
{
}
public void windowIconified(WindowEvent e)
{
}
public void windowDeiconified(WindowEvent e)
{
}
public void windowActivated(WindowEvent e)
{
}
public void windowDeactivated(WindowEvent e)
{
}
//Saving options
public boolean saveStart()
{
try
{
FileOutputStream f=new FileOutputStream("Start.save"); //Opens file
ObjectOutputStream foutput=new ObjectOutputStream(f); //Attach object writter
//Write objects
foutput.writeObject(new Boolean(showPos));
foutput.writeObject(new Integer(size));
foutput.writeObject(new Boolean(printCell));
foutput.writeObject(new Boolean(printHistory));
foutput.writeObject(new Integer(timing));
f.close(); //Close
}
catch(Exception e)
{
return false;
}
return true;
}
private boolean openStart()
{
try
{
FileInputStream f=new FileInputStream("Start.save"); //Open file
ObjectInputStream finput=new ObjectInputStream(f); //Attach object reader
//REad objects
showPos=((Boolean)finput.readObject()).booleanValue();
size=((Integer)finput.readObject()).intValue();
printCell=((Boolean)finput.readObject()).booleanValue();
printHistory=((Boolean)finput.readObject()).booleanValue();
timing=((Integer)finput.readObject()).intValue();
f.close(); //Close
}
catch(Exception e)
{
return false;
}
return true;
}
//Shows date and time in label
public void showDate(String d)
{
dateLine.setText(" "+d);
}
}