Due at 5:00pm, Monday, March 3rd, 2008, by email submission only.
This assignment explores the following topics related to GUI input:
This assignment builds on top of the code you wrote for PS2, plus a new class:
Add input handling to your StrokeBoardView and PixelBoardView, so that the user can drag checkers around.
Pressing and dragging in a square containing a checker should pick up the checker from the board. The checker should not make any abrupt jumps — neither when the user presses the mouse button, nor when the user starts moving the mouse. The checker should move smoothly with the mouse pointer, hovering over the other checkers on the board as in the example screenshot.
If the user releases the mouse button when the mouse pointer is over an empty square, the checker should be moved to that square using Board.move(). The board model should not be changed until then. If the mouse pointer is over a filled square or off the checkerboard entirely, then the dropped checker should be put back where it was taken from.
If the mouse pointer leaves the checkerboard during the drag operation, the dragged checker may either follow it (becoming invisible because of clipping), or it may stop at the edge of the checkerboard — the behavior is up to you. But if the mouse pointer moves back into the checkerboard, the checker should resume following the mouse.
A word of advice: you may notice that your stroke and pixel model implementations are very similar, differing only in how a single checker is painted. Before you add input to either class, you might want to restructure your code so that one class extends the other, or so that both classes extend a single abstract class, with a method called something like paintChecker() that can be overridden with the appropriate behavior for both models. That way you can just add input handling once, to the superclass, and save yourself a lot of redundant implementation.
We could add input handling to ComponentBoardView in the same way, but instead we're going to build a solution more in the spirit of the component model: a generic controller.
Create a generic, reusable move interactor that is capable of dragging any components around inside their container, not just checker components in your ComponentBoardView. We have provided a skeleton interface for MoveInteractor for you to fill in. Your implementation should be generic, not referring to any classes specific to the checkerboard problem.
MoveInteractor should listen to the draggable components themselves (the checkers) for mouse events. It should not listen to their container (i.e., the checkerboard view).
Use MoveInteractor to add input handling to your ComponentBoardView. Checker dragging should behave the same way as specified in Problem 1. Since MoveInteractor is generic, you need to customize it for the checkerboard, which you can do by creating a subclass of MoveInteractor that overrides drop(), in order to decide whether it should allow the checker to be dropped, and if so, change the board model.
Hints:
A checker should only be picked up when you click on the checker itself, and not the edge of the grid square around it. Modify your MoveInteractor so that this behavior is observed.
This can be done by overriding the contains() method in the CheckerComponent class. Note: There are two contains() methods, one that takes a pair of integers and one that takes a Point. You should override both.
This implementation is only required for the component model.
Answer the following questions in readme.txt.
Package your completed assignment as a jar file, as described in PS1. Here's a checklist of things you should confirm before you hand in:
java -jar yourfile.jar
Submit your solution by email (with all required jar files attached) to 6831handin@csail.mit.edu and include PS3 in the subject line.