Forgotten your password?

Forum Index > Java > Reverse String
Author Message
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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();
        }
      }
  }

}
Forum Index > Java > Reverse String