Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.event.*;
- import javax.swing.*;
- import javax.swing.text.*;
- import javax.swing.border.*;
- import java.util.*;
- public class CipherCesarGUI_MVC {
- public static void main(String args[]) {
- try {
- UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
- } catch (Exception e) {}
- JFrame cipherGUIFrame = new CipherGUIFrame();
- cipherGUIFrame.setVisible(true);
- }
- }
- class CipherGUIFrame extends JFrame{
- public CipherGUIFrame() {
- super("Caesar Cipher GUI_MVC -- Courtney Ball");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setSize(400, 600);
- Container content = getContentPane();
- content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
- //Variables
- final JTextArea Cleartext1;
- final JTextArea Ciphertext1;
- Ciphertext1 = new JTextArea(5, 10);
- final Cipher cipherVar = new CipherCaesar();
- cipherVar.setDecrypting(false);
- Color tmp = this.getBackground();
- //-----------------------Cleartext Area Jtextarea------------------------------
- Cleartext1 = new JTextArea(5, 10);
- DocumentListener cleartextListener = new DocumentListener(){
- public void insertUpdate(DocumentEvent e){
- try{
- if(!cipherVar.isDecrypting()){
- cipherVar.setCleartext(Cleartext1.getText());
- Ciphertext1.setText(cipherVar.getCiphertext());
- }
- }
- catch(CipherEncryptingException n){
- System.out.println("Aww crap, thats wrong yo.");
- System.exit(0);
- }
- }
- public void changedUpdate(DocumentEvent e){
- System.out.println("Text2 is working");
- }
- public void removeUpdate(DocumentEvent e){
- try{
- if(!cipherVar.isDecrypting()){
- cipherVar.setCleartext(Cleartext1.getText());
- Ciphertext1.setText(cipherVar.getCiphertext());
- }
- }
- catch(CipherEncryptingException n){
- System.out.println("Aww crap, thats wrong yo.");
- System.exit(0);
- }
- }
- } ;
- Cleartext1.setAlignmentX(Component.LEFT_ALIGNMENT);
- Cleartext1.setBackground(tmp);
- Cleartext1.setBorder(new TitledBorder(new EtchedBorder(),
- "ClearText"));
- Cleartext1.getDocument().addDocumentListener(cleartextListener);
- content.add(Cleartext1);
- //-----------------------------------------------------------------------------------------------
- final JSpinner spinner = new JSpinner(new SpinnerNumberModel(0, 0, 25, 1)) {
- //numModel = new SpinnerNumberModel(0, 0, 25, 1);
- public Object getNextValue() {
- Object returnValue = super.getNextValue();
- if(returnValue == null) {
- returnValue = ((SpinnerNumberModel) super.getModel()).getMinimum();
- }
- return returnValue;
- }
- public Object getPreviousValue() {
- Object returnValue = super.getPreviousValue();
- if(returnValue == null) {
- returnValue = ((SpinnerNumberModel) super.getModel()).getMaximum();
- }
- return returnValue;
- }
- };
- //----------------------JSPinner------------------------------------------
- ChangeListener spin = new ChangeListener(){
- public void stateChanged(ChangeEvent e){
- int keyValue;
- keyValue = Integer.parseInt(spinner.getValue().toString());
- cipherVar.setKey(keyValue);
- if(cipherVar.isDecrypting()){
- Cleartext1.setText(cipherVar.getCleartext());
- }
- else{
- Ciphertext1.setText(cipherVar.getCleartext());
- }
- }
- };
- spinner.setBackground(tmp);
- spinner.setSize(100,100);
- spinner.setBorder(new TitledBorder(new EtchedBorder(),
- "Key"));
- spinner.addChangeListener(spin);
- content.add(spinner);
- //--------------------------------------------------------------------------
- //---------------------TOGGLE BUTTON-----------------------------------------
- final JToggleButton button;
- button = new JToggleButton("Encrypt");
- ActionListener toggle = new ActionListener(){
- public void actionPerformed(ActionEvent e){
- if(button.isSelected()){
- button.setText("Decrypt");
- Cleartext1.setEnabled(false);
- Ciphertext1.setEnabled(true);
- cipherVar.setDecrypting(true);
- }
- else{
- button.setText("Encrypt");
- Cleartext1.setEnabled(true);
- Ciphertext1.setEnabled(false);
- cipherVar.setDecrypting(false);
- }
- }
- };
- button.addActionListener(toggle);
- content.add(button);
- //--------------------------------------------------------------------------
- //-------------------JTextArea--------------------------------
- DocumentListener text = new DocumentListener(){
- public void insertUpdate(DocumentEvent e){
- try{
- if(cipherVar.isDecrypting()){
- cipherVar.setCiphertext(Ciphertext1.getText());
- Cleartext1.setText(cipherVar.getCiphertext());
- }
- }
- catch(CipherDecryptingException h){
- System.out.println("Aww crap, thats wrong yo.");
- System.exit(0);
- }
- }
- public void changedUpdate(DocumentEvent e){
- System.out.println("Text2 is working");
- }
- public void removeUpdate(DocumentEvent e){
- try{
- if(cipherVar.isDecrypting()){
- cipherVar.setCiphertext(Ciphertext1.getText());
- Cleartext1.setText(cipherVar.getCiphertext());
- }
- }
- catch(CipherDecryptingException h){
- System.out.println("Aww crap, thats wrong yo.");
- System.exit(0);
- }
- }
- } ;
- Ciphertext1.setAlignmentX(Component.LEFT_ALIGNMENT);
- Ciphertext1.setBackground(tmp);
- Ciphertext1.setBorder(new TitledBorder(new EtchedBorder(),
- "Cipher Text"));
- Ciphertext1.getDocument().addDocumentListener(text);
- content.add(Ciphertext1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement