Last active
May 12, 2022 10:45
-
-
Save Da9el00/10b8e3d60cd4c9c37a979c8f3906f39a to your computer and use it in GitHub Desktop.
Morse Code Translator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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