|
|
||||||
Due Sunday Nov. 19, 2000, 11:59 PMThis week's lab is about writing and using anonymous classes as listeners for user-generated events. The Java 1.1 Event Model, so called because it first appeared in Java's 1.1 version, specifies that graphical components (Component class), utilize objects implementing "listener" interfaces as receivers of events such as keyboard or mouse actions. This is the Observer-Observable design pattern , where the Component is the Observable and the listener is the Observer. These actions include, but are not limited to, clicking the mouse, dragging the mouse, resizing a window, passing the mouse over a component, pressing a key, etc. Since each component often (usually) has a unique response when an event occurs, the listener object(s) that is(are) attached to it are usually unique. That is, components do not usually share the same listener. Anonymous classes are the perfect solution to situations like this where only one instance of a particular class is needed. To illustrate the usage of anonymous classes, we will build an interactive picture puzzle game. This puzzle will take an image file, JPG or GIF, and slice it into many small blocks. The user will then be able to drage the blocks around on the sceen and reassemble them back into the original picture. To accomplish this, will require anonymous classes to listen for mouse events and move the mouse accordingly, plus "for" loops to initialize the game board and puzzle pieces. Note: This lab is in keeping with the trend of letting the student be more and more autonomous in writing code, so the directions are deliberately sparse in places. The bottom line is to have the game run as described below, with a reasonable architecture underneath. Description of the Puzzle GameThe picture puzzle game will perform as follows:
Structure of the Picture Puzzle Game ProgramClick here to see the documentation for the classes needed for this projectSynopsis of structure:The JPanel called jPanelPuzzle holds both the puzzle pieces and another JPanel which in turn holds a "snap-to-grid" of Canvases (like JPanels, but simpler). The puzzle pieces themselves are extensions of Canvas and are the same size and shape as the snap-to-grid Canvases. Listeners for mouse clicks and movement are added to the puzzle pieces to enable them to be dragged around the screen and to enable them to "snap" into place on the grid. The seemingly odd layering of panels and having the puzzle pieces on jPuzzlePanel and the grid on a different panel is to separate to puzzle pieces from the grid so the system can figure out which grid Canvas is underneath the puzzle piece of interest. A more detailed look:The class structure and associated behavior of the game is as such:
We will break this lab into two parts: initializing the puzzle game and creating puzzle pieces that can be dragged and dropped on the screen. Follow the sections to the left in order and do not proceed until each section (and sub-section) is working! This lab involves significant usage of methods that are part of classes in the Java AWT package. You are expected to look up these methods in the Java API documentation and figure out what they do. Lab created and written by Stephen Wong and Antonio Garcia, with portions of section 2A from Rhys Price Jones, Rich Salter and Daniel Hutchings. |