TextArea 12 - Me - 0

Okay. So after getting the delayed text / thread thing worked out I am presented with another on going problem. How in the world do I print this text to a text area that word wraps and scrolls. Now after all the reading and googleing I have set up text areas over and over again to not only not have them appear, but also not have my text show in them.

Any Ideas on how this can and should be done. Please let me know if you would like my code as reference.

Thanx.

Here is an altered version of the other example I posted. Hope this helps!


import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JTextArea;

public class TypeWriter extends JFrame implements Runnable
{
	private final String message = "This is your mission, should you choose to accept it:\nFind the Princess and bring her back to the castle where she will be safe from the evil Oompa-Loompas.  Then kill as many of those little bastards as you can!";
	private JTextArea currMessage;
	private int currLetter = 0;
	private Thread thread;
	private long time = 0;
	
	public static void main(String[] args)
	{
		new TypeWriter();
	}

	public TypeWriter()
	{
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent we)
			{
				stop();
				System.exit(0);
			}
		});
		setLayout(new FlowLayout());
		currMessage = new JTextArea(5, 35);
		currMessage.setLineWrap(true);
		currMessage.setWrapStyleWord(true);
		currMessage.setCaretColor(Color.WHITE);
		add(currMessage);
		setSize(400, 120);
		setTitle("Evil Oompa-Loompas!");
		setVisible(true);
		start();
	}
	
	public void run()
    {
        Thread me = Thread.currentThread();
        while (thread == me)
        {
        	if(time == 0 || System.currentTimeMillis() >= time)
        	{
        		if(currLetter <= message.length())
        		{
        			currMessage.setText(message.substring(0, currLetter));
        			currLetter++;
        			repaint();
        			time = System.currentTimeMillis() + 75;
        		}
        		else
        		{
        			break;
        		}
        	}
        	else
        	{
        		try
        		{
        			Thread.sleep(50);
        		}
        		catch(InterruptedException ie)
        		{}
        	}
        }
        thread = null;
    }
	
	public void start()
    {
       thread = new Thread(this);
       thread.setPriority(Thread.MIN_PRIORITY);
       thread.start();       
    }

    public void stop()
    {
       thread = null;
    }
}

Are you using awt or swing??
In swing, you can append text to the document of a TextPane (don’t remember very well the name).
In awt I should see the API to give help…
If I find something, i’ll post it here!
Rafael.-

EDIT: Seems like someone solved it… !

So I basically get rid of the Paint method entirely? Hmmmmm I am using AWT. Ill see what I get done with this one… hopefully it’ll give me a 1w - 12L record :slight_smile:

If you are using a TextArea you won’t need to override the paint(). That’s only if you want to draw with a Graphics object or otherwise alter the default repaint behavior.

So ran in to a couple of issues trying the above code

First I got an out of bounds error in the while loop. go figure…

second… Im trying to put this all into an applet. But the screen blinks when it repaints which is anoying. Can I extend Applet for my class and utilize the JFrame to input the interface inside of the JFrame to reduce the flashyness of repaint();??

Weird, it ran fine for me! As far as a JFrame vs. a JApplet, the repaint speed shouldn’t be any different. Did it run ok (other than the exception) as a JFrame?

Switched my logic to be


for ( int i = 0; i < ConstantsT.message.length(); i++ ) {

try {
Thread.sleep(100); //pause 1 second
   } catch (InterruptedException e) {
    }
			
screenMessage.setText(ConstantsT.message.substring(0, i ));
			
}

This fixed the error… what im not liking is

Class extends Applet <–ideal cause my game runs in an applet

  • The text as its is typing flashes and starts to blink faster as the text writes itself out.

Class extends JFrame <–nifty but no sure how to integrate with my applet game

  • Doesn’t flash but pops up in a new window after running. which leads me to believe I can’t use this for the applet. Which would also mean I may have to redesign my entire game to allow this text to work correctly.

Those are the two biggies at the moment with this class. Any thoughts?

You should be able to add the relevant stuff from the constructor to your init() method


currMessage = new JTextArea(5, 35);
currMessage.setLineWrap(true);
currMessage.setWrapStyleWord(true);
currMessage.setCaretColor(Color.WHITE);

and then add the stuff in the run() method to to your render() method.


if(time == 0 || System.currentTimeMillis() >= time)
 {
        if(currLetter <= message.length())
        {
                currMessage.setText(message.substring(0, currLetter));
                currLetter++;
                time = System.currentTimeMillis() + 75;
        }
        else
        {
                break;
        }
}

You’ll also have to decare the variables, but that should work! I just did it that way to have a fully functional (well mostly functional :-[ ) example.

But as it types the text out… does the system screen flash… Might be just because im using Eclipse not sure?

Also in my init() method… can i make a JFrame to put my JTextArea in? If so how would i do this?

Yeah, if you like you can do a


JFrame textFrame = new JFrame();

then set the attributes and add the JTextArea and set it to visible when you want someone to talk and then not visible when they are done.

cool ill try that and see what it does. Thanx