Computer Science 252
Problem Solving with Java

Fall 2015, The College of Saint Rose

DrinkMug Demo

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



DrinkMug BlueJ Project

Click here to download a BlueJ project for DrinkMug.


DrinkMug Source Code

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


DrinkMug.java

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

/*
 * Example DrinkMug: a custom class representing a refreshing
 * mug of some beverage.  It is capable of many things.
 *
 * Jim Teresco, The College of Saint Rose, CSC 252, Fall 2015
 *
 * $Id: DrinkMug.java 2717 2015-09-29 17:07:09Z terescoj $
 */

public class DrinkMug {

    private static final double WIDTH = 50;
    private static final double HEIGHT = 100;
    private static final double HANDLE = 20;
    private static final double THICKNESS = 5;
    
    private FilledRect background;
    private FilledOval handle;
    private FilledRect contents;
    
    public DrinkMug(Location position, DrawingCanvas c) {
        
        background = new FilledRect(position, WIDTH, HEIGHT, c);
        handle = new FilledOval(position.getX() + WIDTH - HANDLE / 2,
                                position.getY() + (HEIGHT - HANDLE)/ 2,
                                HANDLE, HANDLE, c);
        contents = new FilledRect(position.getX() + THICKNESS,
                                  position.getY() + HEIGHT - THICKNESS*2,
                                  WIDTH - THICKNESS*2, 0, c);
        contents.setColor(Color.white);
    }
    
    public boolean contains(Location point) {
        
        return background.contains(point) || handle.contains(point);
        
    }
    
    public void move(double dx, double dy) {
        
        background.move(dx, dy);
        contents.move(dx, dy);
        handle.move(dx, dy);
    }
    
    // check if any component here overlaps with the given object
    public boolean overlaps(Drawable2DInterface other) {
        
        return other.overlaps(background) || other.overlaps(handle);
    }
    
    // fill it up!
    public void fill() {
        
        contents.setHeight(HEIGHT-THICKNESS);
        contents.moveTo(contents.getX(), background.getY());
    }
    
    // chill
    public void chill() {
        
        if (contents.getHeight() > 0) {
            contents.setColor(Color.blue);
        }
    }
    
    // heat
    public void heat() {
        
        if (contents.getHeight() > 0) {
            contents.setColor(Color.red);
        }
    }
}

DrinkMugGame.java


/**
 * Example DrinkMug: the window controller class for the DrinkMug
 * custom class example
 *
 * Developed partially in class by CSC 252 Fall 2015
 * 
 * @author Jim Teresco
 */

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

public class DrinkMugGame extends WindowController
{

    private static final double FAUCET_SIZE = 40;
    
    private FilledRect fridge;
    private FilledRect stove;
    private DrinkMug mug1, mug2;
    private FilledRect stream;
    
    // support for dragging
    private DrinkMug draggingMug = null;
    private Location lastMouse;
    
    public void begin() 
    {
        double w = canvas.getWidth();
        double h = canvas.getHeight();
        
        // fridge and stove
        fridge = new FilledRect(2*w/3, 2*h/3, w/3, h/3, canvas);
        fridge.setColor(Color.blue);
        
        stove = new FilledRect(2*w/3, 0, w/3, w/3, canvas);
        stove.setColor(Color.red);
        
        // faucet
        new FilledRect(0, h/2, FAUCET_SIZE*2, FAUCET_SIZE, canvas);
        new FilledRect(FAUCET_SIZE, h/2 + FAUCET_SIZE, FAUCET_SIZE, FAUCET_SIZE, canvas);
        stream = new FilledRect(FAUCET_SIZE*1.33, h/2+2*FAUCET_SIZE, FAUCET_SIZE/3, 
            FAUCET_SIZE, canvas);
        stream.setColor(Color.blue);
        
        // mugs
        mug1 = new DrinkMug(new Location(50, 50), canvas);
        mug2 = new DrinkMug(new Location(200, 250), canvas);
    }

    // set up for standard dragging
    public void onMousePress(Location point) {
        
        if (mug1.contains(point)) {
            draggingMug = mug1;
        }
        else if (mug2.contains(point)) {
            draggingMug = mug2;
        }
        else {
            draggingMug = null;
        }
        lastMouse = point;
    }
    
    // standard dragging
    public void onMouseDrag(Location point) {
        
        if (draggingMug != null) {
            draggingMug.move(point.getX() - lastMouse.getX(),
                             point.getY() - lastMouse.getY());
            lastMouse = point;
        }
    }
    
    public void onMouseRelease(Location point) {
        
        if (draggingMug != null) {
	    // regular dragging move
            draggingMug.move(point.getX() - lastMouse.getX(),
                             point.getY() - lastMouse.getY());
                             
            // check for filling, heating, cooling
            if (draggingMug.overlaps(stream)) {
                draggingMug.fill();
            }
            
            if (draggingMug.overlaps(fridge)) {
                draggingMug.chill();
            }
            
            if (draggingMug.overlaps(stove)) {
                draggingMug.heat();
            }
        }
        
        draggingMug = null;
    }
}