Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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  
  Threads and listener  (Read 1682 times)
0 Members and 1 Guest are viewing this topic.
Offline mogwai31

Innocent Bystander





« Posted 2006-05-02 22:59:51 »

Hi

I am devloping a board game in java. In order to allow a user to move, i want to detect mouse clicks on the board(graphics2D stuff).  I have a method called move that gets the squares a user can move to (Rectangle2D.Float).  I add a mouse listener to the board.  I then loop and ask the mouse listener for the Point of the last event to check whther one of the allowed rectangles contains it.  Unfortunately, it never goes in the mouse listener, it just sits in the loop.  I know how to write mouse listeners and I have tested it without the loop.
Is a mouse listener created in a new thread?  It seems like the mouse listener's events are never called, and he code in stuck in the main thread.

Plaese help im so confused.

MY
Offline Kova

Senior Member





« Reply #1 - Posted 2006-05-03 09:27:01 »

mouse listener created in new thread? huh? No.
To see if mouse event gets call simply just print something out (good thing would be x and y pos of event) in console when you enter mouseClicked / mousePressed.
I don't get the loop you're in, is it a game loop? If you're getting x any y of mouse every time you pass through game loop than I can't help you, you didn't wrote anything about it. All I can say why not use mouseClicked or mousePressed?
Offline modelsmith

Senior Newbie




"Never stop three feet short."


« Reply #2 - Posted 2006-05-03 10:26:59 »

Hmm, If you're running the loop from the base invoking thread, then no other threads are likely to get to work.
The main thread usually has priority over the rest.

A better way to run a loop is to create it in a new runnable thread and then periodically 'sleep' every loop iteration.


class MouseInputHandler extends Thread
{
 public void run()
 {
  for (int i=0; i<1000; ++i)
  {
   // < your code here > Check the x and y coords etc.

   System.out.println(""+System.currentTimeMillis()+"> Tick.");

   try
   {
    Thread.sleep(500); // Sleep for 500 milliseconds to allow other threads to run
   }
   catch (InterruptedException e)
   {}
  }
 }
} // end class MouseInputHandler

public class ThreadTest
{
 public static void main(String[] args)
 {
  // Create and add the mouse listener stuff etc.

  Thread l_myThread = new MouseInputHandler();

  // Start the looping thread
  l_myThread.start();
 }
} // end class ThreadTest



Also, events, like AWT events (mouse, keyboard, etc.) are only invoked in the base thread, which your initial code occupies.
If your code works without the loop, then I would definitely suggest moving your loop into a separate thread.

I hope this helps.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #3 - Posted 2006-05-03 11:26:21 »

Hmm, If you're running the loop from the base invoking thread, then no other threads are likely to get to work.
The main thread usually has priority over the rest.
Incorrect.  The event dispatch thread has more priority than the main thread.  Just look at all the Java 4K games.  They are almost all written with a loop in the main thread.

@mogwai31:
You will have to post source to get more help.

Offline Anon666

Junior Member




aka Abuse/AbU5e/TehJumpingJawa


« Reply #4 - Posted 2006-05-03 14:14:32 »

Quote
Also, events, like AWT events (mouse, keyboard, etc.) are only invoked in the base thread, which your initial code occupies.
If your code works without the loop, then I would definitely suggest moving your loop into a separate thread.

Also incorrect.

The Thread that initially invokes an applications main(...) method is in no-way related to the event dispatch Thread.

(infact the event dispatch Thread doesn't even exist until the AWT subsystem is initialized - which can occur at various points; but usually upon constructor of a Window descendent.)
Offline modelsmith

Senior Newbie




"Never stop three feet short."


« Reply #5 - Posted 2006-05-04 08:24:42 »

 Sorry about that. I stand informed and corrected.

However, I think that if one has an infinite loop anywhere (even if one is doing something useful in there), that all other threads, including the EventQueue, will have difficulty getting a chance to work.

In any case, would my code above fix the problem?
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2006-05-04 08:36:24 »

Also incorrect Smiley

As long as all threads ahve the same priority, they have equal chance to get cpu-time.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline modelsmith

Senior Newbie




"Never stop three feet short."


« Reply #7 - Posted 2006-05-04 09:34:19 »

Aw darn'it.

I have much still to learn. Tongue
Offline modelsmith

Senior Newbie




"Never stop three feet short."


« Reply #8 - Posted 2006-05-04 13:11:16 »

Hmm. Searching the other posts, I found this.

Essentially, I think it means that if you start from a listener event call and have an infinite loop where you don't return, only then do you tie up the EventQueue and no more events can be called until the method with the loop returns.

Does the GameWindow constructor start the game loop? If so you might be blocking the AWT event thread. Try constructing your GameWindow in a different thread.

1  
2  
3  
4  
5  
6  
Thread t = new Thread() {
    public void run() {
         new GameWindow(); // or whatever starts your game loop
    }
};
t.start();


Kev

This would explain why I thought that a loop would tie up the events, just for the wrong reasons.

Sorry if this isn't right either. (me a n00b)
Offline Mr_Light

Senior Member




shiny.


« Reply #9 - Posted 2006-05-05 22:56:55 »

Also incorrect Smiley

As long as all threads ahve the same priority, they have equal chance to get cpu-time.

no guarenties though, depence on the underlaying system. unless a thread yields... well, these days all systems today seem to have that behavoir though  Wink

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
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.

TehJavaDev (15 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (34 views)
2014-08-16 06:45:27

Tekkerue (32 views)
2014-08-16 06:22:17

Tekkerue (20 views)
2014-08-16 06:20:21

Tekkerue (30 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (43 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
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!