/*************************************************************** * SmileyPanel.java * Dean & Dean * * This class contains a smiley image and listeners * that enable image dragging and image swapping. ***************************************************************/ package class8_2_DragListener; import javax.swing.ImageIcon; import javax.swing.JPanel; import java.awt.Graphics; import java.awt.Point; import java.awt.event.*; /** * @author Dean & Dean. This example taken nearly straight from the classic Java * text, p. 801. */ public class SmileyPanel extends JPanel { private final ImageIcon SMILEY; private final ImageIcon SCARED; // Question: why aren't these constants static? private final int PANEL_WIDTH; private final int PANEL_HEIGHT; private boolean isPressed = false; private boolean wasPressed = false; /** * image's top-left corner location */ private Point imageCorner; /** * mouse location for previous event (?) */ private Point mouseDownLocation; /** * Either SMILEY or SCARED, depending on what we want... */ private ImageIcon image; /** * Create the SmileyPanel */ public SmileyPanel() { SMILEY = new ImageIcon("smiley.png"); SCARED = new ImageIcon("scared.png"); PANEL_WIDTH = SMILEY.getIconWidth(); PANEL_HEIGHT = SMILEY.getIconHeight(); image = SMILEY; imageCorner = new Point(0, 0); // image starts at top left ClickListener clickListener = new ClickListener(); DragListener dragListener = new DragListener(); this.addMouseListener(clickListener); this.addMouseMotionListener(dragListener); } // end SmileyComponent constructor //************************************************************ private class ClickListener extends MouseAdapter { // When mouse pressed, change to scared image. public void mousePressed(MouseEvent e) { e.getModifiersEx(); isPressed = true; image = SCARED; mouseDownLocation = e.getPoint(); // save current position repaint(); } // end mousePressed // When mouse released, return to smiley image. public void mouseReleased(MouseEvent e) { isPressed = false; wasPressed = false; image = SMILEY; repaint(); } // end mouseReleased } // end class ClickListener //******************************************************* private class DragListener extends MouseMotionAdapter { // Enable image to be dragged by mouse. public void mouseDragged(MouseEvent e) { Point currentPt = e.getPoint(); // current position if (isPressed && wasPressed || isPressed && currentPt.getX() >= imageCorner.getX() && currentPt.getX() <= imageCorner.getX() + PANEL_WIDTH && currentPt.getY() >= imageCorner.getY() && currentPt.getY() <= imageCorner.getY() + PANEL_HEIGHT ) { wasPressed = true; System.out.println("Got here."); imageCorner.translate( (int) (currentPt.getX() - mouseDownLocation.getX()), (int) (currentPt.getY() - mouseDownLocation.getY())); mouseDownLocation = currentPt; // save current position repaint(); } } // end mouseDragged } // end class DragListener //******************************************************* /** * Draw the window, including the icon. * @param g the icon */ public void paintComponent(Graphics g) { super.paintComponent(g); image.paintIcon(this, g, (int) imageCorner.getX(), (int) imageCorner.getY()); } // end paintComponent }