1

I'm simply trying to I’m simply trying to draw a circle in the same location as mouse click, but I can’t get it to work. Here is the code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LineApp
{
 public static void main ( String args[])
 {
 MainWindow mainWdw = new MainWindow();
 }
}
class MainWindow extends Frame
{
 private Point recPoint;
 private boolean mouseClick;
 MainWindow()
 {
 mouseClick = false;
 setBackground( Color.BLACK );
 setSize( 400,300 );
 setLocation( new Point( 300,300));
 addComponentListener(new ComponentCatcher());
 addWindowListener(new WindowCatcher());
 addMouseListener(new MouseCatcher());
 setVisible( true );
 }
 public void paint( Graphics gc )
 {
 gc.setColor(Color.ORANGE);
 if(mouseClick)
 {
 gc.setColor(Color.ORANGE);
 gc.fillOval(recPoint.x, recPoint.y, 30,30);
 }
 }
 class WindowCatcher extends WindowAdapter
 {
 public void windowClosing( WindowEvent evt)
 {
 evt.getWindow().dispose();
 System.exit (0);
 }
 }
 class ComponentCatcher extends ComponentAdapter
 {
 public void componentResized (ComponentEvent evt)
 {
 repaint();
 }
 }
 class MouseCatcher extends MouseAdapter
 {
 public void mousedPressed ( MouseEvent evt)
 {
 Point mousePt = new Point();
 mousePt = evt.getPoint();
 recPoint = new Point(mousePt.x,mousePt.y);
 mouseClick = true;
 repaint();
 }
}

}

asked Feb 1, 2010 at 14:39
1
  • What specifically is not working about your code? Is the circle appearing at all? Commented Feb 1, 2010 at 14:48

1 Answer 1

5

The method in MouseAdapter is called mousePressed, not mousedPressed.

The @Override-annotation can help you to avoid errors like that. If you would have used

 @Override
 public void mousedPressed ( MouseEvent evt)
 {
 ...

your code would not have compiled.

answered Feb 1, 2010 at 14:48
Sign up to request clarification or add additional context in comments.

Comments

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.