| 1 | + | import javax.swing.*; |
| 2 | + | import javax.swing.border.EmptyBorder; |
| 3 | + | import javax.swing.event.DocumentEvent; |
| 4 | + | import javax.swing.event.DocumentListener; |
| 5 | + | import javax.swing.text.BadLocationException; |
| 6 | + | import javax.swing.text.NumberFormatter; |
| 7 | + | import java.text.NumberFormat; |
| 8 | + | |
| 9 | + | public class Main extends JFrame { |
| 10 | + | private final JLabel labelInput, labelOutput, labelOffset; |
| 11 | + | private final JScrollPane scrollPaneInput, scrollPaneOutput; |
| 12 | + | private final JTextArea textAreaInput, textAreaOutput; |
| 13 | + | private final JFormattedTextField jTextFieldOffset; |
| 14 | + | private final JToggleButton jToggleButton; |
| 15 | + | private int offset = 3; |
| 16 | + | private String message = ""; |
| 17 | + | |
| 18 | + | public Main() { |
| 19 | + | JPanel contentPane = new JPanel(); |
| 20 | + | setTitle("Caesar Cipher tool"); |
| 21 | + | setResizable(false); |
| 22 | + | setBounds(100, 100, 800, 600); |
| 23 | + | contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); |
| 24 | + | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 25 | + | setContentPane(contentPane); |
| 26 | + | contentPane.setLayout(null); |
| 27 | + | |
| 28 | + | //toggle |
| 29 | + | jToggleButton = new JToggleButton("Decrypt"); |
| 30 | + | jToggleButton.setBounds(90, 5 , 100,20); |
| 31 | + | jToggleButton.addActionListener(actionEvent -> { |
| 32 | + | if (jToggleButton.isSelected()) { |
| 33 | + | jToggleButton.setText("Encrypt"); |
| 34 | + | cipherDecipher(message,offset); |
| 35 | + | } |
| 36 | + | else { |
| 37 | + | jToggleButton.setText("Decrypt"); |
| 38 | + | cipherDecipher(message,offset); |
| 39 | + | } |
| 40 | + | }); |
| 41 | + | contentPane.add(jToggleButton); |
| 42 | + | |
| 43 | + | //offset |
| 44 | + | labelOffset = new JLabel("Offset:"); |
| 45 | + | labelOffset.setBounds(265, 0 , 146,29); |
| 46 | + | |
| 47 | + | NumberFormat longFormat = NumberFormat.getIntegerInstance(); |
| 48 | + | NumberFormatter numberFormatter = new NumberFormatter(longFormat); |
| 49 | + | numberFormatter.setValueClass(Long.class); //optional, ensures you will always get a long value |
| 50 | + | numberFormatter.setMinimum(1L); //Optional |
| 51 | + | numberFormatter.setMaximum(28L); //Optional |
| 52 | + | |
| 53 | + | jTextFieldOffset = new JFormattedTextField(numberFormatter); |
| 54 | + | jTextFieldOffset.setBounds(320, 5 , 30,20); |
| 55 | + | jTextFieldOffset.setText("3"); |
| 56 | + | jTextFieldOffset.getDocument().addDocumentListener(new DocumentListener() { |
| 57 | + | @Override |
| 58 | + | public void insertUpdate(DocumentEvent documentEvent) { |
| 59 | + | offset = Integer.parseInt(jTextFieldOffset.getText()); |
| 60 | + | cipherDecipher(message,offset); |
| 61 | + | } |
| 62 | + | |
| 63 | + | @Override |
| 64 | + | public void removeUpdate(DocumentEvent documentEvent) {} |
| 65 | + | |
| 66 | + | @Override |
| 67 | + | public void changedUpdate(DocumentEvent documentEvent) { |
| 68 | + | offset = Integer.parseInt(jTextFieldOffset.getText()); |
| 69 | + | cipherDecipher(message,offset); |
| 70 | + | } |
| 71 | + | }); |
| 72 | + | |
| 73 | + | contentPane.add(labelOffset); |
| 74 | + | contentPane.add(jTextFieldOffset); |
| 75 | + | |
| 76 | + | //input |
| 77 | + | labelInput = new JLabel("Input:"); |
| 78 | + | labelInput.setBounds(10, 0, 146, 29); |
| 79 | + | contentPane.add(labelInput); |
| 80 | + | |
| 81 | + | scrollPaneInput = new JScrollPane(); |
| 82 | + | scrollPaneInput.setEnabled(false); |
| 83 | + | scrollPaneInput.setBounds(10, 30, 780, 250); |
| 84 | + | |
| 85 | + | textAreaInput = new JTextArea(); |
| 86 | + | textAreaInput.getDocument().addDocumentListener(new DocumentListener() { |
| 87 | + | @Override |
| 88 | + | public void insertUpdate(DocumentEvent documentEvent) { |
| 89 | + | try { |
| 90 | + | message = documentEvent.getDocument().getText(0, documentEvent.getDocument().getLength()); |
| 91 | + | cipherDecipher(message,offset); |
| 92 | + | } catch (BadLocationException e) { |
| 93 | + | e.printStackTrace(); |
| 94 | + | } |
| 95 | + | } |
| 96 | + | |
| 97 | + | @Override |
| 98 | + | public void removeUpdate(DocumentEvent documentEvent) { |
| 99 | + | try { |
| 100 | + | message = documentEvent.getDocument().getText(0, documentEvent.getDocument().getLength()); |
| 101 | + | cipherDecipher(message,offset); |
| 102 | + | } catch (BadLocationException e) { |
| 103 | + | e.printStackTrace(); |
| 104 | + | } |
| 105 | + | |
| 106 | + | } |
| 107 | + | |
| 108 | + | @Override |
| 109 | + | public void changedUpdate(DocumentEvent documentEvent) { |
| 110 | + | try { |
| 111 | + | message = documentEvent.getDocument().getText(0, documentEvent.getDocument().getLength()); |
| 112 | + | cipherDecipher(message,offset); |
| 113 | + | } catch (BadLocationException e) { |
| 114 | + | e.printStackTrace(); |
| 115 | + | } |
| 116 | + | |
| 117 | + | } |
| 118 | + | }); |
| 119 | + | scrollPaneInput.setViewportView(textAreaInput); |
| 120 | + | contentPane.add(scrollPaneInput); |
| 121 | + | |
| 122 | + | //output |
| 123 | + | labelOutput = new JLabel("Output:"); |
| 124 | + | labelOutput.setBounds(10, 275, 146, 29); |
| 125 | + | |
| 126 | + | scrollPaneOutput = new JScrollPane(); |
| 127 | + | scrollPaneOutput.setEnabled(false); |
| 128 | + | scrollPaneOutput.setBounds(10, 300, 780, 250); |
| 129 | + | |
| 130 | + | textAreaOutput = new JTextArea(); |
| 131 | + | scrollPaneOutput.setViewportView(textAreaOutput); |
| 132 | + | |
| 133 | + | contentPane.add(labelOutput); |
| 134 | + | contentPane.add(scrollPaneOutput); |
| 135 | + | scrollPaneOutput.setViewportView(textAreaOutput); |
| 136 | + | } |
| 137 | + | |
| 138 | + | private void cipherDecipher(String message, int offset){ |
| 139 | + | if (!jToggleButton.isSelected()) { |
| 140 | + | textAreaOutput.setText(cipher(message, offset)); |
| 141 | + | } |
| 142 | + | else{ |
| 143 | + | textAreaOutput.setText(cipher(message,26 - (offset % 26))); |
| 144 | + | } |
| 145 | + | } |
| 146 | + | |
| 147 | + | private String cipher(String message, int offset) { |
| 148 | + | StringBuilder result = new StringBuilder(); |
| 149 | + | for (char character : message.toCharArray()) { |
| 150 | + | if (character != ' ') { |
| 151 | + | int originalAlphabetPosition = character - 'a'; |
| 152 | + | int newAlphabetPosition = (originalAlphabetPosition + offset) % 26; |
| 153 | + | char newCharacter = (char) ('a' + newAlphabetPosition); |
| 154 | + | result.append(newCharacter); |
| 155 | + | } else { |
| 156 | + | result.append(character); |
| 157 | + | } |
| 158 | + | } |
| 159 | + | return result.toString(); |
| 160 | + | } |
| 161 | + | |
| 162 | + | public static void main(String[] args){ |
| 163 | + | Main main = new Main(); |
| 164 | + | main.setVisible(true); |
| 165 | + | } |
| 166 | + | } |
| 167 | + | |