import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JSlider;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
public class SecretMessageGUI extends JFrame {
private JTextField txtKey;
private JTextArea txtIn;
private JTextArea txtOut;
private JSlider slider;
public String encode(String message, int keyVal){
String output = "";
char key = (char)keyVal;
for ( int x = 0; x <= message.length()-1; x++)
{
char input = message.charAt(x);
if(input >= 'A' && input <= 'Z')
{
input += key;
if(input > 'Z')
input -= 26;
if(input < 'A')
input += 26;
}
else if(input >= 'a' && input <= 'z')
{
input += key;
if(input > 'z')
input -= 26;
if(input < 'a')
input += 26;
}
else if(input >= '0' && input <= '9')
{
input += (keyVal % 10);
if(input > '9')
input -= 10;
if(input < '0')
input += 10;
}
output += input;
}
return output;
}
public SecretMessageGUI() {
getContentPane().setBackground(Color.GREEN);
setTitle("SecretMessage App");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
txtIn = new JTextArea();
txtIn.setLineWrap(true);
txtIn.setWrapStyleWord(true);
txtIn.setFont(new Font("Lucida Console", Font.BOLD, 18));
txtIn.setBounds(14, 13, 554, 136);
getContentPane().add(txtIn);
txtOut = new JTextArea();
txtOut.setLineWrap(true);
txtOut.setWrapStyleWord(true);
txtOut.setFont(new Font("Lucida Console", Font.BOLD, 18));
txtOut.setBounds(14, 203, 554, 136);
getContentPane().add(txtOut);
txtKey = new JTextField();
txtKey.setBounds(309, 164, 44, 24);
getContentPane().add(txtKey);
txtKey.setColumns(10);
JLabel lblNewLabel = new JLabel("Key:");
lblNewLabel.setBounds(263, 167, 32, 18);
getContentPane().add(lblNewLabel);
JButton btnNewButton = new JButton("Encode/Decode");
btnNewButton.setBackground(Color.GREEN);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try
{
String message = txtIn.getText();
int key = Integer.parseInt(txtKey.getText());
String output = encode(message, key);
txtOut.setText(output);
}catch(Exception ex){
JOptionPane.showMessageDialog(null,
"Please enter a whole number value for the encryption key.");
txtKey.requestFocus();
txtKey.selectAll();
}
}
});
btnNewButton.setBounds(391, 163, 154, 27);
getContentPane().add(btnNewButton);
slider = new JSlider();
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
txtKey.setText(""+slider.getValue());
String message = txtIn.getText();
int key = Integer.parseInt(txtKey.getText());
String output = encode(message, key);
txtOut.setText(output);
}
});
slider.setMajorTickSpacing(13);
slider.setMinorTickSpacing(1);
slider.setPaintTicks(true);
slider.setMinimum(-26);
slider.setMaximum(26);
slider.setPaintLabels(true);
slider.setBackground(Color.CYAN);
slider.setBounds(42, 153, 200, 45);
getContentPane().add(slider);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SecretMessageGUI theApp = new SecretMessageGUI();
theApp.setSize(new java.awt.Dimension(600,400));
theApp.setVisible(true);
}
}