I managed to create a typewriter class that does what I want, for the most part. It will output the string given to it one character at a time, pausing between them, as if they were printed, pausing a little longer after periods. The problem that I am facing right now is that when I use this class, it only works once. When I call it twice (or more), he tries to run them at the same time. This causes serious problems. Therefore, I need the first instance of this class to start, and each of them should wait for it to “turn” before starting. The following is an example of the desired result and current.
import objectdraw.*; // Where active object comes from. import javax.swing.JTextArea; public class Typewriter extends ActiveObject { private JTextArea out; private String in; public Typewriter(String s, JTextArea output) { in = s; out = output; start(); } public void run() { synchronized(out) { for(int i=0; i<in.length(); i++) { out.append(in.substring(i,i+1)); if(in.charAt(i) == '.') { pause(30); } else { pause(200); } } } } }
Current:
CODE: new typewriter ("\ nHello", exit); new typewriter ("\ nWorld", exit);
CURRENT EXIT
HW ol elr ldo
DESIRED EXIT
Hello World
Obviously, I left most of the code from the Typewriter class. If it is really necessary, I can publish it. Javadocs for an ActiveObject can be found here . This is how the threads taught me, and I'm afraid this might be a problem.
EDIT:
In response to below, I added a synchronized line (out), but when I try to run the code, I get a nullpointer exception.
Exception in thread "main" java.lang.NullPointerException at objectdraw.ActiveObject.<init>(ActiveObject.java:239) at com.caldwellysr.TBA.Typewriter.<init>(Typewriter.java:11) at com.caldwellysr.TBA.Client.initGame(Client.java:78) at com.caldwellysr.TBA.Client.<init>(Client.java:66) at com.caldwellysr.TBA.Client.main(Client.java:24)
Writing line 11 is the heading for the constructor. Client line 78 is where I invoke a new typewriter ("Testing," output); where the output is JTextArea Client line 66 is a call to initGame (), in which there is a typewriter Client line 24 is a JFrame constructor.