-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmyWorld.txt
More file actions
128 lines (109 loc) · 3.35 KB
/
myWorld.txt
File metadata and controls
128 lines (109 loc) · 3.35 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
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/*
* class Encrit13
* @author Leo Hernandez
* Course: advance programming Fall 2012
* Class: This class encrypts each letter by returning the letter located 13 spaces to the right.
if there isn't a letter 13 spaces after, it wraps around and starts at the beginning.
*
* PURPOSE: the purpose for this class is to encrypt a text by changing the letter for the 13th letter after it.
*
* i got some of the code for the enter key action listener in
* http://www.coderanch.com/t/342428/GUI/java/write-event-Enter-key
*/
public class Encrit13 {
private static JTextField text = new JTextField(20);
private static JTextField encoded = new JTextField(20);
//runs when 'Enter' is pressed
private ActionListener listener = new ActionListener(){
/*
* (non-Javadoc)
* I was having some minor difficulties with the actionEvent , but I met with Vu
* one day and he helped me with the code below.
* he showed me the Java Ascii Table
*/
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String line = text.getText();
String encryptM = "";
for(int i = 0; i<line.length();i++)
{
int asciiRep = (int) line.charAt(i);
if((asciiRep >= 65 && asciiRep <= 90)|| asciiRep >= 97 && asciiRep <= 122)
{
if((asciiRep <= 77)|| (asciiRep >= 97 && asciiRep <= 109))
{
asciiRep += 13;
}//end of if
else if((asciiRep > 77 && asciiRep <= 90)|| (asciiRep >= 110 && asciiRep <= 122))
{
asciiRep -= 13;
}//end of if
}//end of main if
encryptM += (char)asciiRep;
}//end of for
encoded.setText(encryptM);
//encoded message cannot be edited
encoded.setEditable(false);
}// end of ActionPerformed
};// end of actionListener
//Constructor
/*
* Method: Encrit13
*
* This method runs the action listener
*/
public Encrit13(){
text.addActionListener(listener);
}
/*Method: frame
* this method creates the frame and makes all the parts visible
*/
public static void frame() {
//Create and set up the window.
JFrame frame = new JFrame("Encript by Rotating 13 "); // title
JPanel j1 = new JPanel(new FlowLayout());
JPanel panel2 = new JPanel();
JLabel original = new JLabel ("Original message: ");
JLabel encrypt = new JLabel ("Encrypted message:");
j1.add(original);
j1.add(text);
panel2.add(encrypt);
panel2.add(encoded);
frame.add(j1);
frame.add(panel2);
frame.setLayout(new GridLayout(2,2));
frame.setSize(300,150);
frame.setVisible(true);
frame.setLocationRelativeTo(null);// makes the window in the center of the screen
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// allows the program to close by clicking on the x
//Display the window.
frame.pack();
frame.setVisible(true);
}
/*
* Method: main
*
* This method runs the program
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
@SuppressWarnings("static-access")
public void run() {
new Encrit13().frame();
}
});
}
}