Computer Science 120
Introduction to Programming
Spring 2011, Siena College
DragStudents Demo
A working demo of DragStudents will appear below. Click inside the applet to interact with it.
DragStudents BlueJ Project
Click here to download a BlueJ project for DragStudents.
DragStudents Source Code
The Java source code for DragStudents is below. Click on a file name to download it.
import objectdraw.*;
import java.awt.*;
/*
* Example DragStudents: randomly scatter draggable student pictures on the canvas
*
* Jim Teresco, Siena College, CSIS 120, Spring 2011
*
* $Id: DragStudents.java 1579 2011-03-29 02:44:33Z terescoj $
*/
public class DragStudents extends WindowController {
// all images will be resized to this
private static final int IMAGE_SIZE = 50;
// the array of image file names
private String[] imageFiles = {
"amy-head.jpg",
"billh-head.jpg",
"bryan-head.jpg",
"cameron-head.jpg",
"chelsea-head.jpg",
"christina-head.jpg",
"dana-head.jpg",
"david-head.jpg",
"devinh-head.jpg",
"ed-head.jpg",
"frank-head.jpg",
"grady-head.jpg",
"john-head.jpg",
"jonbias-head.jpg",
"jonbutler-head.jpg",
"justin-head.jpg",
"karl-head.jpg",
"katie-head.jpg",
"leah-head.jpg",
"luke-head.jpg",
"matt-head.jpg",
"meghan-head.jpg",
"pat-head.jpg",
"samantha-head.jpg",
"shannon-head.jpg",
"zack-head.jpg"
};
// array of VisibleImage objects for display and dragging
private VisibleImage[] heads;
// support for dragging
private boolean dragging;
private VisibleImage selectedHead;
private Location lastPoint;
// how much to scale up images when dragging
private static final int DRAG_ZOOM = 4;
/**
* draw the initial random scattering of student pictures
* on the canvas, taking care not to overlap initially.
*/
public void begin() {
// generators for legal positions with the images completely on the canvas
RandomIntGenerator xPosGen = new RandomIntGenerator(0, canvas.getWidth() - IMAGE_SIZE);
RandomIntGenerator yPosGen = new RandomIntGenerator(0, canvas.getHeight() - IMAGE_SIZE);
heads = new VisibleImage[imageFiles.length];
// place the images
for (int imageNum = 0; imageNum < heads.length; imageNum++) {
heads[imageNum] = new VisibleImage(getImage(imageFiles[imageNum]),
xPosGen.nextValue(),
yPosGen.nextValue(),
canvas);
heads[imageNum].setWidth(IMAGE_SIZE);
heads[imageNum].setHeight(IMAGE_SIZE);
// move this image around until we find a position where it
// does not overlap with any already-placed image
while (overlapsAny(imageNum)) {
heads[imageNum].moveTo(xPosGen.nextValue(), yPosGen.nextValue());
}
}
// setup for dragging
dragging = false;
selectedHead = heads[0];
}
/**
* a private helper method to determine whether the VisibleImage
* at heads[index] overlaps with any of the VisibleImages at
* heads[0] up through heads[index-1].
*
* @param index the index of the VisibleImage to check for overlap
* @return a boolean indicating whether heads[index] overlaps any of
* heads[0] through heads[index-1]
*/
private boolean overlapsAny(int index) {
// check each possible overlap
for (int compareTo = 0; compareTo < index; compareTo++) {
if (heads[index].overlaps(heads[compareTo])) {
return true;
}
}
// we didn't find any overlap, so we return false
return false;
}
/**
* start dragging if the Location is on one of the student images
*
* @param point the Location of the mouse when pressed
*/
public void onMousePress(Location point) {
lastPoint = point;
dragging = false;
// test the selected student (which is on top) first.
if (selectedHead.contains(point)) {
dragging = true;
} else {
for (int imageNum = 0; imageNum