title: Mouse contain within object
body: <p>I'm new to learning Java and for a few days I have searched for a way to click drawn objects, to contain the mouse within a object such as g.drawRect(x, x, x, x)...</p>
  
  <p>I came across the code below, I'm trying to make it work so I can learn what is needed to contain the mouse within a object but I cant seem to make it work.</p>
  
  <p>Got code from " <a href="https://stackoverflow.com/questions/4634107/is-there-any-way-to-add-a-mouselistener-to-a-graphic-object">Is there any way to add a MouseListener to a Graphic object?</a> " </p>
  
  <p>I've tried to make it work according to the tips they gave but no hope :(.</p>
  
  <p>Any help is appreciated.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import java.awt.*;
  import java.awt.event.MouseEvent;
  import java.awt.event.MouseListener;
  import java.awt.geom.Ellipse2D;
  
  import javax.swing.*;
  
  public class Gui3 extends JFrame {
    JFrame frame = new JFrame();
    MyDrawPanel drawpanel = new MyDrawPanel();
  
    public static void main(String[] args) {
      Gui3 gui = new Gui3();
      gui.go();
    }
  
    public void go() {
  
      frame.getContentPane().add(drawpanel);
      // frame.addMouseListener(this);
  
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(300, 300);
      frame.setVisible(true);
  
    }
  
  }
  
  class MyDrawPanel extends JComponent implements MouseListener {
  
    Ellipse2D oval = new Ellipse2D.Double(70, 70, 100, 100);
  
    public void paintComponent(Graphics g) {
  
      Graphics2D g2d = (Graphics2D) g;
  
      g2d.fillOval(70, 70, 100, 100);
  
    }
  
    @
    Override
    public void mouseClicked(MouseEvent e) {
      if ((e.getButton() == 1) &amp;&amp; oval.contains(e.getX(), e.getY())) {
        repaint();
        JOptionPane.showMessageDialog(null, e.getX() + "\n" + e.getY());
      }
    }
  
    @
    Override
    public void mouseEntered(MouseEvent e) {
      // TODO Auto-generated method stub
  
    }
  
    @
    Override
    public void mouseExited(MouseEvent e) {
      // TODO Auto-generated method stub
  
    }
  
    @
    Override
    public void mousePressed(MouseEvent e) {
  
    }
  
    @
    Override
    public void mouseReleased(MouseEvent e) {
      // TODO Auto-generated method stub
  
    }
  
    public boolean contains(int x, int y) {
      return oval.contains(x, y);
    }
  }</code></pre>
  </div>
  </div>
  </p>