Beispiel #1
0
  /**
   * Calculate frequency of each character
   *
   * @param evt
   */
  private void btn_frequencyActionPerformed(ActionEvent evt) {
    String input = taInput.getText();
    Map<Character, Integer> character_frequency_unsorted = calc.count_characters(input);
    character_frequency = calc.sort_characters(character_frequency_unsorted);

    StringBuilder sb = new StringBuilder();
    for (Entry<Character, Integer> entry : character_frequency.entrySet()) {
      sb.append(entry.getKey()).append(" = ").append(entry.getValue()).append("\n");
    }
    taOutput.setText(sb.toString());
  }
Beispiel #2
0
 /**
  * Create huffman tree from characters en frequencies
  *
  * @param evt
  */
 private void btn_createTreeActionPerformed(ActionEvent evt) {
   if (character_frequency == null) {
     return;
   }
   tree = calc.createTree(character_frequency);
   taOutput.setText("Huffman tree created");
 }
Beispiel #3
0
  /**
   * Encode input String with Huffman tree
   *
   * @param evt
   */
  private void btn_encodeActionPerformed(ActionEvent evt) {
    if (tree == null) {
      JOptionPane.showMessageDialog(rootPane, "Create Huffman tree first to encode text");
      return;
    }
    String input = taInput.getText();
    List<Boolean> binary = calc.encodeTree(input, tree);

    StringBuilder sb = new StringBuilder();
    for (boolean b : binary) {
      sb.append(b ? '1' : '0');
    }
    taOutput.setText(sb.toString());
  }
Beispiel #4
0
 /**
  * Decode input text with Huffman tree
  *
  * @param evt
  */
 private void btn_decodeActionPerformed(ActionEvent evt) {
   if (tree == null) {
     JOptionPane.showMessageDialog(rootPane, "Create Huffman tree first to decode text");
     return;
   }
   String input = taInput.getText();
   char[] input_data = input.toCharArray();
   List<Boolean> data = new ArrayList<Boolean>();
   for (char c : input_data) {
     if (c == '0') data.add(false);
     else data.add(true);
   }
   String result = calc.decodeText(data, tree);
   taOutput.setText(result);
 }