Java How to call repaint() on mouse click : <p>So, my code is basically the following:</p>

<pre><code>import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Test extends JPanel{
    boolean circle = false;

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        this.setBackground(Color.WHITE);

        if(circle){
            g.drawRect(150,150,100,100);
        }
    }

    public static void main(String[] args){
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.setSize(400,400);

        Test test = new Test();
        f.add(test);
    }
}
</code></pre>

<p>I want that when I click the mouse, the <code>circle</code> variable changes and it calls <code>repaint()</code>.</p>

<p>How can I do this? Thanks.</p>
