Computer Science 252
Problem Solving with Java

Spring 2014, The College of Saint Rose

StayPuft Demo

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



StayPuft BlueJ Project

Click here to download a BlueJ project for StayPuft.


StayPuft Source Code

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


StayPuft.java

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

/*
 * Example StayPuft: a Marshmallow Man!  Draggable!
 *
 * Jim Teresco, The College of Saint Rose, CSC 252, Spring 2014
 *
 * $Id: StayPuft.java 2328 2014-02-25 14:15:29Z terescoj $
 */

public class StayPuft extends WindowController {
    
    // an objectdraw object that can store a picture
    private VisibleImage marshmallowMan;

    // for dragging
    private Location lastPoint;

    public void begin() {
        // load our image
        Image mManPic = getImage("mm.jpg");

        // create an objectdraw object using that image
        marshmallowMan = new VisibleImage(mManPic, 10, 10, canvas);

        marshmallowMan.setWidth(166);
        marshmallowMan.setHeight(255);
    }

    public void onMousePress(Location point) {
        lastPoint = point;
    }

    public void onMouseDrag(Location point) {
        
        if (marshmallowMan.contains(lastPoint)) {
            marshmallowMan.move(point.getX()-lastPoint.getX(),
                point.getY()-lastPoint.getY());
            lastPoint = point;
        }
    }
}