-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphing.java
More file actions
299 lines (244 loc) · 10.8 KB
/
Graphing.java
File metadata and controls
299 lines (244 loc) · 10.8 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
293
294
295
296
297
298
299
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
/**
* This class just runs the program by creating the frame for it. It adds the GraphingPanel class to the frame.
*/
public class Graphing {
public Graphing () {}
/**
* This method is where the frame for the panel is created. The third line in the method gets the user's screen size
* so that the window can automatically be centered in the middle of the screen. The windowWidth and windowHeight variables
* are for the dimensions of the program's window.
*/
private static void run() {
JFrame frame = new JFrame();
GraphingPanel graphingPan = new GraphingPanel();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int windowWidth = 1000;
int windowHeight = 600;
frame.setSize(new Dimension(windowWidth, windowHeight));
frame.setLocation((int)(screenSize.getWidth()/2 - windowWidth/2), (int)(screenSize.getHeight()/2 - windowHeight/2));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(graphingPan);
frame.setVisible(true);
frame.setResizable(false);
}
public static void main(String[] args) {
run();
}
}
/**
* This class is a JPanel where the graph will be drawn.
*/
class GraphingPanel extends JPanel implements ActionListener {
private ArrayList<Double> xVals; // Original x-values from the file
private ArrayList<Double> yVals; // Original y-values from the file
private ArrayList<Integer> xValsScaled; // Scaled y-values
private ArrayList<Integer> yValsScaled; // Scaled x-values
private ArrayList<String> allTxtFiles; // Contains all the .txt files' names
private Point previousPoint; // Contains the coordinates of the previous point when graphing
private double maxX; // Max x-value of the xVals ArrayList
private double maxY; // Max y-value of the yVals ArrayList
private JCheckBox showAxes; // For showing graph axes
private JCheckBox showTickmarks; // For showing tickmarks
private JCheckBox showLabels; // For showing labels on the graph
private JCheckBox printToConsole; // For printing to console
public GraphingPanel() {
xVals = new ArrayList<Double>();
yVals = new ArrayList<Double>();
yValsScaled = new ArrayList<Integer>();
xValsScaled = new ArrayList<Integer>();
previousPoint = new Point(0, 0);
printToConsole = new JCheckBox("Print To Console");
readFileAndInitArrays("data_set1.txt");
analyzeArrays();
setLayout(new BorderLayout());
setBackground(Color.BLACK);
JPanel northGrid = new JPanel();
northGrid.setLayout(new GridLayout(1, 2));
northGrid.setBackground(Color.BLACK);
JPanel menuPanel = new JPanel();
menuPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
menuPanel.setBackground(Color.BLACK);
String[] allFiles = fileNames(".");
allTxtFiles = new ArrayList<String>();
for (int i = 0; i < allFiles.length; i++) {
if (allFiles[i].endsWith(".txt"))
allTxtFiles.add(allFiles[i]);
}
JMenuBar fileBar = new JMenuBar();
JMenu fileMenu = new JMenu("Open File");
for (int i = 0; i < allTxtFiles.size(); i++) {
JMenuItem newItem = new JMenuItem(allTxtFiles.get(i));
newItem.addActionListener(this);
fileMenu.add(newItem);
}
fileBar.add(fileMenu);
menuPanel.add(fileBar);
northGrid.add(menuPanel);
JPanel checkBoxPanel = new JPanel();
checkBoxPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
checkBoxPanel.setBackground(Color.BLACK);
showAxes = new JCheckBox("Show Axes");
showTickmarks = new JCheckBox("Show Tickmarks");
showLabels = new JCheckBox("Show Labels");
showAxes.setForeground(Color.WHITE);
showTickmarks.setForeground(Color.WHITE);
showLabels.setForeground(Color.WHITE);
printToConsole.setForeground(Color.WHITE);
showAxes.setSelected(true);
showTickmarks.setSelected(true);
showLabels.setSelected(true);
printToConsole.setSelected(false);
showAxes.addActionListener(this);
showTickmarks.addActionListener(this);
showLabels.addActionListener(this);
checkBoxPanel.add(showAxes);
checkBoxPanel.add(showTickmarks);
checkBoxPanel.add(showLabels);
checkBoxPanel.add(printToConsole);
northGrid.add(checkBoxPanel);
add(northGrid, BorderLayout.NORTH);
}
/**
* Whenever this is called, the method first determines which component called it. If it was not a JCheckBox, then
* it knows that it was a JMenuItem. It will then reread the file, reinitialize the ArrayLists and arrays, and redraw
* the graph. Lastly, it repaints regardless of whether the component was a JCheckBox or a JMenuItem.
* @param evt - The ActionEvent that allows more information to be found out about the event
*/
public void actionPerformed(ActionEvent evt) {
String command = evt.getActionCommand();
if (!(command.equals("Show Axes") || command.equals("Show Tickmarks") || command.equals("Show Labels") || command.equals("Print To Console"))) {
readFileAndInitArrays(command);
analyzeArrays();
}
this.repaint();
}
/**
* This method finds all the files in the directory.
* @param directoryPath - The path of the directory that is needed to be searched. In this case, it's the current directory,
* so the path is ".".
* @return - Returns a String[] containing the names of all of the directory's files.
*/
public static String[] fileNames(String directoryPath) {
File dir = new File(directoryPath);
Collection<String> files = new ArrayList<String>();
if (dir.isDirectory()) {
File[] listFiles = dir.listFiles();
for (File file : listFiles) {
if (file.isFile()) {
files.add(file.getName());
}
}
}
return files.toArray(new String[]{});
}
/**
* Here, the file data.txt is read and processed. The first line contains the x-values, and the second line contains
* the y-values. In order to read the file in that format, the Scanner is first initialized, and it skips over the
* "x" label in the file. It then stores all the numbers in that row using input.next() until the value of input.next()
* becomes a String "y". At this point, it knows that it has reached the next line, and it will store the next row of
* numbers into a different array called yVals. The previousPoint[] array is used for being able to draw in the
* paintComponent() method.
*/
public void readFileAndInitArrays(String fileName) {
xVals = new ArrayList<Double>();
yVals = new ArrayList<Double>();
yValsScaled = new ArrayList<Integer>();
xValsScaled = new ArrayList<Integer>();
previousPoint = new Point(0, 0);
Scanner input = null;
File data = new File(fileName);
try {
input = new Scanner(data);
} catch (FileNotFoundException e) {
System.out.println("The file wasn't found.");
System.exit(1);
}
input.next();
String value = input.next();
while (!value.equals("y")) {
xVals.add(Double.parseDouble(value));
value = input.next();
}
while (input.hasNext()) {
value = input.next();
yVals.add(Double.parseDouble(value));
}
if (printToConsole.isSelected()) {
System.out.println("\n\n\n");
System.out.println("x: " + xVals.toString());
System.out.println("y: " + yVals.toString());
System.out.println();
}
input.close();
}
/**
* In this method, the values are scaled so that they cover the entire grid. All the x-values and y-values are multiplied
* by 400/maxY or 800/maxX.
*/
public void analyzeArrays() {
maxX = Collections.max(xVals);
for (int i = 0; i < xVals.size(); i++) {
xValsScaled.add((int)(xVals.get(i) * 800/maxX));
}
maxY = Collections.max(yVals);
for (int i = 0; i < yVals.size(); i++) {
yValsScaled.add((int)(yVals.get(i) * 400/maxY));
}
if (printToConsole.isSelected()) {
System.out.println("Max Y: " + maxX);
System.out.println("Max Y: " + maxY);
System.out.println("x new: " + xValsScaled.toString());
System.out.println("y new: " + yValsScaled.toString());
System.out.println("\n\n\n");
}
}
/**
* First, the axes, the tickmarks, and labels are drawn if needed. For drawing the graph, the grid is drawn first.
* Then, there is a for-loop that starts from index 1 and repeats until xVals.size(). The reason why it starts
* from 1 and not 0 is because we already have the point stored at the 0th index in the previousPoint[] array if
* i == 1 in the for-loop. The previousPoint[] is then reinitialized with new values, and the graphing repeats
* until all the data has been drawn.
*/
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.WHITE);
// Graph axes
if (showAxes.isSelected()) {
g.drawLine(100, 100, 100, 500);
g.drawLine(100, 500, 900, 500);
}
// Tick marks
if (showTickmarks.isSelected()) {
g.drawLine(90, 100, 110, 100);
g.drawLine(900, 490, 900, 510);
g.drawLine(90, 300, 110, 300);
g.drawLine(500, 490, 500, 510);
g.drawLine(100, 500, 90, 500);
g.drawLine(100, 500, 100, 510);
}
// Labels
if (showLabels.isSelected()) {
g.drawString("" + (int)maxY, 25, 105);
g.drawString("" + (int)maxX, 893, 550);
g.drawString("" + (int)maxY/2.0, 25, 305);
g.drawString("" + (int)maxX/2.0, 493, 550);
g.drawString("0", 25, 505);
g.drawString("0", 97, 550);
}
for (int i = 1; i < xValsScaled.size(); i++) {
g.setColor(Color.GREEN);
if (i == 1) {
previousPoint = new Point(xValsScaled.get(0), yValsScaled.get(0));
}
g.drawLine(previousPoint.x + 100, 500 - previousPoint.y, xValsScaled.get(i) + 100, 500 - yValsScaled.get(i));
previousPoint.x = xValsScaled.get(i);
previousPoint.y = yValsScaled.get(i);
}
}
}