forked from suprgyabhushan/The-Dictation-Software
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDictation.java
581 lines (530 loc) · 18.5 KB
/
Dictation.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.FlowLayout;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;
import java.util.ArrayList;
import java.io.IOException;
/**
* Encrypts and Decrypts text using the Caesar Cihper algorithm.
* @author Invisible Computer, JTN
*
*/
public class Dictation extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private static String alphabet = "abcdefghijklmnopqrstuvwxyz";
private JTextField shiftFactor;
public JTextField value;
public JTextField value2;
private JTextField value1;
private int count=0;
public JTextArea inputTA;
private JTextArea outputTA;
private JTextArea inputTA1;
private JTextArea outputTA1;
private Scanner scanner;
JButton btn, btn2, b,readButton,reset,exe;
AudioClip click;
AudioClip click1,click2,click3,click4;
private Timer timer;
public int random=0;
public int i=0;
public int lno=0;
public int prevlno=0;
public int checkv=0;
ArrayList<String> data=new ArrayList<String>();
public int flag=0;
public int index=0;
public Thread t;
/**
* @param args
*/
public static void main(String[] args) {
new Dictation().setVisible(true);
}
public void encryptText() throws InterruptedException {
//Create a HashMap
//A hash map takes keys and values, which are both Characters in this case.
HashMap<Character, Character> alphaMap = new HashMap<Character, Character>();
int shift;
//Get the text from the app and store it in a String variable.
String textNum = this.shiftFactor.getText();
//Check to see if a "Shift Factor" value was entered.
//If there wasn't, set shift to zero,
//Otherwise parse the input value to an integer so we can use it.
if(!textNum.equals("")){
shift = Integer.parseInt(textNum)%26;
}
else{
shift = 0;
}
//Map every letter of the alphabet to another letter in the alphabet, shifted by x places.
for(int i=0; i<alphabet.length(); i++){
alphaMap.put(alphabet.charAt(i), alphabet.charAt((i+shift)%26));
}
//Get input text and put it all to lower-case so it's easy to convert
String inputText = inputTA1.getText().toLowerCase();
String outputText = "";
//Go to each letter and switch it with it's shifted counterpart
for(int j=0; j<inputText.length(); j++){
outputText = outputText.concat(alphaMap.get(inputText.charAt(j)).toString());
}
//Output the encrypted text
outputTA1.setText(outputText);
}
public void decryptText() throws InterruptedException{
HashMap<Character, Character> alphaMap = new HashMap<Character, Character>();
int shift;
String textNum = this.shiftFactor.getText();
if(!textNum.equals("")){
shift = Integer.parseInt(textNum)%26;
}
else{
shift = 0;
}
for(int i=0; i<alphabet.length(); i++){
alphaMap.put(alphabet.charAt((i+shift)%26), alphabet.charAt(i));
}
String inputText = inputTA1.getText().toLowerCase();
String outputText = "";
for(int j=0; j<inputText.length(); j++){
outputText = outputText.concat(alphaMap.get(inputText.charAt(j)).toString());
}
outputTA1.setText(outputText);
}
// public void check() throws InterruptedException, FileNotFoundException{
//HashMap<Character, Character> alphaMap = new HashMap<Character, Character>();
//int v;
//v=Integer.parseInt(this.value.getText());
/* String s = this.value.getText();
File text = new File("speech.txt");
//Creating Scanner instnace to read File in Java
Scanner scnr = new Scanner(text);
String line = scnr.nextLine();
//Reading each line of file using Scanner class
//int lineNumber = 1;
while(scnr.hasNextLine()){
line = scnr.nextLine();
s = this.value.getText();
if(s.equals(line)){
outputTA.setText("Yess");}
else{
outputTA.setText("Nooo");
}
// System.out.println("line " + lineNumber + " :" + line);
//lineNumber++;
}
//Read more: http://java67.blogspot.com/2012/11/how-to-read-file-in-java-using-scanner-example.html#ixzz3sZb28trb
//String textNum1 = this.value.getText();
/*if(!textNum1.equals("")){
v = Integer.parseInt(textNum1)%26;
}
else{
v = 1;
}*/
/*for(int i=0; i<alphabet.length(); i++){
alphaMap.put(alphabet.charAt((i+shift)%26), alphabet.charAt(i));
}*/
//String inputText = inputTA.getText().toLowerCase();
/*for(int j=0; j<inputText.length(); j++){
outputText = outputText.concat(alphaMap.get(inputText.charAt(j)).toString());
}*/
//}
public Dictation(){
setTitle("Dictation Software");
setVisible(true);
setDefaultCloseOperation(3);
Container content = getContentPane();
GridLayout layout = new GridLayout(3, 0, 0, 10);
content.setLayout(layout);
inputTA = new JTextArea("", 12, 40);
inputTA.setLineWrap(true);
inputTA.setWrapStyleWord(true);
inputTA.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
JScrollPane scroller = new JScrollPane(inputTA);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
content.add(scroller);
inputTA1 = new JTextArea("", 12, 40);
inputTA1.setLineWrap(true);
inputTA1.setWrapStyleWord(true);
inputTA1.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
JScrollPane scroller1 = new JScrollPane(inputTA1);
scroller1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
content.add(scroller1);
outputTA = new JTextArea("",12, 40);
outputTA.setLineWrap(true);
outputTA.setWrapStyleWord(true);
outputTA.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
JScrollPane scroller2 = new JScrollPane(outputTA);
scroller2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
content.add(scroller2);
outputTA1 = new JTextArea("", 12, 40);
outputTA1.setLineWrap(true);
outputTA1.setWrapStyleWord(true);
outputTA1.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
JScrollPane scroller3 = new JScrollPane(outputTA1);
scroller3.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
content.add(scroller3);
JPanel box2 = new JPanel();
box2.setLayout(new FlowLayout());
readButton = new JButton("Check");
box2.add(readButton);
content.add(box2);
inputTA.addKeyListener(new KeyListener(){
@Override
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_ENTER){
e.consume();
checkv=1;
random=1;
readButton.doClick();
}
//else
// random=0;
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
});
readButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//random=0;
String str = "";
random=1;
checkv=1;
if(flag==0)
{
while(scanner.hasNext()){
//String s = inputTA.getText();
str=scanner.next();
data.add(str);
//if(s.equals(str)){
//count=count+1;
//outputTA.append("Yesss "+"\n");
//}
//else{
// outputTA.append("Nooo " + "The correct spelling is " + str + "\n");
//}
//}
}
flag=1;
scanner.close();
//outputTA.append("Score is " + count + "\n");
//random=0;
}
if(index!=data.size())
{
String s = inputTA.getText();
//str=scanner.next();
str=data.get(index);
//data.add(str);
if(s.equals(str)){
count=count+1;
outputTA.append("Yesss "+"\n");
}
else{
outputTA.append("Nooo " + "The correct spelling is " + str + "\n");
}
index++;
}
outputTA.append("Score is " + count + "\n");
timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
b.doClick();
timer.stop();
}
});
timer.start();
}
});
try {
scanner = new Scanner(new File("speech.txt"));
scanner.useDelimiter("[,\\|]|"+System.getProperty("line.separator"));
} catch (FileNotFoundException e1) {
// Auto-generated catch block
e1.printStackTrace();
}
btn = new JButton("Play");
//int random=0;
//btn2 = new JButton("Stop");
box2.add(btn);
content.add(box2);
b = new JButton("Clear");
box2.add(b);
content.add(box2);
reset = new JButton("Reset");
box2.add(reset);
content.add(box2);
JButton exit = new JButton("Exit");
box2.add(exit);
content.add(box2);
/*exe = new JButton("Execute");
box2.add(exe);
content.add(box2);*/
//box2.add(new JLabel("Enter No. of Users : "));
//box2.add(this.value2 = new JTextField(20));
//content.add(box2);
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
reset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
outputTA.setText("");
inputTA.setText("");
index=0;
flag=1;
count=0;
t.interrupt();
//scanner.close();
/*try {
scanner = new Scanner(new File("speech1.txt"));
scanner.useDelimiter("[,\\|]|"+System.getProperty("line.separator"));
} catch (FileNotFoundException e1) {
// Auto-generated catch block
e1.printStackTrace();
}*/
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
inputTA.setText("");
//textfield.setText(null); //or use this
}
});
/*Timer timer = new Timer(5000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
b.doClick();
}
});*/
//box2.add(btn2);
//content.add(box2);
btn.addActionListener(this);
//btn2.addActionListener(this);
content.add(box2);
JPanel box1 = new JPanel();
box1.setLayout(new FlowLayout());
JButton decryptButton = new JButton("Decrypt");
JButton encryptButton = new JButton("Encrypt");
JButton resetButton = new JButton("Reset");
//JButton multiplyButton = new JButton("Check");
//JButton x = new JButton("x");
decryptButton.addActionListener(this);
encryptButton.addActionListener(this);
resetButton.addActionListener(this);
//multiplyButton.addActionListener(this);
//box1.add(x);
box1.add(decryptButton);
box1.add(encryptButton);
//box1.add(multiplyButton);
//box1.add(readButton);
box1.add(new JLabel("Shift Factor"));
box1.add(this.shiftFactor = new JTextField(20));
content.add(box1);
/*box1.add(new JLabel("Value"));
box1.add(this.value = new JTextField(20));
content.add(box1);*/
pack();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Encrypt")){
try{
encryptText();
}
catch(InterruptedException e1){
e1.printStackTrace();
}
}
/*if(e.getActionCommand().equals("Check")){
try{
check();
}
catch(InterruptedException e1){
e1.printStackTrace();
}
catch(FileNotFoundException e2){
e2.printStackTrace();i
}
}*/
/*if (e.getActionCommand().equals("Execute"))
{
try {
Process p = Runtime.getRuntime().exec("./script");
} catch (IOException e1) {
//Auto-generated catch block
e1.printStackTrace();
}
}*/
if (e.getSource() == btn) {
t = new Thread(new Runnable(){
public void run(){
try{
URL urlClick = Dictation.class.getResource("example1.wav");
URL urlClick1 = Dictation.class.getResource("example2.wav");
URL urlClick3 = Dictation.class.getResource("example3.wav");
URL urlClick4 = Dictation.class.getResource("example4.wav");
click = Applet.newAudioClip(urlClick);
click1 = Applet.newAudioClip(urlClick1);
click3 = Applet.newAudioClip(urlClick3);
click4 = Applet.newAudioClip(urlClick4);
click.play();
Thread.sleep(6000);
if(checkv==1)
{
checkv=0;
random=0;
}
else if(checkv==0)
{
Thread.sleep(4000);
if(random==0)
{
//lno=1;
readButton.doClick();
//outputTA.append("Score is " + count + "\n");
//random=0;
}
else if(random==1)
{
//lno=1;
random=0;
//prevlno=lno;
}
}
Thread.sleep(2000);
random=0;
checkv=0;
click1.play();
Thread.sleep(6000);
if(checkv==1)
{
checkv=0;
random=0;
}
else if(checkv==0)
{
Thread.sleep(4000);
if(random==0)
{
//lno=1;
readButton.doClick();
//outputTA.append("Score is " + count + "\n");
//random=0;
}
else if(random==1)
{
//lno=1;
random=0;
//prevlno=lno;
}
}
Thread.sleep(2000);
checkv=0;
random=0;
click3.play();
Thread.sleep(6000);
if(checkv==1)
{
checkv=0;
random=0;
}
else if(checkv==0)
{
Thread.sleep(4000);
if(random==0)
{
//lno=1;
readButton.doClick();
//outputTA.append("Score is " + count + "\n");
//random=0;
}
else if(random==1)
{
//lno=1;
random=0;
//prevlno=lno;
}
}
Thread.sleep(2000);
checkv=0;
random=0;
click4.play();
Thread.sleep(6000);
if(checkv==1)
{
checkv=0;
random=0;
}
else if(checkv==0)
{
Thread.sleep(4000);
if(random==0)
{
//lno=1;
readButton.doClick();
//outputTA.append("Score is " + count + "\n");
//random=0;
}
else if(random==1)
{
//lno=1;
random=0;
//prevlno=lno;
}
}
//scanner.close();
checkv=0;
random=0;
Thread.sleep(1000);
outputTA.append("Press on Reset to reset or Exit to exit the software");
}
catch (InterruptedException e1) {
Thread.currentThread().interrupt();
e1.printStackTrace();
}
}
});
t.start();
}
/*if (e.getSource() == reset) {
outputTA.setText("");
click.stop();
click1.stop();
click3.stop();
click4.stop();
}*/
if (e.getActionCommand().equals("Decrypt")){
try {
decryptText();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}