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);
   }
   
 }

I think your problem is your not making the thread do the typing, so look into making a new thread and making it write to the textbox then sleep.. Im not 100% sure how to though.

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(100)+15);          
        }
        output +="\n";
        mainBox.setText(output);
   }
   
   public void sleep(int timer){
   	try {
		Thread.sleep(timer);
	}
	catch (InterruptedException ie){}
   }

The way i thought it should work is it goes into the loop,
adds get next char in string and adds to the output string
then sets the mainBox to the output string
then sleeps
and repeats

I did a simple one in class, we drew 10 circles 1 every 100ms, and the code looks like the same layout as mine

Try this instead

import java.applet.Applet;
 import java.awt.*;
 import java.awt.event.*; 
import java.lang.reflect.InvocationTargetException;
 import javax.swing.*;
 import java.util.*;

 public class Game extends Applet implements ActionListener{
   String textB="textB",output="",input="type here";
   JTextField inputBox;
   JTextArea mainBox;
   
   public void actionPerformed(ActionEvent e) {
    if (textB.equals(e.getActionCommand())) {
        Typewriter type = new Typewriter(inputBox.getText());
        type.start();
        inputBox.setText(null);
    }
   }
   
   class Typewriter extends Thread{
       String text=null;
       Random SEED = new Random();
        Random rand = new Random(SEED.nextInt(999999));
        
       public Typewriter(String text){
           this.text = text;
       }
        public void run() {
            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);
                    
                try {
                    sleep(rand.nextInt(25)+100);          
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }          
                 System.out.println(EventQueue.isDispatchThread());
            }
            output +="\n";
            mainBox.setText(output);            
        }
   }
   
   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);
   }
   
 }

I believe you were getting that behavior because you were sleeping directly in the AWT Event Dispatch thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.