■ ■ ■ ■ ■ ■
Projects/7_ROT13_Cipher/src/Main.java
| 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 | + | |
| 7 | + | public class Main extends JFrame { |
| 8 | + | private final JTextArea textAreaOutput; |
| 9 | + | private final int offset = 13; |
| 10 | + | private String message = ""; |
| 11 | + | |
| 12 | + | public Main() { |
| 13 | + | JPanel contentPane = new JPanel(); |
| 14 | + | setTitle("ROT13 Cipher tool"); |
| 15 | + | setResizable(false); |
| 16 | + | setBounds(100, 100, 800, 600); |
| 17 | + | contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); |
| 18 | + | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 19 | + | setContentPane(contentPane); |
| 20 | + | contentPane.setLayout(null); |
| 21 | + | |
| 22 | + | //input |
| 23 | + | JLabel labelInput = new JLabel("Input:"); |
| 24 | + | labelInput.setBounds(10, 0, 146, 29); |
| 25 | + | contentPane.add(labelInput); |
| 26 | + | |
| 27 | + | JScrollPane scrollPaneInput = new JScrollPane(); |
| 28 | + | scrollPaneInput.setEnabled(false); |
| 29 | + | scrollPaneInput.setBounds(10, 30, 780, 250); |
| 30 | + | |
| 31 | + | JTextArea textAreaInput = new JTextArea(); |
| 32 | + | textAreaInput.getDocument().addDocumentListener(new DocumentListener() { |
| 33 | + | @Override |
| 34 | + | public void insertUpdate(DocumentEvent documentEvent) { |
| 35 | + | try { |
| 36 | + | message = documentEvent.getDocument().getText(0, documentEvent.getDocument().getLength()); |
| 37 | + | cipherDecipher(message,offset); |
| 38 | + | } catch (BadLocationException e) { |
| 39 | + | e.printStackTrace(); |
| 40 | + | } |
| 41 | + | } |
| 42 | + | |
| 43 | + | @Override |
| 44 | + | public void removeUpdate(DocumentEvent documentEvent) { |
| 45 | + | try { |
| 46 | + | message = documentEvent.getDocument().getText(0, documentEvent.getDocument().getLength()); |
| 47 | + | cipherDecipher(message,offset); |
| 48 | + | } catch (BadLocationException e) { |
| 49 | + | e.printStackTrace(); |
| 50 | + | } |
| 51 | + | |
| 52 | + | } |
| 53 | + | |
| 54 | + | @Override |
| 55 | + | public void changedUpdate(DocumentEvent documentEvent) { |
| 56 | + | try { |
| 57 | + | message = documentEvent.getDocument().getText(0, documentEvent.getDocument().getLength()); |
| 58 | + | cipherDecipher(message,offset); |
| 59 | + | } catch (BadLocationException e) { |
| 60 | + | e.printStackTrace(); |
| 61 | + | } |
| 62 | + | |
| 63 | + | } |
| 64 | + | }); |
| 65 | + | scrollPaneInput.setViewportView(textAreaInput); |
| 66 | + | contentPane.add(scrollPaneInput); |
| 67 | + | |
| 68 | + | //output |
| 69 | + | JLabel labelOutput = new JLabel("Output:"); |
| 70 | + | labelOutput.setBounds(10, 275, 146, 29); |
| 71 | + | |
| 72 | + | JScrollPane scrollPaneOutput = new JScrollPane(); |
| 73 | + | scrollPaneOutput.setEnabled(false); |
| 74 | + | scrollPaneOutput.setBounds(10, 300, 780, 250); |
| 75 | + | |
| 76 | + | textAreaOutput = new JTextArea(); |
| 77 | + | scrollPaneOutput.setViewportView(textAreaOutput); |
| 78 | + | |
| 79 | + | contentPane.add(labelOutput); |
| 80 | + | contentPane.add(scrollPaneOutput); |
| 81 | + | scrollPaneOutput.setViewportView(textAreaOutput); |
| 82 | + | } |
| 83 | + | |
| 84 | + | private void cipherDecipher(String message, int offset){ |
| 85 | + | StringBuilder sb = new StringBuilder(); |
| 86 | + | for (int i = 0; i < message.length(); i++) { |
| 87 | + | char c = message.charAt(i); |
| 88 | + | if (c >= 'a' && c <= 'm') c += offset; |
| 89 | + | else if (c >= 'A' && c <= 'M') c += offset; |
| 90 | + | else if (c >= 'n' && c <= 'z') c -= offset; |
| 91 | + | else if (c >= 'N' && c <= 'Z') c -= offset; |
| 92 | + | sb.append(c); |
| 93 | + | } |
| 94 | + | textAreaOutput.setText(sb.toString()); |
| 95 | + | } |
| 96 | + | |
| 97 | + | public static void main(String[] args){ |
| 98 | + | Main main = new Main(); |
| 99 | + | main.setVisible(true); |
| 100 | + | } |
| 101 | + | } |
| 102 | + | |