Encoder / Decoder


Just for fun. String encoding/decoding. JavaFX

encodeco1.EncoDeco1.java
 package encodeco1;  
   
 import java.io.File;  
 import java.io.FileWriter;  
 import java.io.IOException;  
 import java.net.URL;  
 import javafx.application.Application;  
 import javafx.fxml.FXML;  
 import javafx.fxml.FXMLLoader;  
 import javafx.scene.Scene;  
 import javafx.scene.control.TextArea;  
 import javafx.scene.layout.AnchorPane;  
 import javafx.scene.layout.HBox;  
 import javafx.stage.FileChooser;  
 import javafx.stage.Stage;  
   
 /**  
  *  
  * @author Djordje Gavrilovic  
  */  
 public class EncoDeco1 extends Application {  
         
   @FXML  
   private AnchorPane root;  
   @FXML  
   private HBox hBox1;   
   @FXML  
   private TextArea inputTa;  
   @FXML  
   private TextArea outputTa;  
     
   private Stage primaryStage;  
     
   @FXML  
   public void initialize() {      
     hBox1.prefWidthProperty().bind(root.widthProperty());  
   }  
       
   @FXML  
   private void encode() {  
     
     String inputStr = inputTa.getText();  
     StringBuilder outputStr = new StringBuilder();  
       
     for(int i = 0; i < inputStr.length(); i++){  
       if(i != 0){  
         outputStr.append('y');  
       }  
       outputStr.append(EncoDecoMaps.codeLibrary.get(inputStr.charAt(i)));  
      }  
       
     outputTa.setText(outputStr.toString());  
       
   }  
       
   @FXML  
   private void decode() {  
     
     String inputStr = inputTa.getText();  
     String[] inputParts = inputStr.split("y");  
     StringBuilder outputStr = new StringBuilder();  
       
     for(String part : inputParts) {  
       outputStr.append(EncoDecoMaps.letterLibrary.get(part));  
      }  
       
     outputTa.setText(outputStr.toString());  
     
   }  
     
     
   @FXML  
   private void saveFile() {  
     
     FileChooser fileChooser = new FileChooser();  
     //Set extension filter  
     FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");  
     fileChooser.getExtensionFilters().add(extFilter);  
     //Show save file dialog  
     File file = fileChooser.showSaveDialog(primaryStage);  
          
     if(file != null){  
       try {  
         FileWriter fileWriter = null;         
         fileWriter = new FileWriter(file);  
         fileWriter.write(outputTa.getText());  
         fileWriter.close();  
       } catch (IOException ex) {  
         System.out.println("Exception: " + ex.getMessage());;  
       }  
     }  
   }  
       
     
   @Override  
   public void start(Stage primaryStage) throws IOException {  
     URL fxmlUrl = getClass().getClassLoader().getResource("view/EncoDeco1View.fxml");  
     root = FXMLLoader.<AnchorPane>load(fxmlUrl);  
     Scene scene = new Scene(root);  
     primaryStage.setScene(scene);  
     primaryStage.setTitle("Encoder/Decoder");  
     primaryStage.centerOnScreen();  
     primaryStage.show();  
   }  
   
   public static void main(String[] args) {launch(args);}  
     
 }  
   

encodeco1.EncoDecoMaps.java
 package encodeco1;  
   
 import java.util.HashMap;  
 import java.util.Map;  
   
 public class EncoDecoMaps {  
     
   public static final Map<Character, String> codeLibrary;  
     static{  
       codeLibrary = new HashMap<>();  
       codeLibrary.put('a',"ox");          codeLibrary.put('b',"xooo");  
       codeLibrary.put('c',"xoxo");     codeLibrary.put('d',"xoo");  
       codeLibrary.put('e',"o");          codeLibrary.put('f',"ooxo");  
       codeLibrary.put('g',"xxo");          codeLibrary.put('h',"oooo");  
       codeLibrary.put('i',"oo");          codeLibrary.put('j',"oxxx");  
       codeLibrary.put('k',"xox");          codeLibrary.put('l',"oxoo");  
       codeLibrary.put('m',"xx");          codeLibrary.put('n',"xo");       
       codeLibrary.put('o',"xxx");          codeLibrary.put('p',"oxxo");  
       codeLibrary.put('q',"xxox");     codeLibrary.put('r',"oxo");  
       codeLibrary.put('s',"ooo");          codeLibrary.put('t',"x");  
       codeLibrary.put('u',"oox");          codeLibrary.put('v',"ooox");  
       codeLibrary.put('w',"oxx");          codeLibrary.put('x',"xoox");  
       codeLibrary.put('y',"xoxx");     codeLibrary.put('z',"xxoo");  
       codeLibrary.put('1',"oxxxxo");     codeLibrary.put('2',"ooxxx");  
       codeLibrary.put('3',"oooxx");     codeLibrary.put('4',"oxxxx");  
       codeLibrary.put('5',"ooooo");     codeLibrary.put('6',"xoooo");  
       codeLibrary.put('7',"xxooo");     codeLibrary.put('8',"xxxoo");  
       codeLibrary.put('9',"xxxxo");     codeLibrary.put('0',"xxxxx");  
       codeLibrary.put(' ',"a");      codeLibrary.put('.',"i");  
       codeLibrary.put(',',"e");      codeLibrary.put('!',"ii");  
       codeLibrary.put('?',"ee");  
       codeLibrary.put('A',"uox");          codeLibrary.put('B',"uxooo");  
       codeLibrary.put('C',"uxoxo");     codeLibrary.put('D',"uxoo");  
       codeLibrary.put('E',"uo");          codeLibrary.put('F',"uooxo");  
       codeLibrary.put('G',"uxxo");     codeLibrary.put('H',"uoooo");  
       codeLibrary.put('I',"uoo");          codeLibrary.put('J',"uoxxx");  
       codeLibrary.put('K',"uxox");     codeLibrary.put('L',"uoxoo");  
       codeLibrary.put('M',"uxx");          codeLibrary.put('N',"uxo");       
       codeLibrary.put('O',"uxxx");     codeLibrary.put('P',"uoxxo");  
       codeLibrary.put('Q',"uxxox");     codeLibrary.put('R',"uoxo");  
       codeLibrary.put('S',"uooo");     codeLibrary.put('T',"ux");  
       codeLibrary.put('U',"uoox");     codeLibrary.put('V',"uooox");  
       codeLibrary.put('W',"uoxx");     codeLibrary.put('X',"uxoox");  
       codeLibrary.put('Y',"uxoxx");     codeLibrary.put('Z',"uxxoo");  
      }  
   public static final Map<String, Character> letterLibrary;  
     static{  
       letterLibrary = new HashMap<>();  
       letterLibrary.put("ox", 'a');     letterLibrary.put("xooo", 'b');  
       letterLibrary.put("xoxo", 'c');     letterLibrary.put("xoo", 'd');  
       letterLibrary.put("o", 'e');     letterLibrary.put("ooxo", 'f');  
       letterLibrary.put("xxo",'g');     letterLibrary.put("oooo", 'h');  
       letterLibrary.put("oo", 'i');     letterLibrary.put("oxxx", 'j');  
       letterLibrary.put("xox", 'k');     letterLibrary.put("oxoo", 'l');  
       letterLibrary.put("xx", 'm');     letterLibrary.put("xo", 'n');       
       letterLibrary.put("xxx", 'o');     letterLibrary.put("oxxo", 'p');  
       letterLibrary.put("xxoo", 'q');     letterLibrary.put("oxo", 'r');  
       letterLibrary.put("ooo", 's');     letterLibrary.put("x", 't');  
       letterLibrary.put("oox", 'u');     letterLibrary.put("ooox", 'v');  
       letterLibrary.put("oxx", 'w');     letterLibrary.put("xoox", 'x');  
       letterLibrary.put("xoxx", 'y');     letterLibrary.put("xxoo", 'z');  
       letterLibrary.put("oxxxxo", '1');     letterLibrary.put("ooxxx", '2');  
       letterLibrary.put("oooxx", '3');     letterLibrary.put("oxxxx", '4');  
       letterLibrary.put("ooooo", '5');     letterLibrary.put("xoooo", '6');  
       letterLibrary.put("xxooo", '7');     letterLibrary.put("xxxoo", '8');  
       letterLibrary.put("xxxxo", '9');     letterLibrary.put("xxxxx", '0');  
       letterLibrary.put("a", ' ');    letterLibrary.put("i",'.');  
       letterLibrary.put("e",',');     letterLibrary.put("ii",'!');  
       letterLibrary.put("ee",'?');  
       letterLibrary.put("uox", 'A');     letterLibrary.put("uxooo", 'B');  
       letterLibrary.put("uxoxo", 'C');     letterLibrary.put("uxoo", 'D');  
       letterLibrary.put("uo", 'E');     letterLibrary.put("uooxo", 'F');  
       letterLibrary.put("uxxo",'G');     letterLibrary.put("uoooo", 'H');  
       letterLibrary.put("uoo", 'I');     letterLibrary.put("uoxxx", 'J');  
       letterLibrary.put("uxox", 'K');     letterLibrary.put("uoxoo", 'L');  
       letterLibrary.put("uxx", 'M');     letterLibrary.put("uxo", 'N');       
       letterLibrary.put("uxxx", 'O');     letterLibrary.put("uoxxo", 'P');  
       letterLibrary.put("uxxoo", 'Q');     letterLibrary.put("uoxo", 'R');  
       letterLibrary.put("uooo", 'S');     letterLibrary.put("ux", 'T');  
       letterLibrary.put("uoox", 'U');     letterLibrary.put("uooox", 'V');  
       letterLibrary.put("uoxx", 'W');     letterLibrary.put("uxoox", 'X');  
       letterLibrary.put("uxoxx", 'Y');     letterLibrary.put("uxxoo", 'Z');  
      }  
     
 }  
   

view.EncoDeco1View.fxml
 <?xml version="1.0" encoding="UTF-8"?>  
   
 <?import java.lang.*?>  
 <?import java.net.*?>  
 <?import java.util.*?>  
 <?import javafx.scene.*?>  
 <?import javafx.scene.control.*?>  
 <?import javafx.scene.layout.*?>  
   
 <AnchorPane fx:id="root" prefHeight="800.0" prefWidth="1000.0" styleClass="mainFxmlClass"   
    xmlns:fx="http://javafx.com/fxml/1" fx:controller="encodeco1.EncoDeco1">  
   <stylesheets>  
     <URL value="@EncoDeco1SuzieStyle.css"/>  
   </stylesheets>  
     
   <HBox fx:id="hBox1" AnchorPane.topAnchor="20.0" AnchorPane.bottomAnchor="150.0"   
      AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0" spacing="20" alignment="BASELINE_CENTER">  
       
     <VBox spacing="5">  
       <Label text="Input:" AnchorPane.topAnchor="20.0" AnchorPane.leftAnchor="20.0" />  
       <TextArea fx:id="inputTa" wrapText="true" prefHeight="500" />  
     </VBox>  
       
     <VBox spacing="5">  
       <Label text="Output:" AnchorPane.topAnchor="20.0" AnchorPane.rightAnchor="20.0" />  
       <TextArea fx:id="outputTa" wrapText="true" prefHeight="500" />  
     </VBox>  
       
   </HBox>  
     
   <HBox AnchorPane.bottomAnchor="100.0" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0"   
      alignment="BASELINE_CENTER" spacing="20">  
     <Button fx:id="encodeBtn" text="Encode" onAction="#encode" />  
     <Button fx:id="decodeBtn" text="Decode" onAction="#decode" />  
   </HBox>  
     
   <Button fx:id="saveBtn" text="Save Output to a file" onAction="#saveFile"   
       AnchorPane.rightAnchor="60.0" AnchorPane.bottomAnchor="50.0"/>  
     
 </AnchorPane>  
   

view.EncoDeco1Style.css
 .root {  
   -fx-background-color: #2c3e50;  
 }  
 .label {  
   -fx-text-fill: #27ae60;  
 }  
 .text-area {  
   -fx-control-inner-background: #2980b9;  
   -fx-text-fill: #2c3e50;   
 }  
 .button {  
   -fx-background-color: #27ae60;  
   -fx-text-fill: #2c3e50;  
 }  

Comments

Post a Comment