title: Java - MouseListener Action Event in paintComponent
body: <p>Here i have a code which draws a rectangle on the mouseClicked position using the paintComponent.I can get the output message but anything related to graphics and .draw() wont work.</p>
  
  <p>Code:</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  
  public final class testclass extends JFrame {
  
      static JPanel p;
      Timer t;
      int x = 1;
      int y = 1;
      int xspeed = 1;
      int yspeed = 1;
  
      public testclass() {
          initComponents();
          this.setBounds(100, 300, 500, 500);
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          t.start();
          this.add(p);
      }
  
      public void initComponents() {
          final ActionListener action = new ActionListener() {
  
              public void actionPerformed(ActionEvent evt) {
                  System.out.println("Hello!");
                  p.repaint();
              }
          };
  
          t = new Timer(50, action);
          p = new JPanel() {
  
              public void paintComponent(Graphics g) {
                  super.paintComponent(g);
                  final Graphics2D gD = (Graphics2D) g;
                  moveBALL();
                  gD.drawOval(x, y, 25, 25);
  
                  p.addMouseListener(new MouseListener() {
  
                      @Override
                      public void mouseReleased(MouseEvent e) {
                          System.out.println("a");
                      }
  
                      @Override
                      public void mousePressed(MouseEvent e) {
                          System.out.println("b");
                      }
  
                      @Override
                      public void mouseExited(MouseEvent e) {
                          System.out.println("c");
                      }
  
                      @Override
                      public void mouseEntered(MouseEvent e) {
                          System.out.println("d");
                      }
  
                      @Override
                      public void mouseClicked(MouseEvent e) {
                          gD.drawRect(e.getX(), e.getY(), 10, 60);
                          gD.setColor(Color.green);
                          System.out.println("clicked");
                      }
                  });
              }
  
              void moveBALL() {
                  x = x + xspeed;
                  y = y + yspeed;
                  if (x &lt; 0) {
                      x = 0;
                      xspeed = -xspeed;
                  } else if (x &gt; p.getWidth() - 20) {
                      x = p.getWidth() - 20;
                      xspeed = -xspeed;
                  }
                  if (y &lt; 0) {
                      y = 0;
                      yspeed = -yspeed;
                  } else if (y &gt; p.getHeight() - 20) {
                      y = p.getHeight() - 20;
                      yspeed = -yspeed;
                  }
              }
          };
      }
  
      public static void main(String args[]) {
          EventQueue.invokeLater(new Runnable() {
              public void run() {
                  new testclass().setVisible(true);
                  p.setBackground(Color.WHITE);
              }
          });
      }
  }
  </code></pre>
  
  <p>What is the proper way to implement a mouseListener() in this program?
  Thanks.</p>