I'm trying to simulate a typewriter effect in an applet. I have a main box and input box which I want to copy the input text letter by letter in to the mainbox. The problem I'm having is when I try to sleep it sleeps the whole time then prints the text. I tested printing to JOptionPane and I didn't have any trouble. Any help would be great.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Game extends Applet implements ActionListener{
String textB="textB",output="",input="type here";
JTextField inputBox;
JTextArea mainBox;
Random SEED = new Random();
Random rand = new Random(SEED.nextInt(999999));
public void actionPerformed(ActionEvent e) {
if (textB.equals(e.getActionCommand())) {
typewriter(inputBox.getText());
inputBox.setText(null);
}
}
public void typewriter(String Text){
char adder;
for(int i=0;i<Text.length();i++){
adder=Text.charAt(i);
//JOptionPane.showMessageDialog(null,adder, "Button",JOptionPane.PLAIN_MESSAGE);
output+=adder+"";
mainBox.setText(output);
sleep(rand.nextInt(25)+6);
}
output +="\n";
mainBox.setText(output);
}
public void sleep(int timer){
try {
Thread.sleep(timer);
}
catch (InterruptedException ie){}
}
public void init() {
inputBox = new JTextField();
mainBox = new JTextArea();
JScrollPane bottomScrollPane = new JScrollPane(mainBox);
mainBox.setEditable(false);
inputBox.setText(input);
//inputBox.select(0,input.length());
inputBox.setActionCommand(textB);
inputBox.addActionListener(this);
setLayout(new BorderLayout());
add(bottomScrollPane,BorderLayout.CENTER);
add(inputBox, BorderLayout.SOUTH);
}
}