public void mousePressed(MouseEvent evt) { // User has pressed the mouse. Find the shape that the user has clicked on, if // any. If there is a shape at the position when the mouse was clicked, then // start dragging it. If the user was holding down the shift key, then bring // the dragged shape to the front, in front of all the other shapes. int x = evt.getX(); // x-coordinate of point where mouse was clicked int y = evt.getY(); // y-coordinate of point for (int i = shapes.size() - 1; i >= 0; i--) { // check shapes from front to back Shape s = (Shape) shapes.get(i); if (s.containsPoint(x, y)) { shapeBeingDragged = s; prevDragX = x; prevDragY = y; if (evt.isShiftDown()) { // Bring the shape to the front by moving it to shapes.remove(s); // the end of the list of shapes. shapes.add(s); repaint(); // repaint canvas to show shape in front of other shapes } return; } } }