Computer Science 252
Problem Solving with Java
Fall 2015, The College of Saint Rose
DragStudentsF15 Demo
A working demo of DragStudentsF15 will appear below. Click inside the applet to interact with it.
DragStudentsF15 BlueJ Project
Click here to download a BlueJ project for DragStudentsF15.
DragStudentsF15 Source Code
The Java source code for DragStudentsF15 is below. Click on a file name to download it.
import objectdraw.*;
import java.awt.*;
import java.util.Random;
/*
* Example DragStudentsF15: drag around all of you!
*
* Jim Teresco, The College of Saint Rose, CSC 252, Spring 2015
*
* $Id: DragStudentsF15.java 2606 2015-04-07 00:18:15Z terescoj $
*/
public class DragStudentsF15 extends WindowController {
// image sizes
private static final int FULL_WIDTH = 125;
private static final int FULL_HEIGHT = 141;
private static final int SMALL_WIDTH = FULL_WIDTH/2;
private static final int SMALL_HEIGHT = FULL_HEIGHT/2;
// the array of image file names
private String[] imageFiles = {
"alex.jpg",
"caitlin.jpg",
"caleb.jpg",
"carlos.jpg",
"chistopher.jpg",
"derek.jpg",
"faiza.jpg",
"ken.jpg",
"nico.jpg",
"sm.jpg"
};
// array of VisibleImage objects for display and dragging
private VisibleImage[] heads;
// support for dragging
private boolean dragging;
private VisibleImage selectedHead;
private Location lastPoint;
/**
* draw the initial random scattering of student pictures
* on the canvas, taking care not to overlap initially.
*/
public void begin() {
// a larger canvas.
setSize(600, 600);
// we'll need to generate some random positions
Random r = new Random();
// construct the array of VisibleImage objects we'll need to remember -
// it will need to be the same length as the static array we
// declared and constructed above
heads = new VisibleImage[imageFiles.length];
// place the images on the canvas randomly
for (int imageNum = 0; imageNum < heads.length; imageNum++) {
heads[imageNum] = new VisibleImage(getImage(imageFiles[imageNum]),
r.nextDouble()*(canvas.getWidth() - SMALL_WIDTH),
r.nextDouble()*(canvas.getHeight() - SMALL_HEIGHT),
canvas);
heads[imageNum].setWidth(SMALL_WIDTH);
heads[imageNum].setHeight(SMALL_HEIGHT);
// 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(r.nextDouble()*(canvas.getWidth() - SMALL_WIDTH),
r.nextDouble()*(canvas.getHeight() - SMALL_HEIGHT));
}
}
// 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(FULL_HEIGHT);
selectedHead.setWidth(FULL_WIDTH);
selectedHead.move((SMALL_WIDTH-FULL_WIDTH)/2,(SMALL_HEIGHT-FULL_HEIGHT)/2);
}
}
/**
* 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(SMALL_HEIGHT);
selectedHead.setWidth(SMALL_WIDTH);
selectedHead.move((FULL_WIDTH-SMALL_WIDTH)/2, (FULL_HEIGHT-SMALL_HEIGHT)/2);
dragging = false;
}
}
}