Computer Science 252
Problem Solving with Java

Spring 2015, The College of Saint Rose

BookGame Demo

A working demo of BookGame will appear below. Click inside the applet to interact with it.



BookGame BlueJ Project

Click here to download a BlueJ project for BookGame.


BookGame Source Code

The Java source code for BookGame is below. Click on a file name to download it.


BookGame.java


/**
 * In Class Book Game implementation: yet another simple custom
 * object that we can drag around.
 * 
 * @author Jim Teresco
 * 
 * $Id$
 */

import objectdraw.*;
import java.awt.*;

public class BookGame extends WindowController
{
    // a couple of books
    private Book hp, mr;
    
    // dragging support
    private boolean draggingHP, draggingMR;
    private Location lastMouse;
    
    public void begin() 
    {
        // let's create a couple Books.
        double bookX = 100;
        hp = new Book(bookX, 100, "Harry Potter", canvas);
        bookX += 150;
        mr = new Book(bookX, 75, "Maze Runner", canvas);
    }

    public void onMousePress(Location point) {
        
        // set up to drag either or both books if we press on them
        if (hp.contains(point)) {
            draggingHP = true;
        }
        if (mr.contains(point)) {
            draggingMR = true;
        }
        lastMouse = point;
    }
    
    public void onMouseDrag(Location point) {
        
        // standard dragging procedure
        if (draggingHP) {
            hp.move(point.getX() - lastMouse.getX(),
                    point.getY() - lastMouse.getY());
        }
        if (draggingMR) {
            mr.move(point.getX() - lastMouse.getX(),
                    point.getY() - lastMouse.getY());
        }
        lastMouse = point;
    }
    
    public void onMouseRelease(Location point) {
        
        // finish up our drag
        if (draggingHP) {
            hp.move(point.getX() - lastMouse.getX(),
                    point.getY() - lastMouse.getY());
        }
        if (draggingMR) {
            mr.move(point.getX() - lastMouse.getX(),
                    point.getY() - lastMouse.getY());
        }
        draggingHP = false;
        draggingMR = false;
        
    }
    
    public void onMouseExit(Location point) {
        
        // when we leave the window, we ask the Books to go back
        // where they started.
        hp.returnHome();
        mr.returnHome();
    }
}