Skip to content

Instantly share code, notes, and snippets.

@Da9el00
Last active May 12, 2022 10:45
Show Gist options
  • Save Da9el00/10b8e3d60cd4c9c37a979c8f3906f39a to your computer and use it in GitHub Desktop.
Save Da9el00/10b8e3d60cd4c9c37a979c8f3906f39a to your computer and use it in GitHub Desktop.
Morse Code Translator
package sample;
import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
import javafx.scene.input.KeyEvent;
public class Controller {
@FXML
private TextArea textInput;
@FXML
private TextArea morseOutput;
MorseCodeTranslator morseCodeTranslator = new MorseCodeTranslator();
@FXML
void updateMorseCode(KeyEvent event) {
morseOutput.setText(morseCodeTranslator.translateToMorse(textInput.getText()));
}
}
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package sample;
import java.util.HashMap;
import java.util.Map;
public class MorseCodeTranslator {
Map<Character, String> mapping = new HashMap<Character, String>();
char[] letter = { 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x',
'y', 'z', '1', '2', '3', '4',
'5', '6', '7', '8', '9', '0', ' ' };
String[] morse = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".---.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----",
"..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.",
"-----", "/" };
public MorseCodeTranslator() {
setupMap();
}
private void setupMap(){
for (int i = 0; i < letter.length; i++) {
mapping.put(letter[i], morse[i]);
}
}
public String translateToMorse(String string){
char[] characters = string.toCharArray();
StringBuilder morseString = new StringBuilder();
for (char character : characters) {
morseString.append(mapping.get(character));
}
return String.valueOf(morseString);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<TextArea fx:id="textInput" layoutX="82.0" layoutY="64.0" onKeyTyped="#updateMorseCode" prefHeight="146.0" prefWidth="437.0">
<font>
<Font size="24.0" />
</font>
</TextArea>
<TextArea fx:id="morseOutput" editable="false" layoutX="82.0" layoutY="226.0" prefHeight="146.0" prefWidth="437.0">
<font>
<Font size="24.0" />
</font>
</TextArea>
<Text layoutX="175.0" layoutY="35.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Morse Code Translator">
<font>
<Font size="25.0" />
</font>
</Text>
</children>
</AnchorPane>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment