Drag Cards
Learn to move the card on screen with the mouse button and go to its actual place on button release.
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.
- Add a public method called
contains()
. It should take two parameters, integers calledpointX
andpointY
, and return a boolean. - Create a boolean called
contains
, initialized to false. - If
pointX
is betweenx
andx
pluswidth
, andpointY
is betweeny
andy
plusheight
, set contains to true. - Return
contains
.
...
________________ contains(_________________) {
__________ contains = ________;
if (pointX ______
&& pointX ______
&& pointY ______
&& pointY ______) {
contains = ______;
}
return ______;
}
...
Get hands-on with 1200+ tech skills courses.