This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads and listeners

 
mogwai mogwai
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mr. Gremlin,

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Repeated names of furry 1980's movie characters are not sufficient. You can change your display name here. Thanks for your immediate attention.

Now, as to your question: the mouse listener is going to be invoked on the AWT event thread, while you've presumably got a separate "main" thread that is doing other things. Communication among threads can be tricky when you're just learning. Your explanations are a bit hard to follow, so why don't you illustrate for us with some code?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic