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 6 of 6

Thread: MouseListener accumulating mouse clicks

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default MouseListener accumulating mouse clicks

    Hi there,

    I am writing a basic GUI that has a circle in it and when you click the circle it cycles through a choice of 4 colours.

    I have written a class that extends JPanel and uses paintComponent to draw the circle, I then use this in another class as an object in a JFrame.

    To change the colour of the circle I have used a MouseListener, and a modulus function, however when you click the first time the MouseListener calls the code inside of it once, but the second time it calls the code twice and the third 3 times and so on.

    Please help as I can't see why it would do this, below is the code for the JPanel class

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
     
    public class CircleClicker extends JPanel{
     
    	public final static int RED = 1, YELLOW = 2, GREEN = 3, BLUE = 0;
    	int colour = 0;
     
    	public void paintComponent(Graphics g){
     
    		super.paintComponent(g);
     
    		addMouseListener(
    			new MouseAdapter(){
    				public void mousePressed(MouseEvent e){
    					colour = (colour + 1);
    					repaint();
    					System.out.println("Click value " + colour);
    				}
    			}
    		);
     
    		if (colour==RED){
    			g.setColor(Color.RED);
    			//g.fillOval(100, 100, 50, 50);
    		}
    		else if (colour==YELLOW){
    			g.setColor(Color.YELLOW);
    			//g.fillOval(100, 100, 50, 50);
    		}
    		else if (colour==GREEN){
    			g.setColor(Color.GREEN);
    			//g.fillOval(100, 100, 50, 50);
    		}
    		else if (colour==BLUE){
    			g.setColor(Color.BLUE);
    			//g.fillOval(100, 100, 50, 50);
    		}
    		else{
    			g.setColor(Color.lightGray);
    		}
    		g.fillOval(0,0,50,50);
    	}
     
    	public Dimension getPreferredSize(){
    		return new Dimension(50, 50);
    	}
     
    	public Dimension getMinimumSize(){
    		return getPreferredSize();
    	}
    }
    Last edited by copeg; February 16th, 2011 at 01:33 PM.


  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: MouseListener accumulating mouse clicks

    For future reference, please use the highlight/code tags (the announcements post at the top of every forum has instructions).

    On to the problem, you are adding a mouse listener every time the paintComponent is called. So every call will add yet another mouse listener which accumulates the number of times the mousePressed function is called. Move the addMouseListener to a more appropriate location (for example the constructor)

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: MouseListener accumulating mouse clicks

    Question: Why is your mouse listener inside of your paintComponent method?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: MouseListener accumulating mouse clicks

    Firstly I am very sorry for not putting my code in the appropriate tags, poor form on my behalf

    In terms of moving the MouseListener outside the paintComponent, I tried this but got the error "illegal start to operation" when I moved the bit of code pertaining to the MouseListener to underneath where I declare the int colour.

    If I am to move it outside the paintComponent method do I need to put it in a method of its own and if so do I need to call that method in my main class that add the JPanel to my JFrame for the whole GUI.

    Many thanks for your help, you have stopped me going insane.

    M

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: MouseListener accumulating mouse clicks

    The most obvious way would be to create a constructor for CircleClicker and put the statement to add the mouse listener in there. I don't know of any way to add a mouse listener by overriding anything.

    To make sure that CircleClicker is still technically a JPanel, you want to call the JPanel constructor as your first statement in your constructor. To do this, use the following statement: super();

    Since your problem isn't directly about constructors and I would have to assume that you understand constructors since you are doing listeners, I'll provide you the code for the constructor since it is really just reorganizing your code:
    public CircleClicker()
    {
    	super();
    	addMouseListener(
                new MouseAdapter(){
                    public void mousePressed(MouseEvent e){
                        colour = (colour + 1);
                        repaint();
                        System.out.println("Click value " + colour);
                    }
                }
            );
    }

    Remember to also remove the mouse listener from your paintComponent method.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #6
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: MouseListener accumulating mouse clicks

    Fantastic reply, thanks for all your help. It seems so obvious now.

    Thanks again M

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. MouseListener
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 18th, 2011, 11:43 AM
  3. [SOLVED] implements MouseListener doesn't work why
    By voltaire in forum AWT / Java Swing
    Replies: 2
    Last Post: May 1st, 2010, 04:30 PM
  4. MouseListener & MouseMotionListener
    By JavaLearner in forum AWT / Java Swing
    Replies: 9
    Last Post: March 26th, 2010, 07:18 AM
  5. Java Loops - accumulating totals
    By iv3java in forum Loops & Control Statements
    Replies: 0
    Last Post: December 14th, 2009, 10:10 PM