Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.event.*;
  4. import javax.swing.*;
  5. import javax.swing.text.*;
  6. import javax.swing.border.*;
  7. import java.util.*;
  8.  
  9.  
  10.  
  11. public class CipherCesarGUI_MVC {
  12.  
  13.  
  14. public static void main(String args[]) {
  15. try {
  16. UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
  17. } catch (Exception e) {}
  18. JFrame cipherGUIFrame = new CipherGUIFrame();
  19. cipherGUIFrame.setVisible(true);
  20.  
  21. }
  22. }
  23.  
  24. class CipherGUIFrame extends JFrame{
  25. public CipherGUIFrame() {
  26. super("Caesar Cipher GUI_MVC -- Courtney Ball");
  27. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28. setSize(400, 600);
  29. Container content = getContentPane();
  30. content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
  31.  
  32.  
  33.  
  34. //Variables
  35. final JTextArea Cleartext1;
  36. final JTextArea Ciphertext1;
  37. Ciphertext1 = new JTextArea(5, 10);
  38. final Cipher cipherVar = new CipherCaesar();
  39. cipherVar.setDecrypting(false);
  40.  
  41. Color tmp = this.getBackground();
  42.  
  43.  
  44.  
  45. //-----------------------Cleartext Area Jtextarea------------------------------
  46. Cleartext1 = new JTextArea(5, 10);
  47. DocumentListener cleartextListener = new DocumentListener(){
  48. public void insertUpdate(DocumentEvent e){
  49. try{
  50. if(!cipherVar.isDecrypting()){
  51. cipherVar.setCleartext(Cleartext1.getText());
  52. Ciphertext1.setText(cipherVar.getCiphertext());
  53. }
  54. }
  55. catch(CipherEncryptingException n){
  56.  
  57. System.out.println("Aww crap, thats wrong yo.");
  58. System.exit(0);
  59. }
  60.  
  61. }
  62.  
  63. public void changedUpdate(DocumentEvent e){
  64. System.out.println("Text2 is working");
  65. }
  66. public void removeUpdate(DocumentEvent e){
  67. try{
  68. if(!cipherVar.isDecrypting()){
  69. cipherVar.setCleartext(Cleartext1.getText());
  70. Ciphertext1.setText(cipherVar.getCiphertext());
  71. }
  72. }
  73. catch(CipherEncryptingException n){
  74.  
  75. System.out.println("Aww crap, thats wrong yo.");
  76. System.exit(0);
  77. }
  78.  
  79. }
  80. } ;
  81.  
  82. Cleartext1.setAlignmentX(Component.LEFT_ALIGNMENT);
  83. Cleartext1.setBackground(tmp);
  84. Cleartext1.setBorder(new TitledBorder(new EtchedBorder(),
  85. "ClearText"));
  86. Cleartext1.getDocument().addDocumentListener(cleartextListener);
  87. content.add(Cleartext1);
  88.  
  89. //-----------------------------------------------------------------------------------------------
  90. final JSpinner spinner = new JSpinner(new SpinnerNumberModel(0, 0, 25, 1)) {
  91. //numModel = new SpinnerNumberModel(0, 0, 25, 1);
  92. public Object getNextValue() {
  93. Object returnValue = super.getNextValue();
  94. if(returnValue == null) {
  95. returnValue = ((SpinnerNumberModel) super.getModel()).getMinimum();
  96. }
  97. return returnValue;
  98. }
  99. public Object getPreviousValue() {
  100. Object returnValue = super.getPreviousValue();
  101. if(returnValue == null) {
  102. returnValue = ((SpinnerNumberModel) super.getModel()).getMaximum();
  103. }
  104. return returnValue;
  105. }
  106. };
  107.  
  108. //----------------------JSPinner------------------------------------------
  109. ChangeListener spin = new ChangeListener(){
  110. public void stateChanged(ChangeEvent e){
  111. int keyValue;
  112. keyValue = Integer.parseInt(spinner.getValue().toString());
  113. cipherVar.setKey(keyValue);
  114. if(cipherVar.isDecrypting()){
  115. Cleartext1.setText(cipherVar.getCleartext());
  116. }
  117. else{
  118. Ciphertext1.setText(cipherVar.getCleartext());
  119. }
  120. }
  121. };
  122.  
  123. spinner.setBackground(tmp);
  124. spinner.setSize(100,100);
  125. spinner.setBorder(new TitledBorder(new EtchedBorder(),
  126. "Key"));
  127. spinner.addChangeListener(spin);
  128. content.add(spinner);
  129. //--------------------------------------------------------------------------
  130.  
  131.  
  132. //---------------------TOGGLE BUTTON-----------------------------------------
  133. final JToggleButton button;
  134. button = new JToggleButton("Encrypt");
  135. ActionListener toggle = new ActionListener(){
  136. public void actionPerformed(ActionEvent e){
  137. if(button.isSelected()){
  138. button.setText("Decrypt");
  139. Cleartext1.setEnabled(false);
  140. Ciphertext1.setEnabled(true);
  141. cipherVar.setDecrypting(true);
  142. }
  143. else{
  144. button.setText("Encrypt");
  145. Cleartext1.setEnabled(true);
  146. Ciphertext1.setEnabled(false);
  147. cipherVar.setDecrypting(false);
  148. }
  149. }
  150.  
  151. };
  152.  
  153.  
  154. button.addActionListener(toggle);
  155. content.add(button);
  156.  
  157.  
  158. //--------------------------------------------------------------------------
  159.  
  160.  
  161.  
  162. //-------------------JTextArea--------------------------------
  163. DocumentListener text = new DocumentListener(){
  164. public void insertUpdate(DocumentEvent e){
  165. try{
  166. if(cipherVar.isDecrypting()){
  167. cipherVar.setCiphertext(Ciphertext1.getText());
  168. Cleartext1.setText(cipherVar.getCiphertext());
  169. }
  170. }
  171. catch(CipherDecryptingException h){
  172. System.out.println("Aww crap, thats wrong yo.");
  173. System.exit(0);
  174. }
  175. }
  176.  
  177. public void changedUpdate(DocumentEvent e){
  178. System.out.println("Text2 is working");
  179. }
  180. public void removeUpdate(DocumentEvent e){
  181. try{
  182. if(cipherVar.isDecrypting()){
  183. cipherVar.setCiphertext(Ciphertext1.getText());
  184. Cleartext1.setText(cipherVar.getCiphertext());
  185. }
  186. }
  187. catch(CipherDecryptingException h){
  188. System.out.println("Aww crap, thats wrong yo.");
  189. System.exit(0);
  190. }
  191. }
  192. } ;
  193.  
  194.  
  195. Ciphertext1.setAlignmentX(Component.LEFT_ALIGNMENT);
  196. Ciphertext1.setBackground(tmp);
  197. Ciphertext1.setBorder(new TitledBorder(new EtchedBorder(),
  198. "Cipher Text"));
  199. Ciphertext1.getDocument().addDocumentListener(text);
  200. content.add(Ciphertext1);
  201.  
  202.  
  203. }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement