i am writing a program that when the mouse is clicked, a circle will be drawn. The below code i\'ve wrote so far.
import java.awt.*;
import javax.swing.*;
i
When you call repaint()
, the component gets painted again from scratch. You're circle is wiped away. You will want to override paintComponent(Graphics)
which is called every time the component is painted.
Change your mouseClick(...)
to:
int x, y;
public void mouseClicked(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
Override paint(...)
:
@Override
public void paint(Graphics g) {
drawCircle(x, y);
}