Drop a Card
Learn to drop a dragged card in the valid location.
Dropping a card on a foundation stack
Now we will write the code to add the dropped card to the stack if the user drops the card in a valid place, or to return the card to its original column if the user drops the card in an invalid place.
- In the constructor, in the block of code that created the new
MouseAdapter
, add a second public method calledmouseReleased()
. It should take one parameter, aMouseEvent
callede
, and return nothing.mouseReleased()
should:- Create integers called
x
andy
, initialized to thex
andy
coordinate values ofe
, respectively using MouseEvent’sgetX()
andgetY()
methods. - Call
released()
, passing itx
andy
.
- Create integers called
Note: If you execute the code right away after adding these changes, it will have a syntax error because released()
does not exist yet.
...
public TablePanel() {
...
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
clicked(x, y);
}
____________ mouseReleased(___________) {
______ x = e.______;
______ y = e.______;
released(________);
}
});
...
Get hands-on with 1200+ tech skills courses.