title: Java MouseEvent position is inaccurate
body: <p>I've got a problem in Java using a "canvas" class I created, which is an extended <code>JPanel</code>, to draw an animated ring chart. This chart is using a <code>MouseListener</code> to fetch click events.</p>
  
  <p>The problem is that the mouse position does not seem to be accurate, meaning it does not seem to be relative to the "canvas" but instead relative to the window (in the left, upper corner I got about 30px for y coord).</p>
  
  <p>This is my code:</p>
  
  <p>I created a class, that extends JPanel and does have a BufferedImage as member.</p>
  
  <pre><code>public class Canvas extends JPanel {
  
      public BufferedImage buf;
      private RingChart _parent;
  
      public Canvas(int width, int height, RingChart parent){
          buf = new BufferedImage(width, height, 1);
      ...
  </code></pre>
  
  <p>In the paint component method I just draw the buffered image, so I am able to paint on the canvas from 'outside' by painting on the buffered image, which is public.</p>
  
  <pre><code>public void paintComponent(Graphics g) {
          super.paintComponent(g);
  
          Graphics2D g2 = (Graphics2D)g; 
          g2.drawImage(buf, null, 0, 0); 
  
      }
  </code></pre>
  
  <p>Now there's a class RingChart which contains a "canvas":</p>
  
  <pre><code>public class RingChart extends JFrame{
  
      public Canvas c;
      ...
  </code></pre>
  
  <p>And I create a Graphics2D from the bufferedImage in the canvas class. This g2d is used for painting:</p>
  
  <pre><code>public RingChart(){
      c = new Canvas(1500,980,this);
      add(c);
      setSize(1500, 1000);
      setTitle("Hans");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      g2d = (Graphics2D)c.buf.createGraphics();
      ...
  </code></pre>
  
  <p>What I now was trying to achieve, was a mouse listener that listened to mouse events happening on the canvas. So when the user clicks on the canvas I could retrieve the position he clicked on, upon the canvas, through the event variable.</p>
  
  <p>So I created a mouse listener:</p>
  
  <pre><code>class MouseHandler implements MouseListener {
  
      @Override
      public void mouseClicked(MouseEvent e){
          RingChart r = ((Canvas)e.getSource()).getParent();
          r.mouseClick(e);
      }
      ...
  </code></pre>
  
  <p>...and added this mouse listener to the canvas of the RingChart class (myChart is an instance of RingChart and c is the canvas it contains):</p>
  
  <pre><code>        ...
          MouseHandler mouse = new MouseHandler();
          myChart.c.addMouseListener(mouse);
          ...
  </code></pre>
  
  <p>But as I mentioned above, the mouse position, that's returned when the click event is called, does not seem to be accurate. I think the mistake must be somehow in the way I created that mouseListener or maybe assigned it to the wrong element or something like that. But I've tried quite a couple of things and it didn't change. Can maybe someone tell me, what I've done wrong?</p>
  
  <p>UPDATE:</p>
  
  <p>The code of the function "mouseClick" that is a member of RingChart and is called in the mouse listener:</p>
  
  <pre><code>public void mouseClick(MouseEvent evt){
      //evt = SwingUtilities.convertMouseEvent(this, evt, c);
      if(evt.getButton() == MouseEvent.BUTTON1 &amp;&amp; animation == null){
          for(Element e : elements){
              if(e.getShape() != null &amp;&amp; e.getShape().contains(evt.getPoint())){
                  //do some stuff
              }
          }
      }
  }
  </code></pre>
  
  <p>Again, the hierarchy of my classes:
  RingChart --contains a--> Canvas --got a--> MouseListener.
  The shapes in this function are shapes that have been painted on the canvas c. Now I want to check, if the user has clicked on one of them. So as I thought, the shapes should be in canvas-coordinates and the event position should be in canvas-coordinates and everything should fit together. But it doesn't.
  Now user MadProgrammer told me, to use the ConvertMouseEvent function. But I currently don't see which exact way I should use this sensibly.</p>
  
  <p>UPDATE:</p>
  
  <p>I found a solution: All I had to do is adding the canvas not directly to the <code>JFrame</code> but to the <code>ContentPane</code> of the <code>JFrame</code> instead:</p>
  
  <p>So instead:</p>
  
  <pre><code>public RingChart(){
      c = new Canvas(1500,980,this);
      add(c);
      ...
  </code></pre>
  
  <p>I do:</p>
  
  <pre><code>public RingChart(){
      c = new Canvas(1500,980,this);
      getContentPane().add(c);
      ...
  </code></pre>
  
  <p>Then I give the <code>MouseListener</code> to the <code>ContentPane</code>.</p>
  
  <pre><code>getContentPane().addMouseListener(new MouseHandler());
  getContentPane().addMouseMotionListener(new MouseMoveHandler());
  </code></pre>
  
  <p>I don't know, if this is an elegant solution, but it works.</p>