-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodecGUI.java
215 lines (168 loc) · 5.5 KB
/
CodecGUI.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
package pis.hue1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class CodecGUI extends JFrame implements ActionListener {
JTextArea klartext;
JTextArea geheimtext;
JTextField loesungswort;
JTextField loesungswort2;
JRadioButton kodiere;
JRadioButton dekodiere;
JRadioButton caesar;
JRadioButton wuerfel;
JButton execute;
JButton exit;
JPanel field=new JPanel();
JPanel field2=new JPanel();
JPanel key=new JPanel();
JPanel key2=new JPanel();
JPanel choice=new JPanel();
JPanel method=new JPanel();
JPanel button=new JPanel();
ButtonGroup gruppe;
ButtonGroup gruppe2;
JLabel l1;
JLabel l2;
JLabel l3;
JLabel l4;
Container c;
Caesar ca=new Caesar();
Caesar ca2=new Caesar();
Wuerfel wf=new Wuerfel("Schwarzwald");
Wuerfel wf2=new Wuerfel("Schwenningen");
CodecGUI(){
l1 = new JLabel("klartext:");
l2=new JLabel("GeheimText:");
l3=new JLabel("Key");
l4=new JLabel("Key2");
klartext=new JTextArea(2,50);
geheimtext=new JTextArea(2,50);
loesungswort=new JTextField(20);
loesungswort2=new JTextField(20);
kodiere= new JRadioButton("kodiere");
dekodiere= new JRadioButton("dekodiere");
caesar= new JRadioButton("caesar");
wuerfel= new JRadioButton("wuerfel");
execute= new JButton("ausfuehren");
exit= new JButton("schliessen");
gruppe=new ButtonGroup();
gruppe2=new ButtonGroup();
c=new Container();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(800,600);
this.setTitle("EN/DE'crypter'");
c=this.getContentPane();
field.setLayout(new FlowLayout());
field.add(l1);
field.add(klartext);
field2.add(l2);
field2.add(geheimtext);
key.add(l3);
key.add(loesungswort);
key2.add(l4);
key2.add(loesungswort2);
choice.add(kodiere);
choice.add(dekodiere);
method.add(caesar);
method.add(wuerfel);
gruppe.add(kodiere);
gruppe.add(dekodiere);
gruppe2.add(wuerfel);
gruppe2.add(caesar);
button.add(execute);
button.add(exit);
c.add(field);
c.add(field2);
c.add(key);
c.add(key2);
c.add(method);
c.add(choice);
c.add(button);
c.setLayout(new BoxLayout(c,BoxLayout.Y_AXIS));
l1.setVerticalTextPosition(JLabel.TOP);
l1.setHorizontalTextPosition(JLabel.CENTER);
l2.setVerticalTextPosition(JLabel.TOP);
l2.setHorizontalTextPosition(JLabel.CENTER);
/**when the user presses schliessen the programm will shut off */
execute.addActionListener(this);
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
/**if caesar is selected the seconds input will be desactivated*/
caesar.addActionListener(this);
caesar.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(caesar.isSelected()){
loesungswort2.setEditable(false);
}
}
});
/**when the user selects wuerfel the 2 keys inputs will remain editable*/
wuerfel.addActionListener(this);
wuerfel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(wuerfel.isSelected()){
loesungswort2.setEditable(true);
}
}
});
}
public void actionPerformed(ActionEvent a) {
String geheim;
String klar;
/**call the kodiere method when caesar and kodiere boxes are selected*/
if(a.getSource()==this.execute && caesar.isSelected() && kodiere.isSelected()){
ca.wort=loesungswort.getText();
ca.setzeLosung(loesungswort.getText());
geheim=ca.kodiere(klartext.getText());
geheimtext.setText(geheim);
}
/**call the dekodiere method when caesar and dekodiere boxes are selected*/
if(a.getSource()==this.execute && caesar.isSelected() && dekodiere.isSelected()){
ca.wort=loesungswort.getText();
klar=ca.dekodiere(geheimtext.getText());
klartext.setText(klar);
}
/**call the kodiere method when wuerfel and kodiere boxes are selected*/
if(a.getSource()==this.execute && wuerfel.isSelected() && kodiere.isSelected()){
String krypt;
wf.key=loesungswort.getText();
wf2.key=loesungswort2.getText();
wf.setzeLosung(loesungswort.getText());
wf2.setzeLosung(loesungswort2.getText());
/**if the keys are longer than the klarText an alert will pop up*/
if(loesungswort.getText().length()>klartext.getText().length()||loesungswort2.getText().length()>klartext.getText().length()){
JOptionPane option = new JOptionPane("The Key must be shorter than the Text",JOptionPane.WARNING_MESSAGE);
JDialog log = option.createDialog("Warning!");
log.setVisible(true);
System.exit(1);
}
krypt=wf.kodiere(klartext.getText());
geheim=wf2.kodiere(krypt);
geheimtext.setText(geheim);
}
/**call the dekodiere method when wuerfel and dekodiere boxes are selected*/
if(a.getSource()==this.execute && wuerfel.isSelected() && dekodiere.isSelected()){
String dekrypt;
wf.key=loesungswort.getText();
wf2.key=loesungswort2.getText();
wf.setzeLosung(loesungswort.getText());
wf2.setzeLosung(loesungswort2.getText());
/**if the keys are longer than the geheimText an alert will pop up*/
if(loesungswort.getText().length()>geheimtext.getText().length()||loesungswort2.getText().length()>geheimtext.getText().length()){
JOptionPane option = new JOptionPane("The Key must be shorter than the Text",JOptionPane.WARNING_MESSAGE);
JDialog log = option.createDialog("Warning!");
log.setVisible(true);
System.exit(1);
}
dekrypt=wf2.dekodiere(geheimtext.getText());
klar=wf.dekodiere(dekrypt);
klartext.setText(klar);
}
}
}