/**
* CeasarBox.java
*
*/
import javax.swing.*;
import BreezySwing.*;
import TerminalIO.*;
public class CeasarBox extends GBFrame {
//Declare variables for the window objects.
private JLabel encryptLabel;
private JLabel decryptLabel;
private JTextField encryptField;
private JTextField decryptField;
private JButton encryptButton;
private JButton decryptButton;
//Constructor
public CeasarBox(){
//Instantiate and add window objects to the window.
encryptLabel = addLabel ("Enter phrase to be encrypted:" ,1,1,2,1);
decryptLabel = addLabel ("Enter phrase to be decrypted:" ,1,3,2,1);
encryptField = addTextField ("" ,2,1,2,1);
decryptField = addTextField ("" ,2,3,2,1);
encryptButton = addButton ("Encrypt" ,3,1,2,1);
decryptButton = addButton ("Decrypt" ,3,3,2,1);
KeyboardReader reader = new KeyboardReader();
}
//Respond to button clic events
public void buttonClicked (JButton buttonObj){
// Local variables
Encrypt eCrypt = new Encrypt();
Decrypt dCrypt = new Decrypt();
// Determine which button was clicked.
if (buttonObj == encryptButton){
//Encrypt the phrase
eCrypt.setString(encryptField.getText());
decryptField.setText(eCrypt.getString());
}else{
// Decrypt the phrase
dCrypt.setString(decryptField.getText());
encryptField.setText(dCrypt.getString());
}
}
// Execution begins in the mehtod main as usual.
public static void main(String[] args) {
CeasarBox theGUI = new CeasarBox();
theGUI.setSize (350, 100); //Set the window's size in pixels
// width = 350, height = 100
theGUI.setVisible (true); //Make the window visible
}
}
/**************End of CeasarBox.java*/
import java.awt.*;
import java.awt.event.*;
/**
* Sample application using Frame.
*
* @author
* @version 1.00 05/03/21
*/
public class CeasarBoxFrame extends Frame {
/**
* The constructor.
*/
public CeasarBoxFrame() {
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem();
menuFile.setLabel("File");
menuFileExit.setLabel("Exit");
// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
CeasarBoxFrame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("CeasarBox");
setMenuBar(menuBar);
setSize(new Dimension(400, 400));
// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
CeasarBoxFrame.this.windowClosed();
}
}
);
}
/**
* Shutdown procedure when run as an application.
*/
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
System.exit(0);
}
}
/**************End of CeasarBoxFrame.java*/
public class Decrypt {
public int stringLength;
public int square;
public String decryptedString = "";
public String inputedString = "";
public String setString(String s) {
for(int i= 0; i < s.length(); i++) {
if(s.charAt(i) != ' ') {
inputedString += s.charAt(i);
}
}
stringLength = inputedString.length();
for(int i = 1; i < (stringLength / 2); i++) {
if (i * i == stringLength) {
square = i;
break;
}
}
return inputedString;
}
public String getString() {
char[][] array = new char[square][square];
int counter = 0;
for(int i = 0; i < square; i++) {
for(int j = 0; j < square; j++) {
array[i][j] = inputedString.charAt(counter);
counter++;
}
}
for(int i = 0; i < square; i++) {
for(int j = 0; j < square; j++) {
decryptedString += array[j][i];
}
}
return decryptedString;
}
}
/*****End of decrypt.java*/
public class Encrypt {
public int stringLength;
public int square;
public String encryptedString = "";
public String inputedString = "";
public String setString(String s) {
for(int i= 0; i < s.length(); i++) {
if(s.charAt(i) != ' ') {
inputedString += s.charAt(i);
}
}
stringLength = inputedString.length();
for(int i = 1; i < (stringLength / 2); i++) {
if (i * i == stringLength) {
square = i;
break;
}
}
return inputedString;
}
public String getString() {
char[][] array = new char[square][square];
int counter = 0;
for(int i = 0; i < square; i++) {
for(int j = 0; j < square; j++) {
array[j][i] = inputedString.charAt(counter);
counter++;
}
}
for(int i = 0; i < square; i++) {
for(int j = 0; j < square; j++) {
encryptedString += array[i][j];
}
}
return encryptedString;
}
}
/*****End of encrypt.java*/