944,191 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2384
  • Java RSS
Oct 9th, 2007
0

trouble with sleeping in an applet

Expand Post »
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.
java Syntax (Toggle Plain Text)
  1. import java.applet.Applet;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import java.util.*;
  6.  
  7. public class Game extends Applet implements ActionListener{
  8. String textB="textB",output="",input="type here";
  9. JTextField inputBox;
  10. JTextArea mainBox;
  11. Random SEED = new Random();
  12. Random rand = new Random(SEED.nextInt(999999));
  13.  
  14. public void actionPerformed(ActionEvent e) {
  15. if (textB.equals(e.getActionCommand())) {
  16. typewriter(inputBox.getText());
  17. inputBox.setText(null);
  18. }
  19. }
  20. public void typewriter(String Text){
  21. char adder;
  22. for(int i=0;i<Text.length();i++){
  23. adder=Text.charAt(i);
  24. //JOptionPane.showMessageDialog(null,adder, "Button",JOptionPane.PLAIN_MESSAGE);
  25. output+=adder+"";
  26. mainBox.setText(output);
  27. sleep(rand.nextInt(25)+6);
  28. }
  29. output +="\n";
  30. mainBox.setText(output);
  31. }
  32.  
  33. public void sleep(int timer){
  34. try {
  35. Thread.sleep(timer);
  36. }
  37. catch (InterruptedException ie){}
  38. }
  39.  
  40. public void init() {
  41. inputBox = new JTextField();
  42. mainBox = new JTextArea();
  43. JScrollPane bottomScrollPane = new JScrollPane(mainBox);
  44. mainBox.setEditable(false);
  45. inputBox.setText(input);
  46. //inputBox.select(0,input.length());
  47. inputBox.setActionCommand(textB);
  48. inputBox.addActionListener(this);
  49. setLayout(new BorderLayout());
  50. add(bottomScrollPane,BorderLayout.CENTER);
  51. add(inputBox, BorderLayout.SOUTH);
  52. }
  53.  
  54. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ob1wan is offline Offline
2 posts
since Oct 2007
Oct 9th, 2007
0

Re: trouble with sleeping in an applet

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.
Reputation Points: 10
Solved Threads: 8
Posting Whiz in Training
nschessnerd is offline Offline
216 posts
since Dec 2006
Oct 9th, 2007
0

Re: trouble with sleeping in an applet

java Syntax (Toggle Plain Text)
  1. public void typewriter(String Text){
  2. char adder;
  3. for(int i=0;i<Text.length();i++){
  4. adder=Text.charAt(i);
  5. //JOptionPane.showMessageDialog(null,adder, "Button",JOptionPane.PLAIN_MESSAGE);
  6. output+=adder+"";
  7. mainBox.setText(output);
  8. //sleep(rand.nextInt(100)+15);
  9. }
  10. output +="\n";
  11. mainBox.setText(output);
  12. }
  13.  
  14. public void sleep(int timer){
  15. try {
  16. Thread.sleep(timer);
  17. }
  18. catch (InterruptedException ie){}
  19. }

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ob1wan is offline Offline
2 posts
since Oct 2007
Oct 9th, 2007
0

Re: trouble with sleeping in an applet

Try this instead
java Syntax (Toggle Plain Text)
  1. import java.applet.Applet;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.lang.reflect.InvocationTargetException;
  5. import javax.swing.*;
  6. import java.util.*;
  7.  
  8. public class Game extends Applet implements ActionListener{
  9. String textB="textB",output="",input="type here";
  10. JTextField inputBox;
  11. JTextArea mainBox;
  12.  
  13. public void actionPerformed(ActionEvent e) {
  14. if (textB.equals(e.getActionCommand())) {
  15. Typewriter type = new Typewriter(inputBox.getText());
  16. type.start();
  17. inputBox.setText(null);
  18. }
  19. }
  20.  
  21. class Typewriter extends Thread{
  22. String text=null;
  23. Random SEED = new Random();
  24. Random rand = new Random(SEED.nextInt(999999));
  25.  
  26. public Typewriter(String text){
  27. this.text = text;
  28. }
  29. public void run() {
  30. char adder;
  31. for(int i=0;i<text.length();i++){
  32. adder=text.charAt(i);
  33. //JOptionPane.showMessageDialog(null,adder, "Button",JOptionPane.PLAIN_MESSAGE);
  34. output+=adder+"";
  35.  
  36. mainBox.setText(output);
  37.  
  38. try {
  39. sleep(rand.nextInt(25)+100);
  40. } catch (InterruptedException ex) {
  41. ex.printStackTrace();
  42. }
  43. System.out.println(EventQueue.isDispatchThread());
  44. }
  45. output +="\n";
  46. mainBox.setText(output);
  47. }
  48. }
  49.  
  50. public void init() {
  51. inputBox = new JTextField();
  52. mainBox = new JTextArea();
  53. JScrollPane bottomScrollPane = new JScrollPane(mainBox);
  54. mainBox.setEditable(false);
  55. inputBox.setText(input);
  56. //inputBox.select(0,input.length());
  57. inputBox.setActionCommand(textB);
  58. inputBox.addActionListener(this);
  59. setLayout(new BorderLayout());
  60. add(bottomScrollPane,BorderLayout.CENTER);
  61. add(inputBox, BorderLayout.SOUTH);
  62. }
  63.  
  64. }
I believe you were getting that behavior because you were sleeping directly in the AWT Event Dispatch thread.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: A doubt in SWING event handling
Next Thread in Java Forum Timeline: Need some idea about RR?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC