java drawing a circle when mouse clicked

前端 未结 2 793
不知归路
不知归路 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条回答
  •  梦毁少年i
    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);
    }
    

提交回复
热议问题