Skip to main content
gamedev.net
🔒 Locked

[java] Threads

Started by kino Apr 4, 2005 at 3:22 PM 7 replies 1.1k views
Original Post
kino
kino
Hi guys! Is it possible for a thread to create another thread? Are there any problem at all?
Long live Rock!!
arm
arm
Basically, yes, since your program begins life with a single thread which can spawn new threads and so on....
kino
kino
This is in fact my problem;

I create a JFrame where I just added a yellow label to the content pane, to work as a game presentation screen…
There’s also a JMenu too, where the user must click “Start” (a JmenuItem) to play the game…
That’s the point: selecting Start in the Menu will make this:

- Create a new JPanel object
- Create an instance of the GameArea class
- Add the gameArea object to the JPanel
- Set the JPanel as the content pane of the JFrame

It’s important to say that the GameArea extends JComponent and implements the Runnable interface, so in some moment it would be a good idea to create a Thread, and make it run…My doubt is if this is possible?
Help me please!!

Thanks for all!!
Long live Rock!!
snufer
snufer
Well I get your problem but why in hell are you tring to Thread this?
Is it a RTS, RPG or FPS game? But if you like Threads you can do this. But I
don't know what for... Can you tell me?
I might have misunderstood the context.



Last Attacker
Last Attacker
You don't need to give GameArea a Runnable Interface.
Just create a thread that affects GameArea.
Its not a good idea to make a main class also a thread or whatever.

Just create a class lets ,say GameThread, like this:

public class GameThread extends Thread{   private Component mComp;      /* Pass GameArea through here - comp */   public GameThread(Component comp)   {      if(comp == null)         throw new NullPointerException();            mComp = comp;   }      public void run()   {      /* Use GameArea */   }}


Hope this helps.
"Take delight in the Lord and He will give you your heart's desires" - Psalm 37:4My Blog
CaptainJester
CaptainJester
Quote:
Original post by Last Attacker
You don't need to give GameArea a Runnable Interface.
Just create a thread that affects GameArea.
Its not a good idea to make a main class also a thread or whatever.


Why not? I have never had a problem doing that. If you do it in a separate class, then you need to expose a lot more functionality than you might want to.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
kino
kino
Hello you people!!

Quote:
Is it a RTS, RPG or FPS game?


Hey, hold your horses man!! It’s just a 2D sprites based game, a plagiarism of the Atari’s Tennis game, I don’t remember the real name, it’s a classical, very old…

Quote:
…why in hell are you trying to Thread this?


Although it’s a simple game, I think it’s nice and necessary to create a thread to control de frps.
My paint method, with an off screen implementation is in this class I mentioned…
But, as Last Attacker says:

Quote:
Just create a thread that affects GameArea.


I got it! I create a thread that invokes the paint method, calling repaint() ,
20 times per second, so 20 frps!

Quote:
I might have misunderstood the context



To be honest I think that I made a confusion to explain my problem, well I’m not a fluent English speaker, and neither writer!!
But some problems were solved and I thank you all!!
I restart my work and now I have to look for the error in the old version…
When I find it, I’ll post here, OK?

Thanks everybody!! See ya!!
Long live Rock!!
Leadorn
Leadorn
just a note, i think java uses microthreads (fibers) and fibers arent like other threads like windowsthreads o pthreads.

http://java.sun.com/docs/books/tutorial/essential/threads/priority.html

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.