home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-14 | 1.9 KB | 95 lines |
- import java.awt.*;
- import java.awt.event.*;
- import java.applet.Applet;
-
- /*
- * the applet class
- */
- public class ImageMove extends Applet implementsĪ
- MouseListener, MouseMotionListener{
-
- /*
- * the image to be displayed
- */
- Image img;
-
- /*
- * the width and height of the image
- */
- int width, height;
-
- /*
- * xpos, ypos are the coordinates of the upper left of the image
- */
- int xpos=10, ypos=10;
-
- /*
- * dx, dy are the deltas from the mouse point to xpos, ypos
- */
- int dx, dy;
-
- /*
- * called when the applet is loaded
- * load the image and use MediaTracker so that
- * the width and height are available immediately
- */
- public void init () {
-
- MediaTracker tracker = new MediaTracker (this);
-
- img = getImage (getDocumentBase(), "T1.gif");
- tracker.addImage (img, 0);
- showStatus ("Getting image: T1.gif");
- try {
- tracker.waitForID (0);
- } catch (InterruptedException e) { }
- width = img.getWidth (this);
- height = img.getHeight (this);
- addMouseListener(this);
- addMouseMotionListener(this);
- }
-
- /*
- * paint the image in the new location
- * @param g - destination graphics object
- */
- public void paint (Graphics g) {
-
- g.setColor (Color.white);
- g.drawImage (img, xpos, ypos, this);
- }
-
-
- /*
- * adjust the new position and repaint the image
- */
-
- public void mouseClicked(MouseEvent e){}
- public void mouseEntered(MouseEvent e){}
- public void mouseExited(MouseEvent e){}
- public void mouseReleased(MouseEvent e){}
- public void mouseMoved(MouseEvent e){}
-
- public void mousePressed(MouseEvent e)
- {
- int x =e.getX();
- int y =e.getY();
-
- dx = x - xpos;
- dy = y - ypos;
- }
-
- public void mouseDragged(MouseEvent e)
- {
- int x =e.getX();
- int y =e.getY();
- if (dx < width && dx >= 0 && dy < height && dy >= 0)
- {
- xpos = x - dx;
- ypos = y - dy;
- repaint ();
- }
- }
-
- }
-