import java.awt.*; // Defines basic classes for GUI programming. import java.awt.event.*; // Defines classes for working with events. import javax.swing.*; // Defines the Swing GUI classes. public class decoderApplet extends JApplet implements ActionListener { Display display; protected JTextField textField; protected JTextArea textArea; class Display extends JPanel { int colorNum; Font textFont; Display() { super(new GridBagLayout()); setBackground(Color.black); colorNum = 1; textFont = new Font("Serif",Font.BOLD,36); textField = new JTextField(20); textArea = new JTextArea(5, 20); textArea.setEditable(false); JScrollPane scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); //Add Components to this panel. GridBagConstraints c = new GridBagConstraints(); c.gridwidth = GridBagConstraints.REMAINDER; c.fill = GridBagConstraints.HORIZONTAL; add(textField, c); c.fill = GridBagConstraints.BOTH; c.weightx = 1.0; c.weighty = 1.0; add(scrollPane, c); } void encode() { String text = textField.getText(); String toInsert = ""; for ( int i = 0; i < text.length(); ++i ) { char c = text.charAt( i ); int j = (int) c; toInsert = new String(toInsert.concat(j + "-")); } textArea.append(toInsert + "\n"); textField.selectAll(); textField.setText(""); textArea.setCaretPosition(textArea.getDocument().getLength()); repaint(); } void decode() { String text = textField.getText(); String toInsert = ""; String[] decoded = text.split("-"); for(int i = 0; i < decoded.length; i++) { toInsert = new String(toInsert.concat(new Character((char)(new Integer(decoded[i]).intValue())).toString())); } textArea.append(toInsert + "\n"); textField.selectAll(); textField.setText(""); textArea.setCaretPosition(textArea.getDocument().getLength()); repaint(); } public void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(Color.red); g.setFont(textFont); // Set the font. g.drawString("Hello World!", 25,50); // Draw the message. } } public void init() { display = new Display(); getContentPane().add(display, BorderLayout.CENTER); JPanel buttonBar = new JPanel(); buttonBar.setBackground(Color.gray); JButton bl = new JButton("Encode"); bl.addActionListener(this); buttonBar.add(bl); JButton bm = new JButton("Decode"); // the second button bm.addActionListener(this); buttonBar.add(bm); getContentPane().add(buttonBar, BorderLayout.SOUTH); } // end init() public void actionPerformed(ActionEvent evt) { String command = evt.getActionCommand(); if (command.equals("Encode")) // Set the color. display.encode(); else if (command.equals("Decode")) display.decode(); } // end actionPerformed() } // end class HelloWorldJApplet