trouble with sleeping in an applet

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 2
Reputation: ob1wan is an unknown quantity at this point 
Solved Threads: 0
ob1wan ob1wan is offline Offline
Newbie Poster

trouble with sleeping in an applet

 
0
  #1
Oct 9th, 2007
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.
  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. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 195
Reputation: nschessnerd is an unknown quantity at this point 
Solved Threads: 8
nschessnerd's Avatar
nschessnerd nschessnerd is offline Offline
Junior Poster

Re: trouble with sleeping in an applet

 
0
  #2
Oct 9th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 2
Reputation: ob1wan is an unknown quantity at this point 
Solved Threads: 0
ob1wan ob1wan is offline Offline
Newbie Poster

Re: trouble with sleeping in an applet

 
0
  #3
Oct 9th, 2007
  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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 516
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: trouble with sleeping in an applet

 
0
  #4
Oct 9th, 2007
Try this instead
  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC