Computer Science 120
Introduction to Programming
Spring 2012, Siena College
DragStudentsS12 Demo
A working demo of DragStudentsS12 will appear below. Click inside the applet to interact with it.
DragStudentsS12 BlueJ Project
Click here to download a BlueJ project for DragStudentsS12.
DragStudentsS12 Source Code
The Java source code for DragStudentsS12 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: DragStudentsS12.java 1846 2012-04-10 15:29:49Z terescoj $
*/
public class DragStudentsS12 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 = {
"amandameher-head.jpg",
"amarmian-head.jpg",
"annagrant-head.jpg",
"corydelagorgendiere-head.jpg",
"emilymejia-head.jpg",
"jamessaddlemire-head.jpg",
"johnbestafka-head.jpg",
"johnkiernozek-head.jpg",
"joshmcdonald-head.jpg",
"juliannesgoifo-head.jpg",
"justinrose-head.jpg",
"kathleenrotondo-head.jpg",
"kathrynmannix-head.jpg",
"laurendavis-head.jpg",
"lesliakeo-head.jpg",
"marissabianchi-head.jpg",
"maryritchie-head.jpg",
"micaelaethier-head.jpg",
"rachelstoklosa-head.jpg",
"ryanclancy-head.jpg",
"shannonmuldowney-head.jpg",
"tylerkallquist-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<heads.length; imageNum++) {
if (heads[imageNum].contains(point)) {
selectedHead = heads[imageNum];
dragging = true;
selectedHead.sendToFront();
}
}
}
// make the head being dragged bigger
if (dragging) {
selectedHead.setHeight(IMAGE_SIZE * DRAG_ZOOM);
selectedHead.setWidth(IMAGE_SIZE * DRAG_ZOOM);
}
}
/**
* continue dragging if currently dragging
*
* @param point the Location of the mouse when dragged
*/
public void onMouseDrag(Location point) {
if (dragging) {
selectedHead.move(point.getX() - lastPoint.getX(),
point.getY() - lastPoint.getY());
lastPoint = point;
}
}
/**
* finish dragging operation
*
* @param point the Location of the mouse release
*/
public void onMouseRelease(Location point) {
if (dragging) {
selectedHead.setHeight(IMAGE_SIZE);
selectedHead.setWidth(IMAGE_SIZE);
dragging = false;
}
}
}