title: Moving image with mouse Java
body: <p>Okay I am fairly new to Java and am having problems on a project that was assigned to me. I am having trouble moving my CTetrimino object across the JFrame
  Here is my CTetrimino class.</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.Image;
  
  public class CTetriMino {
  
  public CTetriMino(int type, int x, int y, int w, int h, Color c) 
  {
      CMino m = new CMino();
      d = m.getDiameter();
      Type=type;
      X=x;
      Y=y;
      Width=w;
      Height=h;
      FillColor=c;
  }
  
  public CTetriMino(CTetriMino src) { // copy constructor
      Type=src.Type;
      X=src.X;
      Y=src.Y;
      Width=src.Width;
      Height=src.Height;
      FillColor=src.FillColor;
  }
  
  public int getX() { return X; }
  public void setX(int x) { X=x; }
  public int getY() { return Y; }
  public void setY(int y) { Y=y; }
  
  public void draw(Graphics g) {
      g.setColor(FillColor);
      switch (Type) {
      case 0:
          //g.fillOval(X, Y, Width, Height);
          //break;
          g.fillOval(X, Y, d, d);
          g.fillOval(X+d,Y, d, d);
          g.fillOval(X+d,Y-d, d, d);
          g.fillOval(X+d+d,Y-d, d, d);
          break;
      case 1:
          g.fillRect(X, Y, Width, Height);
          //g.fillOval(X, Y, d, d);
          //g.fillOval(X+d,Y, d, d);
          //g.fillOval(X+d,Y+d, d, d);
          //g.fillOval(X+d+d,Y+d, d, d);
          break;
      case 2:
          g.fillOval(X, Y, d, d);
          g.fillOval(X+d,Y, d, d);
          g.fillOval(X,Y-d, d, d);
          g.fillOval(X+d,Y-d, d, d);
          break;
      case 3:
          g.fillOval(X, Y, d, d);
          g.fillOval(X,Y+d, d, d);
          g.fillOval(X,Y+d+d, d, d);
          g.fillOval(X,Y-d, d, d);
          break;
      case 4:
          g.fillOval(X, Y, d, d);
          g.fillOval(X,Y+d, d, d);
          g.fillOval(X,Y-d, d, d);
          g.fillOval(X+d,Y-d, d, d);
          break;
      case 5:
          g.fillOval(X, Y, d, d);
          g.fillOval(X,Y+d, d, d);
          g.fillOval(X,Y-d, d, d);
          g.fillOval(X-d,Y-d, d, d);
          break;
      case 6:
          g.fillOval(X, Y, d, d);
          g.fillOval(X+d,Y, d, d);
          g.fillOval(X,Y-d, d, d);
          g.fillOval(X-d,Y, d, d);
          break;
          }
      }
  
  public boolean containPoint(int x, int y) {
          switch (Type) {
          case 0:
              {
                  double a=Width/2.0;
                  double b=Height/2.0;
                  double xc=X+a;
                  double yc=Y+b;
                  System.out.println(a);
                  System.out.println(b);
                  System.out.println(xc);
                  System.out.println(yc);
                  System.out.println(((x-xc)*(x-xc)/(a*a)+(y-yc)*(y-yc)/(b*b)&lt;=1.0));
                  return ((x-xc)*(x-xc)/(a*a)+(y-yc)*(y-yc)/(b*b)&lt;=1.0);
  
              }
          case 1:
              return (x&gt;=X &amp;&amp; y&gt;=Y &amp;&amp; x&lt;X+Width &amp;&amp; y&lt;Y+Height);
  
          }
  
          return false;
      }
  
  
  
  
  
  private int Type;   
  private int X;
  private int Y;
  private int Width;
  private int Height;
  private Color FillColor;
  private int d;
  }
  </code></pre>
  
  <p>Here is my Panel class which I add to the Jframe in a TestNewTetris class.</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Image;
  import java.awt.Graphics;
  import java.awt.event.MouseEvent;
  import java.awt.event.MouseListener;
  import java.awt.event.MouseMotionListener;
  
  import javax.swing.*;
  
  import java.util.*;
  
  
  
  public class Panel extends JPanel 
  implements MouseListener, MouseMotionListener
  {
  
  
  private ArrayList&lt;CTetriMino&gt; originals;  
  private ArrayList&lt;CTetriMino&gt; duplicates;
  
  private CTetriMino BlockToBeMoved;
  private int m_nOffsetX; // difference between cursor and top-left corner
  private int m_nOffsetY;
  
  // double buffering
  private Image backBuffer;
  private Graphics gBackBuffer;
  
  boolean isInitialized;
  
  // init and register mouse event handler
  public Panel()
  {
      isInitialized=false;
      // handle mouse and mouse motion events
      this.addMouseListener(this);
      this.addMouseMotionListener(this);
  }
  
  // Set up the initial state after the panel is created
  void init()
  {
      // Initial state
      duplicates = new ArrayList&lt;CTetriMino&gt;();
      originals = new ArrayList&lt;CTetriMino&gt;();
      Color[] colors = {Color.red, Color.green, Color.blue, Color.magenta, Color.cyan, Color.yellow, Color.orange};
      int count=colors.length;
      int dx=10;
      int dy=30;
      int gap=20;
      int length=(getSize().height-2*dy-(count-1)*gap)/count;
      for (int i=0; i&lt;count; i++) { 
          //originals.add(new CTetriMino((i&lt;count/2)?0:1, dx, dy+i*(length+gap), length, length, colors[i]));
          System.out.println(length);
          originals.add(new CTetriMino(i, dx, dy, length, length, colors[i])); 
          dx = dx+110;
  
      }
      BlockToBeMoved=null; // no shape selected
  
      // create the back buffer
      backBuffer = createImage(getSize().width, getSize().height);
      gBackBuffer = backBuffer.getGraphics();
  }
  
  // State Presentation
  public void paintComponent( Graphics g )
  {
      // super.paintComponent( g ); // clears drawing area
  
      if (!isInitialized) {
          isInitialized=true;
          init();
      }
      // State Presentation, using double buffers
      // First, clear the back buffer
      gBackBuffer.setColor(Color.white);
      gBackBuffer.clearRect(0, 0, getSize().width, getSize().height);
      // draw the originals to back buffer
      for (int i=0; i&lt;originals.size(); i++) {
          originals.get(i).draw(gBackBuffer);
      }
      // draw the duplicates to back buffer
      for (int i=0; i&lt;duplicates.size(); i++) {
          duplicates.get(i).draw(gBackBuffer);
      }
      // copy from back buffer to front
      g.drawImage(backBuffer, 0, 0, null);
      g.fillRect(0, 100, 800, 4);
      g.fillRect(0, 400, 800, 4);
  } // end method paintComponent
  
  // MouseListener event handlers
  public void mouseClicked( MouseEvent e )
  {
      if (e.isMetaDown()) {   // right button
          for (int i=duplicates.size()-1; i&gt;=0; i--) {
              if (duplicates.get(i).containPoint(e.getX(), e.getY())) {
                  duplicates.remove(i);
                  repaint();
                  break;
              }
          }
      }
  }
  
  public void mousePressed( MouseEvent e )
  {
      if (e.isMetaDown()) return; // ignore right button
  
      // First, check the originals, from top down (i.e. back to front)
      for (int i=duplicates.size()-1; i&gt;=0; i--) {
          CTetriMino p=duplicates.get(i);
          if (p.containPoint(e.getX(), e.getY())) {
              duplicates.remove(i);
              duplicates.add(p);  // move to the end, i.e. the top
              BlockToBeMoved=p;
              m_nOffsetX=e.getX()-BlockToBeMoved.getX();
              m_nOffsetY=e.getY()-BlockToBeMoved.getY();
              repaint();
              return;
          }
      }
      // Second, check the orginals 
      for (int i=originals.size()-1; i&gt;=0; i--) {
          CTetriMino p=originals.get(i);
          System.out.println(p.containPoint(e.getX(), e.getY()));
          if (p.containPoint(e.getX(), e.getY())) {
              CTetriMino p2=new CTetriMino(p); // make a copy of p
              duplicates.add(p2); // add to the end
              BlockToBeMoved=p2;  // p2 is selected, to be moved
              m_nOffsetX=e.getX()-BlockToBeMoved.getX();
              m_nOffsetY=e.getY()-BlockToBeMoved.getY();
              repaint();
              return;
          }
      }
  }
  
  public void mouseReleased( MouseEvent e )
  {
      BlockToBeMoved=null; // no shape selected
  }
  
  public void mouseEntered( MouseEvent e )
  {
  }
  
  public void mouseExited( MouseEvent e )
  {
  }
  
  public void mouseMoved( MouseEvent e )
  {
  }
  
  public void mouseDragged( MouseEvent e )
  {
      if (e.isMetaDown()) return; // ignore right button
      System.out.println(BlockToBeMoved);
      if (BlockToBeMoved!=null) {
          BlockToBeMoved.setX(e.getX()-m_nOffsetX);
          BlockToBeMoved.setY(e.getY()-m_nOffsetY);
          repaint();
      }
  
  } // end method mouseDragged
  
  
  
  
  
  
  }
  </code></pre>
  
  <p>I would show what my program does when I run it but I do not have a high enough reputation yet.</p>
  
  <p>The bottom Jframe is what is compiled if I change this code:</p>
  
  <pre><code>case 0:
  g.fillOval(X, Y, d, d);
  g.fillOval(X+d,Y, d, d);
  g.fillOval(X+d,Y-d, d, d);
  g.fillOval(X+d+d,Y-d, d, d);
  break;
  </code></pre>
  
  <p>to this:</p>
  
  <pre><code>case 0:
  g.fillOval(X, Y, Width, Height);
  break;
  </code></pre>
  
  <p>In both variants I am able to drag the green square. In the second variant I am able to drag the red circle. I CAN'T however move my red tetrimino. I am simply wondering why I cannot move my red tetrimino. Eventually I want to be able to move all of the tetriminos and drag them under the second black line to delete them. I also want to be able to rotate the tetriminos by pressing righr click. Any code or advice is appreciated. If you need more information I will provide it asap. 
  Thanks.</p>