-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameComponents.java
More file actions
227 lines (212 loc) · 9.19 KB
/
FrameComponents.java
File metadata and controls
227 lines (212 loc) · 9.19 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
//Sinan Karabocuoglu
//10 July 2018
//Frame Components
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class FrameComponents extends JComponent {
//attitudes
public String sentence;
public String text;
public BufferedReader reader;
public ArrayList<String> lines;
public int page;
public int lineCount;//count of the lines
public int letterOrder;//order of the letter
public int sentenceOrder;//order of the sentence
public int loopCount;//count for the loop that separates the parts
public int leftMargin;
public int topMargin;
public double accuracy;//percentage of correctly pressed letters
public int totalTries;//the number of key presses
public int correctTries;//the number of correct key presses
public long startTime = 0;
public long stopTime = 0;
public double wordsCount = 0;
public static double wordPerMin;
public Graphics2D g2;
public List<Double> wordPerMinList = new ArrayList<Double>();
//constructer
public FrameComponents() {
// Initialize variables
sentence = "";
text = "";
page = 0;
lineCount = 0;
letterOrder = 0;
sentenceOrder = 0;
leftMargin = 20;
topMargin = 20;
totalTries = 0;
correctTries = 0;
accuracy = 0;
wordPerMin = 0;
lines = new ArrayList<>();
g2 = (Graphics2D) this.getGraphics();
//analyzes the passage
try {
JFileChooser chooser = new JFileChooser();
//choosing file from any directory(PUTTING .txt FILE TO DESKTOP BEFORE RUNNING TESTS WILL EASE THE PROCESS)
chooser.setCurrentDirectory(new File(System.getProperty("user.home") +
"/Dropbox/School/L12/AP Programming/Output/production/AP Programming"));
int result = chooser.showOpenDialog(this);
if(result == JFileChooser.APPROVE_OPTION) {
reader = new BufferedReader(new FileReader(chooser.getSelectedFile().getAbsolutePath()));
//gets lines of the monologue
while (true) {
sentence = reader.readLine();
if (!(sentence == null)) {
//sets each line a maximum of 50 characters so that a variety of articles can be read
for(int i = 0; i < sentence.length(); i += 50) {
if(i + 50 > sentence.length()-1){
lines.add(sentence.substring(i,sentence.length()));
}
else{
lines.add(sentence.substring(i,i + 50));
}
lineCount++;
}
} else {
break;
}
}
}else{
System.exit(1);
}
} catch (IOException e) {
System.out.println(e);
}
//*********key listener*********
class KeyListener1 implements KeyListener {
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if(totalTries == 0){
//starts the time when the first letter is pressed
startTime = System.nanoTime();
}
//increment the totalTries
totalTries++;
String character = Character.toString(e.getKeyChar());
if (character.equals(lines.get(sentenceOrder).substring(letterOrder, letterOrder + 1))) {
if(lines.get(sentenceOrder).substring(letterOrder, letterOrder + 1).equals(" ")){
//a space increases the word count
wordsCount++;
//stops the time to calculate the time passed for this word
stopTime = System.nanoTime();
//calculates the time
double time = (double)((((stopTime-startTime)/(double)(1000000000)))/(double)(60));
//updates the word per minute count
TouchTypeRead.label2.setText("Words per Minute: "+ (double)(wordsCount/time));
//gathers data for the graph
wordPerMinList.add((double)(wordsCount/time));
//always the most recent 20 points will be displayed
if(wordPerMinList.size() > 20){
wordPerMinList.remove(0);
}
}
//increments the correctTries
correctTries++;
//goes to the next character
letterOrder++;
//adjusts the values when the line ends
if(letterOrder == lines.get(sentenceOrder).length())
{ //if it was the last sentence on the page, page changes
if(sentenceOrder == 4) {
page += 5;
}
//turns back to the beginning of the sentence
letterOrder = 0;
//goes to the next sentence IN THE FILE for checking the accuracy
sentenceOrder++;
}
//finds the accuracy
accuracy = ((double)correctTries /(double) totalTries)*100;
//display the accuracy
TouchTypeRead.accuracyBar.setValue((int)accuracy);
TouchTypeRead.label.setText("The Accuracy of the Answers: %" + accuracy);
repaint();
}
}
}
//******************************
addKeyListener(new KeyListener1());
setFocusable(true);
}
public void paintComponent(Graphics g) {
g2 = (Graphics2D) g;
int order = 0;
//sets the parts of the monologue as 5 lines each
while (true) {
JTextArea label = new JTextArea();
lineCount = lineCount - 5;
if (lineCount >= 5) {
loopCount = 5;
} else if (5 > lineCount && lineCount > 0) {
loopCount = lineCount;
} else {
break;
}
//creates the display text
for (int a = loopCount; a > 0; a--) {
text = text + "\n" + " " + lines.get(order);
order++;
}
//sets the text of the label
label.setText(text);
text = "";
}
//*******************
Font textFont = new Font("Courier", Font.PLAIN, 16);
g2.setFont(textFont);
g2.setColor(Color.GREEN);
// Integer that will determine the position of the marker rect
int rectX = leftMargin;
// For loop that will loop through the five lines
for(int pageLine = 0; pageLine < 5; pageLine++){
// For loop that will draw each letter. The integer 'i' denotes which letter the loop is on and the integer
// 'xPos' keeps track of which x position the next letter will be drawn on
for (int i = 0, xPos = leftMargin; i < lines.get(page + pageLine).length(); i++) {
// Change to color to black if the marker is on a letter that hasn't been typed
if(sentenceOrder % 5 == pageLine && i == letterOrder) g2.setColor(Color.BLACK);
// Get the character
String c = lines.get(page + pageLine).substring(i, i + 1);
// Draw the character according to the xPos and pageLine
g2.drawString(c, xPos, topMargin + 20 + 20 * pageLine);
// Increase xPos by the width of the character
xPos += g2.getFontMetrics().stringWidth(c) + 1;
// If the loop is on the line that the marker should be on, calculate the x position of the marker
if(sentenceOrder % 5 == pageLine && i < letterOrder) rectX += g2.getFontMetrics().stringWidth(c) + 1;
}
}
g2.setColor(Color.MAGENTA);
g2.drawRect(rectX - 1, topMargin + 4 + (sentenceOrder%5) * 20, g2.getFontMetrics().stringWidth(lines.get(sentenceOrder).substring(letterOrder, letterOrder+1)) + 2, 20);
}
public void prevPage(){
if(page > 4) {
page -= 5;
sentenceOrder = 0;//adjust the size of the checking mechanism
letterOrder = 0;//adjust the size of the checking mechanism
repaint();//repaints the frame
requestFocus();//keeps the focus on the panel
}
}
public void nextPage(){
page += 5;
sentenceOrder = 0;//adjust the size of the checking mechanism
letterOrder = 0;//adjust the size of the checking mechanism
repaint();//repaints the frame
requestFocus();//keeps the focus on the panel
}
}