-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.java
184 lines (153 loc) · 5.22 KB
/
Program.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
import runlenencoder.RunLenEncoder;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Program extends JFrame {
//declares the output and user input variables
private JTextArea output;
private JTextField usr_input;
private JCheckBox case_sens;
/**
* jhaworth.veeqo.Program constructor
*/
public Program() {
//initialises the frame components and packs them to the frame
this.initComponents();
this.pack();
//sets program to stop on exit
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//sets frame to visible
this.setVisible(true);
}
/**
*
*/
private void initComponents() {
//gets the content pane of the window
Container contentPane = this.getContentPane();
//creates a new panel to add the components to
JPanel pane = new JPanel();
//creates a new grid bag layout and constraints
GridBagLayout gb = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
//sets the pane's layout to the grid bag layout
pane.setLayout(gb);
//sets the initial grid positions
gbc.gridx = 0;
gbc.gridy = 0;
//creates the title label of the window
JLabel title = new JLabel("Run Length Encoder");
//adds the title to the pane
pane.add(title, gbc);
//updates the grid positions
gbc.gridx = 0;
gbc.gridy = 1;
JLabel in_label = new JLabel("Input:");
pane.add(in_label, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
usr_input = new JTextField(null, 20);
pane.add(usr_input, gbc);
gbc.gridx = 2;
gbc.gridy = 1;
case_sens = new JCheckBox("Case Sensitive");
pane.add(case_sens, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
JLabel out_label = new JLabel("Output:");
pane.add(out_label, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
this.output = new JTextArea();
output.setEditable(false);
pane.add(output, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
JButton encode_btn = new JButton("Encode");
encode_btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
encode_action();
}
});
//adds the encode button to the pane
pane.add(encode_btn, gbc);
gbc.gridx = 1;
gbc.gridy = 3;
//creates the decode button and adds the action listener
JButton decode_btn = new JButton("Decode");
decode_btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//calls the decode function
decode_action();
}
});
//adds the decode button to the pane
pane.add(decode_btn, gbc);
gbc.gridx = 2;
gbc.gridy = 3;
JButton clear_btn = new JButton("Clear");
clear_btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
clear();
}
});
//adds the clear button to the pane
pane.add(clear_btn, gbc);
//adds the components to the content pane
contentPane.add(pane);
}
/**
* Used to carry out the encoding of the user's input
*/
private void encode_action() {
//creates a new run length encoder object
RunLenEncoder e = new RunLenEncoder();
String encode_str;
//checks if to encode should be case-sensitive
if (case_sens.isSelected()) {
//performs run length encoding on the user input
encode_str = e.encode(this.usr_input.getText());
} else {
//sets input to lower case and then performs run length encoding
encode_str = e.encode(this.usr_input.getText().toLowerCase());
}
//adds the output string to the text area
this.output.setText(null);
this.output.setText(encode_str);
}
/**
* Used to carry out the decoding of the user's input
*/
private void decode_action() {
//creates a new run length encoder object
RunLenEncoder e = new RunLenEncoder();
String decode_str;
//checks if to decode should be case-sensitive
if (case_sens.isSelected()) {
//performs run length encoding on the user input
decode_str = e.decode(this.usr_input.getText());
} else {
//sets input to lower case and then performs run length decoding
decode_str = e.decode(this.usr_input.getText().toLowerCase());
}
//adds the output string to the text area
this.output.setText(null);
this.output.setText(decode_str);
}
/**
* Used to clear all the field on the GUI
*/
private void clear() {
//clears the user input field
this.usr_input.setText(null);
//clears the output field
this.output.setText(null);
}
public static void main(String[] args) {
new Program();
}
}