Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: Printing message to JTextPane with Thread. on: 2013-12-13 15:25:23
I always miss type stuff. I'm from Australia so I spell it "initialise". Again, thanks.
2  Game Development / Newbie & Debugging Questions / Re: Printing message to JTextPane with Thread. on: 2013-12-13 15: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;
   }  
}
3  Game Development / Newbie & Debugging Questions / Re: Printing message to JTextPane with Thread. on: 2013-12-13 13: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
4  Game Development / Newbie & Debugging Questions / Re: Printing message to JTextPane with Thread. on: 2013-12-13 13: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.
5  Game Development / Newbie & Debugging Questions / Printing message to JTextPane with Thread. [Solved] on: 2013-12-13 13: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();
      }

6  Discussions / Miscellaneous Topics / Re: Weird indentation bug on: 2012-12-13 11:10:20
If you reformat the code with shift-ctrl-f does it stay formatted that way?  If so, you clearly have something amiss in your formatting settings, otherwise your quirky brace style is likely confusing things.


When I do that it goes back to the formatted way and looks correct but CTRL + It still doesn't work properly.
7  Discussions / Miscellaneous Topics / Re: Weird indentation bug on: 2012-12-12 14:09:02
Yeah I had to write that long sentence. Sorry but here is a better example and I have not edited any indentation. This is just Eclipse's way of doing it.

CODE WITH SHIFTED RIGHT and CTRL +I
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public void render(int xOffset, int yOffset, Screen screen)
   {
      screen.setOffset(xOffset, yOffset);
      int x1 = xOffset >> 4;
            int x2 = (xOffset + screen.width) >> 4;
            int y1 = yOffset >> 4;
            int y2 = (yOffset + screen.height) >> 4;

            for(int y = y1; y < y2; y++)
            {
               for(int x = x1; x < x2; x++)
               {
                  getTile(x, y).render(x, y, screen);
               }
            }
   }


CODE WITHOUT SHIFTED RIGHT and CTRL +I
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public void render(int xOffset, int yOffset, Screen screen)
   {
      screen.setOffset(xOffset, yOffset);
      int x1 = xOffset;
      int x2 = (xOffset + screen.width);
      int y1 = yOffset;
      int y2 = (yOffset + screen.height);

      for(int y = y1; y < y2; y++)
      {
         for(int x = x1; x < x2; x++)
         {
            getTile(x, y).render(x, y, screen);
         }
      }
   }
8  Discussions / Miscellaneous Topics / Weird indentation bug on: 2012-12-12 13:16:45
I have a weird bug with eclipses indenting. Some reason the shifted right at the start is causing this because as soon as I removed it and did Crtl + A then Ctrl + I it moved the second line in line with first one but not the third because of the shifted right in the second line. Sorry if this is the wrong topic. Anyone has any fix, I would like it Smiley

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public void render(int xOffset, int yOffset, Screen screen)
   {
      int x1 = xOffset >> 4;
            int x2 = (screen.width + 16) >> 4;
            int y1 = yOffset >> 4;
            int y2 = (screen.height + 16) >> 4;
            screen.setOffset(xOffset, yOffset);
            for(int y = y1; y < y2 + y1 ; y++)
            {
               for(int x = x1; x < x2 + x1; x++)
               {
                  getTile(x, y).render(x, y, screen);
               }
            }
            screen.setOffset(xOffset, yOffset);
   }
 
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

CogWheelz (14 views)
2014-08-01 22:53:16

CogWheelz (14 views)
2014-08-01 22:51:43

CopyableCougar4 (15 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (35 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

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

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

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

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

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
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!