Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Mouse Listeners

  1. #1
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Mouse Listeners

    I have a program, which is of bouncing balls running through threads.

    My goal is to be able to pause each thread by clicking on the ball as it moves on screen, and resume when clicked again.

    I create new threads by using new Thread on same object each time, but i do pass them into an Array List.

    How could i go about checking if the user click at coordinates X/Y landed on a SPECIFIC thread, and then be able to reference that thread?

    Obviously i can't expect big answers here, but if you have expertise in such an area, could you give me a recommended tutorial which would cover what I'm after.

    I'm familiar with the basics of a Mouse Listener, but not at detecting what thread was clicked on, or if mouse was clicked on any thread and so forth.

    Thanks in advance.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Mouse Listeners

    You will want some to retrieve some sort of Shape that represents your objects to test the clicking.
    in Pseudo-code
    public void mousePressed(MouseEvent e){
        Shape shape = getShapeSomehow();
        if ( shape.contains(e.getPoint() ){
            //mouse pressed occured in Shape
        }
    }
    How you retrieve the shape is dependent upon how it is represented graphically. Based upon your other posts I presume your balls are still JPanel's, so you can try calling the getBounds function to retrieve the rectangle associated with that object.

  3. The Following User Says Thank You to copeg For This Useful Post:

    newbie (November 29th, 2010)

  4. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Mouse Listeners

    I believe you cannot click on a "thread". You to determine out how to detect click events on the various bouncing ball elements - whatever they may be, use that to change some state variable, and have each thread tracking the state variables. It would be very helpful if you described what you are using to draw the balls with separate threads.

    One way to accomplish your goals is to have an array of boolean values...

    class BallTracker {
    public boolean ballIsPaused[5];
    }

    create one instance of this:

    BallTracker myBallTracker = new BallTracker();

    Pass myBallTracker to your code that is running in the threads, so that the threads can check the status, and if ballIsPaused[ballNum] becomes true, then do whatever you need to in the code pause or end the thread.

    So the ball bouncing code would look something like this:

    if(ballTracker.ballIsPaused[ballNum])
    PauseOrKillThread(); //ball is now paused, so do something to wait for unpausing, or kill thread, or something similiar
    UpdateAnimation();
    Sleep(15); //sleep for 15 ms, I don't know what the real function for this is

    Whatever you click detect code does would look something like this:

    void OnClick(int ballNum) {
    ballTracker.ballIsPaused[ballNum] = true;
    }

    That is a basic idea of what you need to do - use one common object that each thread as manipulating to change and react to the state.

    There are probably many ways to do this, but hopefully this gives you something to start with.

  5. The Following User Says Thank You to TheShagg For This Useful Post:

    newbie (November 29th, 2010)

Similar Threads

  1. How to get Mouse Position even if it is not within our application?
    By Freaky Chris in forum Java Programming Tutorials
    Replies: 2
    Last Post: January 4th, 2012, 10:57 AM
  2. Action Listeners and Key Listeners Help
    By xctive in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 18th, 2010, 09:27 AM
  3. [SOLVED] mouse getX and getY are off?
    By Brt93yoda in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 6th, 2010, 12:49 AM
  4. Listeners, CopyOnWriteArrayList and removals
    By Richard in forum Collections and Generics
    Replies: 2
    Last Post: August 5th, 2010, 02:12 PM
  5. Menu Event Listeners
    By Gondee in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 16th, 2010, 03:08 PM