summaryrefslogtreecommitdiff
path: root/projects/project4_huffman_tree/CompressDecompressGUI.java
blob: 20ecbd01df081104ce8e837a7502c7b1fb63e7a5 (plain)
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
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;

@SuppressWarnings("serial")
public class CompressDecompressGUI extends JFrame
{
	JPanel inputTextAreaPanel;
	JPanel outputTreePanel;
	JPanel outputCompressPanel;
	JPanel outputDecompressPanel;
	JPanel inputTextPanel;
	JPanel treeTextPanel;
	JPanel outputTextPanel;
	
	JTextArea inputTextArea;
	JTextArea outputTreeArea;
	JTextArea outputCompressArea;
	JTextArea outputDecompressArea;
	
	JScrollPane inputTextPane;
	JScrollPane outputTreePane;
	JScrollPane compressPane;
	JScrollPane decompressPane;
	
	JLabel inputNumberOfCharactersLabel;
	JLabel inputNumberOfBytesLabel;
	JLabel treeNumberOfCharactersLabel;
	JLabel treeNumberOfBytesLabel;
	JLabel outputNumberOfBytesLabel;
	JLabel outputTotalNumberOfBytesLabel;
	JLabel compressLabel;
	KeyListener kListener;
	
	public CompressDecompressGUI()
	{
		setLayout(new GridLayout(2,2));
		
		setTextAreasAndPanels();
	}

	private void setTextAreasAndPanels()
	{
		Font monoFont = new Font("Monospaced", Font.BOLD, 14);
		
		// Input Text
		
		inputTextAreaPanel = new JPanel();
		inputTextAreaPanel.setBackground(Color.WHITE);
		inputTextAreaPanel.setLayout(new BorderLayout());
		inputTextAreaPanel.setBorder(new TitledBorder("Type Your Message Here"));
		
		inputTextArea = new JTextArea();
		inputTextArea.setEditable(true);
		inputTextArea.setLineWrap(true);
		inputTextArea.setFont(monoFont);
		kListener = new InputKeyEvent();
		inputTextArea.addKeyListener(kListener);
		inputTextPane = new JScrollPane(inputTextArea);
		inputTextAreaPanel.add(inputTextPane);
		inputTextPanel = new JPanel();
		inputTextPanel.setLayout(new GridLayout(2,1));
		inputNumberOfCharactersLabel = new JLabel("Number of Characters: 0");
		inputNumberOfBytesLabel = new JLabel("Number of Bytes: 0");
		inputTextPanel.add(inputNumberOfCharactersLabel);
		inputTextPanel.add(inputNumberOfBytesLabel);
		inputTextAreaPanel.add(inputTextPanel, BorderLayout.SOUTH);
		
		// Output Tree
		
		outputTreePanel = new JPanel();
		outputTreePanel.setBackground(Color.WHITE);
		outputTreePanel.setLayout(new BorderLayout());
		outputTreePanel.setBorder(new TitledBorder("Tree Structure"));
		outputTreeArea = new JTextArea();
		outputTreeArea.setEditable(false);
		outputTreeArea.setLineWrap(true);
		outputTreeArea.setFont(monoFont);
		outputTreePane = new JScrollPane(outputTreeArea);
		outputTreePanel.add(outputTreePane);
		treeTextPanel = new JPanel();
		treeTextPanel.setLayout(new GridLayout(2,1));
		treeNumberOfCharactersLabel = new JLabel("Number of Characters: 0");
		treeNumberOfBytesLabel = new JLabel("Number of Bytes: 0");
		treeTextPanel.add(treeNumberOfCharactersLabel);
		treeTextPanel.add(treeNumberOfBytesLabel);
		outputTreePanel.add(treeTextPanel, BorderLayout.SOUTH);
		
		// Compressed Output
		
		outputCompressPanel = new JPanel();
		outputCompressPanel.setBackground(Color.WHITE);
		outputCompressPanel.setLayout(new BorderLayout());
		outputCompressPanel.setBorder(new TitledBorder("Compressed Message"));
		outputCompressArea = new JTextArea();
		outputCompressArea.setEditable(false);
		outputCompressArea.setLineWrap(true);
		outputCompressArea.setFont(monoFont);
		compressPane = new JScrollPane(outputCompressArea);
		outputCompressPanel.add(compressPane);
		outputTextPanel = new JPanel();
		outputTextPanel.setLayout(new GridLayout(3,1));
		outputNumberOfBytesLabel = new JLabel("Number of Bytes: 0");
		outputTotalNumberOfBytesLabel = new JLabel("Total Number of Bytes: 0");
		compressLabel = new JLabel("Compressed to 100% of the original size");
		outputTextPanel.add(outputNumberOfBytesLabel);
		outputTextPanel.add(outputTotalNumberOfBytesLabel);
		outputTextPanel.add(compressLabel);
		outputCompressPanel.add(outputTextPanel, BorderLayout.SOUTH);
		
		// Decompress Output
		
		outputDecompressPanel = new JPanel();
		outputDecompressPanel.setBackground(Color.WHITE);
		outputDecompressPanel.setLayout(new BorderLayout());
		outputDecompressPanel.setBorder(new TitledBorder("Decompress Message"));
		outputDecompressArea = new JTextArea();
		outputDecompressArea.setEditable(false);
		outputDecompressArea.setLineWrap(true);
		outputDecompressArea.setFont(monoFont);
		decompressPane = new JScrollPane(outputDecompressArea);
		outputDecompressPanel.add(decompressPane);
		
		add(inputTextAreaPanel);
		add(outputTreePanel);
		add(outputCompressPanel);
		add(outputDecompressPanel);
	}
	
	private class InputKeyEvent implements KeyListener
	{
		public void keyPressed(KeyEvent arg0)
		{
		}

		public void keyReleased(KeyEvent arg0)
		{
			/* Obtain the string from inputTextArea and
			 * calculate the number of bytes from its length.
			 * Assuming that we use unicode which is two bytes
			 * per character.
			 */
			String str = inputTextArea.getText();
			
			int inputStringLength = str.length();
			int inputStringBytes = str.length() * 2;
			inputNumberOfCharactersLabel.setText("Number of Characters: " + inputStringLength);
			inputNumberOfBytesLabel.setText("Number of Bytes: " + inputStringBytes);
			
			/* Generate Huffman tree from the input string
			 * and obtain the root node of the Huffman tree.			
			 */
			HuffmanTree ht = new HuffmanTree(str);
			BinaryNodeInterface<Character> root = ht.getRootNode();
			
			/* Generate a string representing the Huffman tree rooted
			 * at root and display the string on outputTreeArea.
			 */
			String treeString = CompressDecompress.getTreeString(root);
			outputTreeArea.setText(treeString);
			
			int treeStringLength = treeString.length();
			int treeStringBytes = treeStringLength * 2;
			
			treeNumberOfCharactersLabel.setText("Number of Characters: " + treeStringLength);
			treeNumberOfBytesLabel.setText("Number of Bytes: " + treeStringBytes);
			
			/* Compress the input string (str) using the Huffman tree rooted at
			 * root. The result is a new string containing just 0s and 1s. Then
			 * display the result on outputCompressArea.
			 */
			String compressedString = CompressDecompress.compress(root, str);
			outputCompressArea.setText(compressedString);
			int numBytes = compressedString.length() / 8;
			if(compressedString.length() % 8 != 0)
			{
				numBytes++;
			}
			outputNumberOfBytesLabel.setText("Number of Bytes: " +  numBytes);
			outputTotalNumberOfBytesLabel.setText("Total Number of Bytes: " + (numBytes + treeStringBytes));
			float percentCompression = (float) (numBytes + treeStringBytes) / inputStringBytes * 100; 
			compressLabel.setText("Compressed to " + String.format("%.2f", percentCompression) + "% of the original size");
			
			/* Decompress the compressed string (compressedString) using
			 * the string representing the Huffman tree (treeString). The
			 * result is a new string which should be exactly the same
			 * as the input string (str). Then display the result string
			 * on outputDecompressArea.
			 */
			String decompressedString = CompressDecompress.decompress(treeString, compressedString);
			outputDecompressArea.setText(decompressedString);
		}

		public void keyTyped(KeyEvent arg0)
		{
		}
	}
	
	public static void main(String[] args)
	{
		JFrame frame = new CompressDecompressGUI();
		frame.setSize(700,600);
		frame.setTitle("Huffman Tree - Compression / Decompression");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
}