Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Printing message to JTextPane with Thread. [Solved]  (Read 459 times)
0 Members and 1 Guest are viewing this topic.
Offline mr_crayfish

Senior Newbie


Medals: 2



« Posted 2013-12-13 12:20:07 »

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 Smiley

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
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();
      }

Offline CTucker1327
« Reply #1 - Posted 2013-12-13 12:27:05 »

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:

1  
2  
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.
Offline mr_crayfish

Senior Newbie


Medals: 2



« Reply #2 - Posted 2013-12-13 12:32:17 »

Whoah. Your computer hasn't exploded yet?

EDIT: oops, realized what you were trying to do, let me rewrite that, lol.
I haven't really seen a performance drop or anything which is weird.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CTucker1327
« Reply #3 - Posted 2013-12-13 12:35:26 »

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
Offline mr_crayfish

Senior Newbie


Medals: 2



« Reply #4 - Posted 2013-12-13 12:50:56 »

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
Offline CTucker1327
« Reply #5 - Posted 2013-12-13 12:59:40 »

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.
Offline mr_crayfish

Senior Newbie


Medals: 2



« Reply #6 - Posted 2013-12-13 14:21:43 »

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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
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;
   }  
}
Offline CTucker1327
« Reply #7 - Posted 2013-12-13 14:23:28 »

EDIT: Nevermind.

Congrats!
Offline mr_crayfish

Senior Newbie


Medals: 2



« Reply #8 - Posted 2013-12-13 14:25:23 »

I always miss type stuff. I'm from Australia so I spell it "initialise". Again, thanks.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (21 views)
2014-09-12 09:08:26

Norakomi (55 views)
2014-09-10 13:57:51

TehJavaDev (66 views)
2014-09-10 06:39:09

Tekkerue (33 views)
2014-09-09 02:24:56

mitcheeb (54 views)
2014-09-08 06:06:29

BurntPizza (38 views)
2014-09-07 01:13:42

Longarmx (24 views)
2014-09-07 01:12:14

Longarmx (30 views)
2014-09-07 01:11:22

Longarmx (28 views)
2014-09-07 01:10:19

mitcheeb (37 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!