Created
February 25, 2023 11:21
-
-
Save Da9el00/e13e1fd1cb1cd7d4bb99a812bce0caf6 to your computer and use it in GitHub Desktop.
Morse to text and text to morse - JavaFX
This file contains 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
import javafx.event.ActionEvent; | |
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(); | |
boolean morseToText = false; | |
@FXML | |
void updateMorseCode(KeyEvent event) { | |
if(morseToText){ | |
morseOutput.setText(morseCodeTranslator.translateToText(textInput.getText())); | |
}else { | |
morseOutput.setText(morseCodeTranslator.translateToMorse(textInput.getText())); | |
} | |
} | |
@FXML | |
void switchStyle(ActionEvent event) { | |
morseToText = !morseToText; | |
} | |
} |
This file contains 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
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 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
import java.util.HashMap; | |
import java.util.Map; | |
public class MorseCodeTranslator { | |
Map<Character, String> morseToTextMapping = new HashMap<Character, String>(); | |
Map<String, Character> textToMorseMapping = new HashMap<String, Character>(); | |
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++) { | |
morseToTextMapping.put(letter[i], morse[i]); | |
textToMorseMapping.put(morse[i], letter[i]); | |
} | |
} | |
public String translateToMorse(String text){ | |
char[] characters = text.toCharArray(); | |
StringBuilder morseString = new StringBuilder(); | |
for (char character : characters) { | |
morseString.append(morseToTextMapping.get(character)).append(" "); | |
} | |
return String.valueOf(morseString); | |
} | |
public String translateToText(String morse){ | |
String[] morseArray = morse.split(" "); | |
StringBuilder morseString = new StringBuilder(); | |
for (String morsePart : morseArray) { | |
morseString.append(textToMorseMapping.get(morsePart)); | |
} | |
return String.valueOf(morseString); | |
} | |
} |
This file contains 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.Button?> | |
<?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> | |
<Button layoutX="519.0" layoutY="25.0" mnemonicParsing="false" onAction="#switchStyle" text="Switch" /> | |
</children> | |
</AnchorPane> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment