Computer Science 252
Problem Solving with Java
Spring 2014, The College of Saint Rose
DragStudentsF14 Demo
A working demo of DragStudentsF14 will appear below. Click inside the applet to interact with it.
DragStudentsF14 BlueJ Project
Click here to download a BlueJ project for DragStudentsF14.
DragStudentsF14 Source Code
The Java source code for DragStudentsF14 is below. Click on a file name to download it.
import objectdraw.*; import java.awt.*; import java.util.Random; /* * Example DragStudentsF14: drag around all of you! * * Jim Teresco, The College of Saint Rose, CSC 252, Fall 2014 * * $Id: DragStudentsS14.java 2348 2014-04-15 18:20:31Z terescoj $ */ public class DragStudentsF14 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 = { "anthony.jpg", "chris.jpg", "christopher.jpg", "gabriel.jpg", "gregory.jpg", "james.jpg", "john.jpg", "kendal.jpg", "kristopher.jpg", "nerys.jpg", "terrell.jpg", "zekiel.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() { // 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); } } /** * 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); dragging = false; } } }