| | |
trouble with sleeping in an applet
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2007
Posts: 2
Reputation:
Solved Threads: 0
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)
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); } }
•
•
Join Date: Oct 2007
Posts: 2
Reputation:
Solved Threads: 0
java Syntax (Toggle Plain Text)
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 I believe you were getting that behavior because you were sleeping directly in the AWT Event Dispatch thread.
java Syntax (Toggle Plain Text)
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); } }
![]() |
Similar Threads
- Applet Communication problem (Java)
- Fecora Core 6/ c ore 4 installation crashes: network manager applet problem (Getting Started and Choosing a Distro)
- Help getting this applet to work! (Java)
- Applet needs to download files (Java)
- Uninstalling Quicktime error (Windows NT / 2000 / XP)
- Trouble opening certain files... (Windows 95 / 98 / Me)
- Do I have to sign my applet to run it with fullscreen? (Java)
- help!! My applet can't run... Urgent!! (Java)
- Applet or Application? (Java)
Other Threads in the Java Forum
- Previous Thread: A doubt in SWING event handling
- Next Thread: Need some idea about RR?
| Thread Tools | Search this Thread |
6 actuate android api applet application array arrays automation balls binary bluetooth bold business c++ chat class classes client code codesnippet collections component coordinates database defaultmethod doctype dragging ebook eclipse educational error event exception file fractal froglogic game givemetehcodez graphics gui hql html ide ideas image ingres input integer internet intersect invokingapacheantprogrammatically j2me java javaexcel javaprojects jni jpanel jtextarea julia linux list loop looping map method methods mobile mysql netbeans newbie numbers oracle parameter php print problem program programming project recursion recursive scanner screen sell server set size sms sort sql string sun swing swt threads time tree websites windows






