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
Code java:
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();
}
}
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)
Re: MouseListener accumulating mouse clicks
Question: Why is your mouse listener inside of your paintComponent method?
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
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:
Code java:
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.
Re: MouseListener accumulating mouse clicks
Fantastic reply, thanks for all your help. It seems so obvious now.
Thanks again M