Demo entry 6351829

File: Test.java

   

Submitted by anonymous on Mar 23, 2017 at 01:14
Language: Java. Code size: 3.2 kB.

package huffmanCoder.km140263d;

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.HashMap;

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.border.EmptyBorder;

import huffman.km140263d.Weight;
import huffman.km140263d.Huffman;

public class Test {
	
	Test() {
		JLabel label1 = new JLabel("Оригинални текст:");
		label1.setFont(new Font(label1.getFont().getFontName(), Font.PLAIN, 16));
		
		JTextArea textArea1 = new JTextArea();
		textArea1.setFont(new Font(label1.getFont().getFontName(), Font.PLAIN, 16));
		textArea1.setMargin(new Insets(10, 10, 10, 10));
		textArea1.setLineWrap(true);
		
		JScrollPane scroll1 = new JScrollPane (textArea1, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		
		JButton button = new JButton("Шифруј");
		button.setFont(new Font(label1.getFont().getFontName(), Font.PLAIN, 16));
		button.setMargin(new Insets(10, 10, 10, 10));
		
		JTextArea textArea2 = new JTextArea();
		textArea2.setFont(new Font(label1.getFont().getFontName(), Font.PLAIN, 16));
		textArea2.setMargin(new Insets(10, 10, 10, 10));
		textArea2.setLineWrap(true);
		
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent ae) {
				textArea2.setText("");
				String text = textArea1.getText();
				HashMap<Character, Integer> p = new HashMap<>();
				ArrayList<Weight<Character>> symbols = new ArrayList<>();
				for(int i = 0; i < text.length(); i++) {
					if(p.containsKey(text.charAt(i)))
						p.replace(text.charAt(i), p.get(text.charAt(i)) + 1);
					else
						p.put(text.charAt(i), 1);
				}
				Character[] x = p.keySet().toArray(new Character[0]);
				for(Character y: x)
					symbols.add(new Weight<Character>(y, p.get(y) * 1.0 / text.length()));
				
				Huffman<Character> huffman = new Huffman<>(symbols);
				
				for(int i = 0; i < text.length(); i++)
					textArea2.append(huffman.encode(text.charAt(i)));
			}
		});
		
		JLabel label2 = new JLabel("Хафменов код:");
		label2.setFont(new Font(label1.getFont().getFontName(), Font.PLAIN, 16));
		
		JScrollPane scroll2 = new JScrollPane (textArea2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		
		JPanel panel = new JPanel(new GridLayout(5, 1, 40, 40));
		panel.setBorder(new EmptyBorder(40, 40, 40, 40));
		
		panel.add(label1);
		panel.add(scroll1);
		panel.add(button);
		panel.add(label2);
		panel.add(scroll2);
		
		JFrame frame = new JFrame("Хафменов кодер");
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent we) {
				frame.dispose();
			}			
		});
		
		frame.add(panel);
		
		frame.setSize(1000, 800);
		frame.setResizable(false);
		frame.setVisible(true);;
	}

	public static void main(String[] args) {
		new Test();

	}

}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).