Printing message to JTextPane with Thread. [Solved]

Hello, I’m creating a text based/graphic rpg game and I’m using the follow method to print to my JTextPane. My question is, is this the right way to do it? I feel like something is wrong because I’m creating a new Thread everytime. Thanks :slight_smile:

public void printMessage(final Game game, final String message, final int delay, final Color color)
		{
			Runnable run = new Runnable()
			{
				@Override
				public void run() 
				{
					long currentTime = System.currentTimeMillis();
					boolean sent = false;

					while(!sent)
					{
						if(System.currentTimeMillis() > currentTime + (delay * 1000))
						{
							game.printMessage(message, color);
							sent = true;
						}
					}			
				}
			};
			Thread thread = new Thread(run);
			thread.start();
		}

EDIT: You should create a class that implements runnable that handles each string, while each string could have it’s own thread which appends updates, it’s rediculous to start a new thread every-time you update a string.

Example:


StringManager playerHealth = new StringManager();
playerHealth.append("String here");

Then have the StringManager do whatever it needs to do when the append function is called, similar to what you’re doing, however this would create less threads.

Probably more efficient ways to do it, but thats my two-pennies.

I haven’t really seen a performance drop or anything which is weird.

God forbid I link to another forum, much less one that is for “Runescape” private servers of anything, however this EventManager should give you an idea if you read up on it.

Ignore the warning about it being obsolete and the “Cycle-Based Task manager” being better, as that’s because the Runescape Servers are Cycle Based, anyway, read up, if you’d like.

http://www.rune-server.org/runescape-development/rs2-server/tutorials/86435-accurate-eventmanager-fast-accurate-alternative-unreliable-process-timers.html

EDIT: if you’re looking at this for a reference, make sure to search for “Step 1: The Event Manager class” and read from there

Great. This is perfect. I will post my code after so you can tell me what I did wrong XD

If you want to add this Manager to your application, go ahead, but I wont really be able to help you with it. It’s been a very, very long while since I’ve used it. Just look at my first comment that I had edited. It gives you a standard idea on how to do it. The Event Manager is practically the same thing, just… more complicated.

I would just like to say thanks. This really helped me out. Here is the code I just wrote and it works flawlessly.

Printing Manager

package com.mrcrayfish.textcraft;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

public class PrintingManager implements Runnable
{
	private static PrintingManager printingManager = null;
	private List<StringData> stringsToPrint;
	private Game game;

	private PrintingManager(Game game)
	{
		stringsToPrint = new ArrayList<StringData>();
		this.game = game;
	}

	private Thread thread;

	public static PrintingManager getInstance()
	{
		return printingManager;
	}

	public static void initilise(Game game)
	{
		printingManager = new PrintingManager(game);
		printingManager.thread = new Thread(printingManager);
		printingManager.thread.start();
	}

	@Override
	public synchronized void run() 
	{
		while(true)
		{
			for(StringData data : stringsToPrint)
			{
				long currentTime = System.currentTimeMillis();
				boolean sent = false;

				while(!sent)
				{
					if(System.currentTimeMillis() > currentTime + (data.getDelay() * 1000))
					{
						game.printMessage(data.getMessage(), data.getColour());
						sent = true;
					}
				}
			}

			for(StringData data : stringsToPrint)
			{
				stringsToPrint.remove(data);
			}

			try 
			{
				wait();
			} 
			catch (InterruptedException e) 
			{
				e.printStackTrace();
			}
		}

	}

	public synchronized void printString(String message, Color colour, int delay)
	{
		stringsToPrint.add(new StringData(message, colour, delay));
		notify();
	}

	public void shutdown() 
	{
		this.thread.interrupt();
	}

}

StringData

package com.mrcrayfish.textcraft;

import java.awt.Color;

public class StringData 
{
	private String stringToPrint;
	private Color colourForString;
	private int delayBeforePrint;
	
	public StringData(String message, Color colour, int delay)
	{
		this.stringToPrint = message;
		this.colourForString = colour;
		this.delayBeforePrint = delay;
	}
	
	public String getMessage()
	{
		return stringToPrint;
	}
	
	public Color getColour()
	{
		return colourForString;
	}
	
	public int getDelay()
	{
		return delayBeforePrint;
	}	
}

EDIT: Nevermind.

Congrats!

I always miss type stuff. I’m from Australia so I spell it “initialise”. Again, thanks.