|
| 1 | +import java.awt.Dimension; |
| 2 | +import java.awt.GridLayout; |
| 3 | +import java.awt.event.ActionEvent; |
| 4 | +import java.awt.event.ActionListener; |
| 5 | + |
| 6 | +import javax.swing.*; |
| 7 | + |
| 8 | +public class CeaserSgui extends JFrame{ |
| 9 | + |
| 10 | + private JButton encryptC; |
| 11 | + private JLabel message1; |
| 12 | + //Offset for the Ceaser Cypher and multby for how much to multiply for AlphaNum |
| 13 | + private JTextField offset, messageS; |
| 14 | + |
| 15 | + |
| 16 | + public CeaserSgui() { |
| 17 | + //All frame code goes here |
| 18 | + createView(); |
| 19 | + |
| 20 | + setTitle("SafeFile"); |
| 21 | + setDefaultCloseOperation(EXIT_ON_CLOSE); |
| 22 | + setLocationRelativeTo(null); |
| 23 | + setResizable(true); |
| 24 | + pack(); |
| 25 | + } |
| 26 | + |
| 27 | + private void createView() { |
| 28 | + |
| 29 | + GridLayout layout = new GridLayout(0,3); |
| 30 | + JPanel panel = new JPanel(); |
| 31 | + getContentPane().add(panel); |
| 32 | + |
| 33 | + layout.setHgap(10); |
| 34 | + layout.setVgap(10); |
| 35 | + |
| 36 | + panel.setLayout(layout); |
| 37 | + |
| 38 | + message1 = new JLabel(); |
| 39 | + //message1.setPreferredSize( |
| 40 | + //new Dimension(350, 300)); |
| 41 | + message1.setText("Encrypted Message: "); |
| 42 | + panel.add(message1); |
| 43 | + |
| 44 | + //Add in the Textfield to put in the message |
| 45 | + messageS = new JTextField(); |
| 46 | + messageS.setPreferredSize( |
| 47 | + new Dimension(250, 200) |
| 48 | + ); |
| 49 | + panel.add(messageS); |
| 50 | + |
| 51 | + // Add in Offset and Multby |
| 52 | + offset = new JTextField(); |
| 53 | + panel.add(offset); |
| 54 | + |
| 55 | + messageS.setText("Offset for Ceaser Cypher"); |
| 56 | + offset.setText("Message to Encrypt"); |
| 57 | + |
| 58 | + encryptC = new JButton("Encrypt With Ceaser Cypher"); |
| 59 | + encryptC.addActionListener( |
| 60 | + new ButtonCounterActionListener() |
| 61 | + ); |
| 62 | + panel.add(encryptC); |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + public static void main(String[] args) { |
| 67 | + //Makes TryCLickme where Frame Code goes |
| 68 | + SwingUtilities.invokeLater(new Runnable(){ |
| 69 | + @Override |
| 70 | + public void run() { |
| 71 | + new CeaserSgui().setVisible(true); |
| 72 | + } |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + private class ButtonCounterActionListener implements ActionListener { |
| 77 | + |
| 78 | + @Override |
| 79 | + public void actionPerformed(ActionEvent e) { |
| 80 | + int offset; |
| 81 | + String offsetS = messageS.getText(); |
| 82 | + String enc = CeaserSgui.this.offset.getText(); |
| 83 | + |
| 84 | + offset = Integer.parseInt(offsetS); |
| 85 | + |
| 86 | + offset = offset % 26 + 26; |
| 87 | + StringBuilder encoded = new StringBuilder(); |
| 88 | + for (char i : enc.toCharArray()) { |
| 89 | + if (Character.isLetter(i)) { |
| 90 | + if (Character.isUpperCase(i)) { |
| 91 | + encoded.append((char) ('A' + (i - 'A' + offset) % 26 )); |
| 92 | + } else { |
| 93 | + encoded.append((char) ('a' + (i - 'a' + offset) % 26 )); |
| 94 | + } |
| 95 | + } else { |
| 96 | + encoded.append(i); |
| 97 | + } |
| 98 | + |
| 99 | + } |
| 100 | + message1.setText("Encrypted Message: " + encoded.toString()); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments