// // CipherApp.java // // Created by Edward on 2/9/06 // Last revision: 2/16/06 // import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; public class CipherApp extends JApplet implements ActionListener { // initialize variables String lineIn = ""; String lineOut = ""; Font myFont = new Font("monospaced",0,12); int x; int[] y; int inputLength; JTextField typed; JButton rot; // heart of the program, the rest is just io and a loop public static int rotate (int letter, int shift) { int foo = ((letter >= 'a') && (letter <= 'z') ? ((letter - 'a' + shift) % 26 + 'a') : ((letter >= 'A') && (letter <= 'Z') ? ((letter - 'A' + shift) % 26 + 'A') : letter)); return foo; } public void setUpGUI() { // set the default look and feel String laf = UIManager.getSystemLookAndFeelClassName(); try { UIManager.setLookAndFeel(laf); } catch (UnsupportedLookAndFeelException exc) { System.err.println ("Warning: UnsupportedLookAndFeel: " + laf); } catch (Exception exc) { System.err.println ("Error loading " + laf + ": " + exc); } // set up coordinates for the output x = 5; y = new int[27]; for (int i = 0; i < 27; i++) { y[i] = 14*(i+1); } // add text box and button typed = new JTextField("Type something to cipher here", 20); rot = new JButton("Rotate"); rot.addActionListener(this); JPanel pane = new JPanel(); pane.add(typed); pane.add(rot); setContentPane(pane); //Makes buttons actually show up } public void actionPerformed(ActionEvent e) { Object src = e.getSource(); if (src == rot) { repaint(); } } public void init() { setUpGUI(); } public void paint (Graphics g) { super.paint(g); g.setFont(myFont); g.setColor(Color.black); lineIn = typed.getText(); inputLength = lineIn.length(); // and now for the loop for (int j = 1 ; j < 26 ; j++) { lineOut = (j < 10 ? " "+j+" " : j+" "); for (int i = 0 ; i < inputLength ; i++) { lineOut += (char)rotate(lineIn.charAt(i),j); } g.drawString(lineOut, x, y[j+1]); } } }