Computer Science 252
Problem Solving with Java
Spring 2015, The College of Saint Rose
DragStudentsS15 Demo
A working demo of DragStudentsS15 will appear below. Click inside the applet to interact with it.
DragStudentsS15 BlueJ Project
Click here to download a BlueJ project for DragStudentsS15.
DragStudentsS15 Source Code
The Java source code for DragStudentsS15 is below. Click on a file name to download it.
import objectdraw.*; import java.awt.*; import java.util.Random; /* * Example DragStudentsS15: drag around all of you! * * Jim Teresco, The College of Saint Rose, CSC 252, Spring 2015 * * $Id: DragStudentsS15.java 2484 2014-11-17 04:10:26Z terescoj $ */ public class DragStudentsS15 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 = { "alec.jpg", "andrew.jpg", "catherine.jpg", "chris.jpg", "cody.jpg", "johnb.jpg", "johnr.jpg", "kat.jpg", "ken.jpg", "matt.jpg", "mattn.jpg", "michael.jpg", "michaelm.jpg", "mike.jpg", "ryan.jpg", "sm.jpg", "trevor.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); } } /** * 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; } } }