title: Check collisions between two objects in java
body: <p>I have a method that checks if the player and the item are intersecting and adds the item to the inventory, but the method only seems to work if the rectangles are completely overlapping, and the item is not made null unless i specifically say so in the panel. Otherwise, the item keeps moving around the panel as if nothing happened.</p>
  
  <pre><code>public boolean obtainItem(Item item)
  {
      if (item.moveable)
      {
          Rectangle p = getBounds();
          Rectangle i = item.getBounds();
          if (p.intersects(i))
          {
              inventory.add(item);
              i = null;
              System.out.println("hello");
              return true;
          }
      }
      return false;
  }
  </code></pre>
  
  <p>The code for my getBounds() methods are</p>
  
  <p>public Rectangle getBounds()
      {
          bounds = new Rectangle(x, y, 40, 40);
          return bounds;
      }</p>
  
  <p>And it is returning the correct bounds</p>