Moving objects

To move an object, first draw the object in one location, then erase the object and draw it again in a new location. Repeated in small steps, this approach animates the object. If a mouse is used to move an object, a program can get x and y coordinates from a MouseEvent, and use those coordinates to draw the object being moved.

Adding methods to Card to handle card movement

Next we will add code to use the mouse to select a card, drag the card around, and drop the card on another column or on a foundation stack.

To check whether the user clicked on a card, BakersDozen will need to check if a card contains the point where the mouse was clicked.

  1. Add a public method called contains(). It should take two parameters, integers called pointX and pointY, and return a boolean.
  2. Create a boolean called contains, initialized to false.
  3. If pointX is between x and x plus width, and pointY is between y and y plus height, set contains to true.
  4. Return contains.
    ...
 
  ________________ contains(_________________) {
    __________ contains = ________;
    if (pointX ______
        && pointX ______
        && pointY ______
        && pointY ______) {
      contains = ______;
    }
    return ______;
  }
    ...

Get hands-on with 1200+ tech skills courses.