Computer Science 252
Problem Solving with Java
Spring 2016, The College of Saint Rose
DragStudentsS16 BlueJ Project
Click here to download a BlueJ project for DragStudentsS16.
DragStudentsS16 Source Code
The Java source code for DragStudentsS16 is below. Click on a file name to download it.
import objectdraw.*;
import java.awt.*;
import java.util.Random;
/*
* Example DragStudentsS16: drag around all of you!
*
* Jim Teresco, The College of Saint Rose, CSC 252, Spring 2016
*
* $Id: DragStudentsS16.java 2763 2015-11-14 19:46:02Z terescoj $
*/
public class DragStudentsS16 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 = {
"alberto.jpg",
"andrew.jpg",
"antoinette.jpg",
"ashan.jpg",
"astrid.jpg",
"bria.jpg",
"darius.jpg",
"daulton.jpg",
"erick.jpg",
"gowtham.jpg",
"jackie.jpg",
"jesse.jpg",
"johnrobert.jpg",
"kenny.jpg",
"kristin.jpg",
"maria.jpg",
"matt.jpg",
"mohammed.jpg",
"randy.jpg",
"roma.jpg",
"rowan.jpg",
"sarah.jpg",
"stivi.jpg",
"ted.jpg",
"vincent.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;
}
}
}