0

Hi i am trying to add a mouse listener to my frame to get the position of the mouse clicked and check if it is inside the circle, the problem is that it is not triggering

public class CircleDraw extends Frame implements MouseListener {
static int circles = 0;
private double color;
double mousex = 0;
double mousey = 0;
int score;
public void mouseClicked(MouseEvent evt)
{
 mousex = evt.getX();
 mousey = evt.getY();
}
public void mouseEntered (MouseEvent me) {}
public void mousePressed (MouseEvent me) {}
public void mouseReleased (MouseEvent me) {}
public void mouseExited (MouseEvent me) {}
public void paint(Graphics g) {
 try {
 this.addMouseListener(this);
 while (circles < 20) {
 color = 10*Math.random();
 Shape circle = new Ellipse2D.Double(900*Math.random(),900*Math.random(), 50.0f, 50.0f);
 Graphics2D ga = (Graphics2D)g;
 ga.draw(circle);
 if(color >2)
 ga.setPaint(Color.green);
 else
 ga.setPaint(Color.BLACK);
 ga.fill(circle);
 if(circle.contains(mousex, mousey) && color > 2)
 score ++;
 else
 if(circle.contains(mousex, mousey) && color < 2)
 score--;
 Thread.sleep(1000);
 System.out.println(circles);
 System.out.println(mousex);
 System.out.println(mousey);
 circles ++;
 ga.setPaint(Color.white);
 ga.fill(circle);
 }
 System.exit(0);
 } catch (InterruptedException e) {
 e.printStackTrace();
 } 
 }
 public static void main(String args[]) {
 Frame frame = new CircleDraw();
 frame.addWindowListener(new WindowAdapter(){
 public void windowClosing(WindowEvent we){
 System.exit(0);
 }
 });
 frame.setSize(1000, 1000);
 frame.setVisible(true);
 }}
Erik
3,77323 silver badges27 bronze badges
asked Jun 3, 2011 at 10:34

3 Answers 3

2

It is deadly to add your mouselistener in the paint() method, since this method is called very very often (with each repaint), and so many listeners are added (with each repaint).

You should add the listener to your content-panel and not to the JFrame itself. This will do it. You can do this in the constructor of your class:

public CircleDraw() {
 this.getContentPane().addMouseListener(this);
}

This won't solve your problem completely I think, since you won't get your mouseclick while your paint-method is active. Your code-design (especially your while-loop) does not give any time to other events to fire. So the mouseclick-event will be handled after your 20 loops. You can check this by adding

public void mouseClicked(MouseEvent evt) {
 mousex = evt.getX();
 mousey = evt.getY();
 System.out.println("X: "+mousex+"/ Y: "+mousey);
}

to your code. You have to run your GUI in a different thread (e.g. use SwingUtilities and a Runnable() therefor). I recommend you to get a good book on JAVA development. Or you can start with online-tutorials like this one.

IMHO you should not try to deal with awt, instead use SWING or SWT for GUI-design, since this is much more compfortable.

answered Jun 3, 2011 at 10:38
Sign up to request clarification or add additional context in comments.

1 Comment

you're right the while loop is not allowing the listener to sniff the click.
0

add the listener in the constructor, the paint is called repeatedly

answered Jun 3, 2011 at 10:40

Comments

0

Here are some of the problems I see with that source:

  1. Adds the listener in paint()
  2. Calls wait() within the paint() method.
  3. Calls System.exit() within the paint() method (not strictly a problem, but very unusual).
  4. Is poorly formatted and hard to understand
  5. Calls deprecated methods.
  6. Codes in AWT in the wrong millennium.
answered Jun 3, 2011 at 11:35

1 Comment

yeah you are right but this is my first program in paint. thank you for the tips

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.