-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphGUI.java
453 lines (377 loc) · 16.1 KB
/
GraphGUI.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/** Class name: graphGUI
* Author: Nawal M.
* Date Modified: 24/12/2018
* Description: The GUI View for displaying the graphs and additional info
*/
/*
import javax.swing.*;
import java.awt.*;
import java.io.File;
public class GraphGUI extends JPanel{
//Instance Variables
private Graph graphModel; //The game Model
private JPanel fileInput= new JPanel();
private JFileChooser chooseData= new JFileChooser();
private JLabel enterFile= new JLabel("Enter data from a file:");
private JButton fileInputted= new JButton("Choose File");
private JPanel manualInput= new JPanel();
private JLabel enterPoints= new JLabel("Enter in the data points:");
private JTextField dataInputted= new JTextField(10);
private JPanel graphButton= new JPanel();
private JLabel pickGraph= new JLabel("Create Graph:");
private JButton graph= new JButton("Choose graph");
private JButton freqGraph= new JButton("Frequency Graph");
private JButton cumulativeFreqGraph= new JButton("Cumulative Frequency Graph");
private JButton relativeFreqGraph= new JButton("Relative Frequency Graph");
private JButton additionalInfo= new JButton("Additional Info");
private JPanel graphFreqDisplay= new JPanel();
private JPanel graphRelativeDisplay= new JPanel();
private JPanel graphCumulativeDisplay= new JPanel();
//Graph Display Components
private JPanel title= new JPanel();
private JLabel titleFreq= new JLabel("Frequency Graph");
private JLabel titleCumulativeFreq= new JLabel("Cumulative Frequency Graph");
private JLabel titleRelativeFreq= new JLabel("Relative Frequency Graph");
private JPanel display= new JPanel();
private JPanel otherOptions= new JPanel();
public GraphGUI(Graph newGraph)
{
super();
this.graphModel = newGraph;
this.graphModel.setGUI(this);
this.layoutView();
this.registerControllers();
}
private void layoutView()
{
fileInput.add(fileInputted);
manualInput.add(enterPoints);
manualInput.add(dataInputted);
graphButton.add(pickGraph);
graphButton.add(graph);
this.setLayout(new BorderLayout());
this.add(this.fileInput, BorderLayout.NORTH);
this.add(this.manualInput, BorderLayout.CENTER);
this.add(this.graphButton, BorderLayout.SOUTH);
}
private void registerControllers()
{
FreqGraphController freqController = new FreqGraphController(this.graphModel, this.freqGraph);
this.freqGraph.addActionListener(freqController);
CumulativeGraphController cumulativeController = new CumulativeGraphController(this.graphModel, this.cumulativeFreqGraph);
this.cumulativeFreqGraph.addActionListener(cumulativeController);
RelativeFreqGraphController relativeController = new RelativeFreqGraphController(this.graphModel, this.relativeFreqGraph);
this.relativeFreqGraph.addActionListener(relativeController);
AdditionalInfoController additionalController = new AdditionalInfoController(this.graphModel, this.additionalInfo);
this.additionalInfo.addActionListener(additionalController);
GraphController graphController = new GraphController(this.graphModel, this.graph);
this.graph.addActionListener(graphController);
DataInputController inputController = new DataInputController(this.graphModel, this.dataInputted);
this.dataInputted.addActionListener(inputController);
FileInputController fileController = new FileInputController(this.graphModel, this.fileInputted);
this.fileInputted.addActionListener(fileController);
}
public void displayGraphChoices()
{
graphFreqDisplay.add(freqGraph);
graphRelativeDisplay.add(relativeFreqGraph);
graphCumulativeDisplay.add(cumulativeFreqGraph);
this.setLayout(new BorderLayout());
this.add(this.graphFreqDisplay, BorderLayout.NORTH);
this.add(this.graphRelativeDisplay, BorderLayout.CENTER);
this.add(this.graphCumulativeDisplay, BorderLayout.SOUTH);
}
public void displayCumulativeFrequencyGraph()
{
title.add(titleCumulativeFreq);
//display.add(drawnFreqGraph);
otherOptions.add(freqGraph);
otherOptions.add(relativeFreqGraph);
otherOptions.add(additionalInfo);
this.setLayout(new BorderLayout());
this.add(this.title, BorderLayout.NORTH);
this.add(this.display, BorderLayout.CENTER);
this.add(this.otherOptions, BorderLayout.SOUTH);
}
public void displayRelativeFrequencyGraph()
{
/*
title.add(titleRelativeFreq);
//display.add(drawnFreqGraph);
otherOptions.add(freqGraph);
otherOptions.add(cumulativeFreqGraph);
otherOptions.add(additionalInfo);
this.setLayout(new BorderLayout());
this.add(this.title, BorderLayout.NORTH);
this.add(this.display, BorderLayout.CENTER);
this.add(this.otherOptions, BorderLayout.SOUTH);
}
public void displayFrequencyGraph()
{
//Variable Declarations
Startup.f.setVisible(false);
JFrame frame = new JFrame(); //The Frame
JPanel contents = new JPanel(); //The Content Pane
JButton cumulativeFreqGraph= new JButton("Cumulative Frequency Graph");
JButton relativeFreqGraph= new JButton("Relative Frequency Graph");
JButton additionalInfo= new JButton("Additional Info");
DrawFreqGraph drawnFreqGraph= new DrawFreqGraph();
//Add components to panels
title.add(titleFreq);
display.add(drawnFreqGraph);
otherOptions.setLayout(new FlowLayout());
otherOptions.add(relativeFreqGraph);
otherOptions.add(additionalInfo);
otherOptions.add(cumulativeFreqGraph);
contents.setLayout(new BorderLayout());
contents.add(title, BorderLayout.NORTH);
contents.add(display, BorderLayout.CENTER);
contents.add(otherOptions, BorderLayout.SOUTH);
//Display frame
frame.setContentPane(contents);
frame.setTitle("");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocation(0,0);
frame.setVisible(true);
}
public static void updateAdditional()
{
}
}
*/
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.table.TableModel;
import javax.swing.border.*;
import java.util.*;
import java.io.*;
public class GraphGUI extends JPanel
{
private Graph graphData;
JButton fileInput = new JButton("Choose a file");
JButton generateGraph = new JButton("Generate Graph");
JFileChooser chooseFile = new JFileChooser();
JPanel buttons = new JPanel();
JTabbedPane data = new JTabbedPane();
JPanel tab1 = new JPanel();
JPanel tab2 = new JPanel();
JPanel tab3 = new JPanel();
JPanel tab4 = new JPanel();
JPanel tab5 = new JPanel();
JPanel dataDisplay = new JPanel();
JPanel statusPanel = new JPanel();
DefaultTableModel model = new DefaultTableModel();
JTable numbers = new JTable(model);
JScrollPane scrollTable;
JLabel text = new JLabel("Please either choose a file or enter numbers into the table to get Statistical metrics on it.");
JLabel mean = new JLabel("Mean:");
JLabel median = new JLabel("Median:");
JLabel mode = new JLabel("Mode:");
JLabel sd = new JLabel("Standard Deviation:");
JLabel dataPoints = new JLabel("# of data points:");
JLabel meanValue = new JLabel("___");
JLabel medianValue = new JLabel("___");
JLabel modeValue = new JLabel("___");
JLabel sdValue = new JLabel("___");
JLabel dataPointsValue = new JLabel("___");
JLabel statusLabel = new JLabel();
int fileSelected = 0;
private JPanel title= new JPanel();
private JLabel titleFreq= new JLabel("Frequency Graph");
private JLabel titleCumulativeFreq= new JLabel("Cumulative Frequency Graph");
private JLabel titleRelativeFreq= new JLabel("Relative Frequency Graph");
private JPanel display= new JPanel();
private JPanel otherOptions= new JPanel();
private JButton freqGraph= new JButton("Frequency Graph");
private JButton cumulativeFreqGraph= new JButton("Cumulative Frequency Graph");
private JButton relativeFreqGraph= new JButton("Relative Frequency Graph");
private JButton additionalInfo= new JButton("Additional Info");
public GraphGUI(Graph newGraph)
{
super();
this.graphData = new Graph();
this.graphData.setGUI(this);
this.layoutView();
this.registerControllers();
}
private void layoutView()
{
this.setLayout(new BorderLayout());
dataDisplay.setLayout(new GridLayout(2,2));
buttons.add(text);
buttons.add(fileInput);
buttons.add(generateGraph);
this.add(buttons, BorderLayout.NORTH);
// dataDisplay.setLayout(new GridLayout(3,4));
// dataDisplay.add(mean);
// dataDisplay.add(meanValue);
// dataDisplay.add(median);
// dataDisplay.add(medianValue);
// dataDisplay.add(mode);
// dataDisplay.add(modeValue);
// dataDisplay.add(sd);
// dataDisplay.add(sdValue);
// dataDisplay.add(dataPoints);
// dataDisplay.add(dataPointsValue);
tab1.setLayout(new BorderLayout());
model.addColumn("Data");
TableColumn column = null;
for (int x = 0; x < 25; x++)
{
model.addRow(new Object[]{});
}
tab1.add(dataDisplay, BorderLayout.CENTER);
dataDisplay.add(freqGraph);
dataDisplay.add(cumulativeFreqGraph);
dataDisplay.add(relativeFreqGraph);
dataDisplay.add(additionalInfo);
column = numbers.getColumnModel().getColumn(0);
column.setPreferredWidth(100);
scrollTable = new JScrollPane(numbers);
scrollTable.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
tab1.add(scrollTable, BorderLayout.WEST);
dataDisplay.setBorder(BorderFactory.createTitledBorder("Options"));
data.add(tab1);
data.setTitleAt(0,"Data & Statistical Metrics");
this.add(data, BorderLayout.CENTER);
data.add(tab2);
data.add(tab3);
data.add(tab4);
data.add(tab5);
data.setTitleAt(1, "Frequency Graph");
data.setTitleAt(2, "Relative Frequency Graph");
data.setTitleAt(3, "Cumulative Frequency Graph");
data.setTitleAt(4, "Additional Information");
// statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
// statusPanel.setPreferredSize(new Dimension(this.getWidth(), 16));
// statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS));
// statusLabel.setHorizontalAlignment(SwingConstants.LEFT);
// statusPanel.add(statusLabel);
// this.add(statusPanel,BorderLayout.SOUTH);
}
private void registerControllers()
{
GraphController controller = new GraphController(graphData, fileInput);
this.fileInput.setActionCommand("LOADFILE");
this.fileInput.addActionListener(controller);
this.generateGraph.setActionCommand("GENGRAPH");
this.generateGraph.addActionListener(controller);
FreqGraphController freqController = new FreqGraphController(this.graphData, this.freqGraph);
this.freqGraph.addActionListener(freqController);
CumulativeGraphController cumulativeController = new CumulativeGraphController(this.graphData, this.cumulativeFreqGraph);
this.cumulativeFreqGraph.addActionListener(cumulativeController);
RelativeFreqGraphController relativeController = new RelativeFreqGraphController(this.graphData, this.relativeFreqGraph);
this.relativeFreqGraph.addActionListener(relativeController);
// AdditionalInfoController additionalController = new AdditionalInfoController(this.graphModel, this.additionalInfo);
// this.additionalInfo.addActionListener(additionalController);
}
/*
public void UpdateStatMetrics(ArrayList<Double> data)
{
graphData.SetGraphData(data);
meanValue.setText(graphData.getMean().toString());
medianValue.setText(graphData.getMedian().toString());
modeValue.setText(graphData.getMode().toString());
sdValue.setText(graphData.getStandardDeviation().toString());
dataPointsValue.setText(new Integer(data.size()).toString());
}
*/
public void UpdateData()
{
// Iterate through JTable and build ArrayList
ArrayList<Integer> lstValues = new ArrayList<Integer>();
for (int i=0; i < this.numbers.getRowCount(); i++)
{
Object value = ((DefaultTableModel)this.numbers.getModel()).getValueAt(i,0);
if (value != null)
{
try {
lstValues.add(Integer.parseInt(value.toString()));
}
catch (NumberFormatException ex)
{
continue;
}
}
}
// Update Stat Metrics
//if (lstValues.size() > 0)
//UpdateStatMetrics(lstValues);
}
public void LoadFileData()
{
fileSelected = chooseFile.showOpenDialog(null);
if (fileSelected == chooseFile.APPROVE_OPTION)
{
File myFile = chooseFile.getSelectedFile();
BufferedReader reader = null;
String fileContent = "";
String line;
try
{
reader = new BufferedReader(new FileReader(myFile));
while ((line = reader.readLine()) != null)
{
fileContent+=line;
}
reader.close();
}catch (Exception ex) {}
String[] fileArray = fileContent.split(",");
ArrayList<Integer> lstValues = new ArrayList<Integer>();
int value;
for (int i=0; i < fileArray.length; i++)
{
try
{
value = Integer.parseInt(fileArray[i]);
lstValues.add(value);
}
catch (NumberFormatException ex)
{
continue;
}
}
// Update JTable - Clear existing rows
( (DefaultTableModel) this.numbers.getModel()).setRowCount(0);
// Add data from File
for (Integer tmp: lstValues)
{
// Append a row
((DefaultTableModel) this.numbers.getModel()).addRow(new Object[]{tmp});
}
//if (lstValues.size() > 0)
//UpdateStatMetrics(lstValues);
}
}
public void displayFrequencyGraph()
{
JPanel contents = new JPanel();
DrawFreqGraph drawnFreqGraph= new DrawFreqGraph();
contents.add(drawnFreqGraph);
tab2.add(contents);
data.setSelectedIndex(1);
}
public void displayCumulativeFrequencyGraph()
{
JPanel contents = new JPanel();
DrawCumulativeGraph drawnCumGraph= new DrawCumulativeGraph();
contents.add(drawnCumGraph);
tab4.add(contents);
data.setSelectedIndex(3);
}
public void displayRelativeFrequencyGraph()
{
JPanel contents = new JPanel();
DrawRelativeGraph drawnRelGraph= new DrawRelativeGraph();
contents.add(drawnRelGraph);
tab3.add(contents);
data.setSelectedIndex(2);
}
public void updateAdditional()
{
JPanel contents = new JPanel();
tab5.add(contents);
}
}