Nina Ametuar Posts: 134
|
Code: | import java.applet.*; import java.awt.*; import java.awt.event.*; public class ReverseString extends Applet { public static final long serialVersionUID = 1L; public String sentence = ""; public String reverse = ""; public char[] letters = new char[20]; public int length; TextField textfield; public void init() { textfield = new TextField(11); textfieldHandler kh = new textfieldHandler(); textfield.addActionListener(kh); setLayout(null); add(textfield); textfield.setSize(textfield.getPreferredSize()); textfield.setLocation(10, 50); } public void paint (Graphics g) { g.setFont (new Font ("SansSerif", Font.BOLD, 12)); g.setColor(Color.black); int i = length-1; while (i >= 0 ){ g.drawChars(letters, i, 1, 100-(i*20), 100); i--; } } public class textfieldHandler implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == textfield) { sentence = textfield.getText(); length = sentence.length(); int i = 0; while (i < length){ letters[i] = sentence.charAt(i); i++; } repaint(); } } } } |
|