java drawing a circle when mouse clicked

前端 未结 2 790
不知归路
不知归路 2021-01-18 04:57

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         


        
相关标签:
2条回答
  • 2021-01-18 05:36

    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.

    0 讨论(0)
  • 2021-01-18 05:46

    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);
    }
    
    0 讨论(0)
提交回复
热议问题