Page 719 - Introduction to Programming with Java: A Problem Solving Approach
P. 719
16.18 Mouse Listeners and Images (Optional) 685
}
// end class ClickListener
}
// end class DragListener
// When mouse released, return to smiley image.
public void mouseReleased(MouseEvent e)
{
}
// end mouseDragged
image = SMILEY;
repaint();
} // end mouseReleased
//*******************************************************
private class DragListener extends MouseMotionAdapter
{
// Enable image to be dragged by mouse.
public void mouseDragged(MouseEvent e)
{
Point currentPt = e.getPoint(); // current position
// Make sure mouse was pressed within the image.
if (currentPt.getX() >= imageCorner.getX() &&
currentPt.getX() <= imageCorner.getX() + WIDTH &&
{
}
currentPt.getY() >= imageCorner.getY() &&
currenAtpPta.ggetoY() P<=DiFmageECnorhnear.ngectYe()r+ imageCorner.translate(
(int) (currentPt.getX() - prevPt.getX()),
HEIGHT)
(int) (currentPt.getY() - prevPt.getY()));
prevPt = currentPt; // save current position
repaint();
//*******************************************************
// Draw the window, including the updated image.
public void paintComponent(Graphics g)
}
// end class SmileyPanel
{
}
super.paintComponent(g);
⎫ image.paintIcon(this, g, ⎬
(int) imageCorner.getX(), (int) imageCorner.getY());⎭ // end paintComponent
Call paintIcon to display the image.
Figure 16.17b The DragSmiley program’s SmileyPanel class—part B