Results 1 to 9 of 9
  1. #1
    Serialcek is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Need some help with mouse listener

    First post deleted :)
    Last edited by Serialcek; 12-16-2011 at 12:43 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Need some help with mouse listener

    Moved from 'Advanced Java'

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Serialcek is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: Need some help with mouse listener

    So I actually found a solution for my timer that doesn't start more than once. But now is not working smoothly.
    I think that from my code you will understand the problem.

    Java Code:
    	public boolean timerbool = true;
    
    	public void Pressed(java.awt.event.MouseEvent evt) {
    
    		for (int x = 0; x < 11; x++) {
    			for (int y = 0; y < 11; y++) {
    
    				if (grid[x][y] == evt.getSource()) {
    
    					if (grid[x][y].getText().equals(true)) {
    
    						timerbool = false;
    
    					}
    				}
    			}
    		}
    	}
    
    
    public void run_right() {
    
    		final Timer timer = new Timer();
    
    		timer.schedule(new TimerTask() {
    
    			public void run() {
    
    	if (timerbool== false) {
    
    
    					reeset_answer();
    					timer.cancel();
    
    				}
    		}
    
    		}, 0, TIME* 1000); 
    	}
    So the function reset answer just clean up my grid layout and put the "right" answer on some button and some wrong answers to the anther buttons. Before I had the checking of which button is clicked in the function run_right(); but now I don't so is not working smootly. I think that the solution will be to put a while(true) loop somewhere?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need some help with mouse listener

    Perhaps it's me, but I have no idea what you're doing or what your problem is. If you don't get a decent answer soon, consider...

    • supplying us with more code, best would be an SSCCE that shows your problem
    • supplying us with more information regarding the problem. Don't assume that we understand what you're trying to do, what your code does, or anything at all for that matter.
    • posting only well formatted code. Your code formatting is poor, making it even more difficult for us to understand it and be able to help you. If you are asking the volunteers here to put in the effort to try to help you, it's not asking too much for us to expect you to put in a little more effort to make your code presentable and understandable.
    • You appear to be using a java.util.Timer in a Swing application, and I have to wonder why you're not instead using the better suited javax.swing.Timer.
    • Your first bit of code in your first post appears to have infinite recursion where mouseClicked calls mouseClicked which calls mouseClicked which calls.... Has that been solved?


    Best of luck.

  5. #5
    Serialcek is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: Need some help with mouse listener

    Ok thank you for the advice Fubarable. I will try to show better my problem.
    Let's start with a screenshot.

    Need some help with mouse listener-screen-shot-2011-12-15-11.25.27-pm.png.jpg

    So I have this grid layout with some math solutions. A player when the "game" is started see a math problem. And I give him a solutions.
    This solutions are in the balloons. The balloons start to appear at the bottom of the grid and then they go up. The speed of how they are
    going up depends of the timers.
    If the player clicks the right solution a function reset_answer() put a new problem to solve and new answers on the grid which again start from the bottom.
    The problem is that the game is not running smoothly because when I click the right solution I have to wait the function run_right() to check if timerbool== false. I have to wait because of the timer.

    I am asking here what should I do that the game will run smoothly.
    I have no problem with trying to explain you my problem so if there are still people who don't understand I will try to put maybe more code or to reexplain. But I think that the code that I put in my second post (because you can ignore now my first post, I already solved that problem) is enough.

  6. #6
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default Re: Need some help with mouse listener

    There appears to be no need for the timer. Try reorganizing the code without it.

  7. #7
    Serialcek is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: Need some help with mouse listener

    I really don't know how to do that without a timer. It depends on a timer how fast the balloons will go up.

  8. #8
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default Re: Need some help with mouse listener

    I really don't know how to do that without a timer. It depends on a timer how fast the balloons will go up.
    My mistake. I was misled by the fact that in your 11:32PM post the timer task does not update the image.

    One possible performance problem is the double loop in Pressed() to find x and y. The object available at evt.getSource() could have its own x and y values so there would be no need to search.

Similar Threads

  1. Mouse listener
    By chetoos in forum Advanced Java
    Replies: 3
    Last Post: 06-23-2011, 10:23 PM
  2. mouse click listener from textfield
    By ryanmk54 in forum AWT / Swing
    Replies: 2
    Last Post: 05-11-2011, 03:22 AM
  3. Need Help with Mouse Listener!!!!
    By Doggir in forum New To Java
    Replies: 11
    Last Post: 05-06-2011, 12:48 AM
  4. Mouse Listener
    By Quizerno in forum New To Java
    Replies: 8
    Last Post: 03-17-2011, 06:25 AM
  5. Mouse Listener for mouse floating over object?
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-18-2009, 05:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •