// rough demo of reading an image from a file, handling mouse clicks, // and displaying the image wherever the mouse is clicked. // // optional parameters to applet // image - name of png, gif, or jpg file in directory with file // height - applet height between 150 and 500 // width - applet window width between 150 and 500 // example in HTML: // // // // // // // Beth Katz, March 2008 import java.awt.*; import javax.swing.JApplet; import java.awt.event.*; public class MovingPicture extends JApplet implements MouseListener { // default values private static final long serialVersionUID = 1L; private final int APPLET_WIDTH_DEFAULT = 320; private final int APPLET_HEIGHT_DEFAULT = 320; private final int IMAGE_HEIGHT = 80; private final int IMAGE_WIDTH = 80; private final String IMAGE_DEFAULT = "sunflower.jpg"; // instance variables private Image theImage; private int imageX, imageY; private int appletWidth; private int appletHeight; /* * initializes the applet - sets image, adjusts sizes * these will read some parameters from the HTML request */ public void init( ) { loadImage( ); appletWidth = APPLET_WIDTH_DEFAULT; appletHeight = APPLET_HEIGHT_DEFAULT; adjustAppletSize( ); setSize(appletWidth, appletHeight); setImageLoc(appletWidth/2, appletHeight/2); addMouseListener(this); } /* * loads the image (from parameter if provided) * really gets the name of the image but doesn't load */ public void loadImage( ) { String s = getParameter("image"); String imageName = (s != null) ? s : IMAGE_DEFAULT; theImage = getImage(getDocumentBase( ), imageName); } /* * adjusts applet dimensions if good parameters are supplied */ public void adjustAppletSize( ) { String h = getParameter("height"); String w = getParameter("width"); if (h != null && w != null) { int height = Integer.parseInt(h); int width = Integer.parseInt(w); if (150 <= height && height <= 500) { appletHeight = height; } if (150 <= width && width <= 500) { appletWidth = width; } } } /* * changes location where image will be displayed * so that it is centered at xLoc,yLoc */ public void setImageLoc(int xLoc, int yLoc) { int x = xLoc - (IMAGE_WIDTH / 2); int y = yLoc - (IMAGE_HEIGHT / 2); imageX = x > 0 ? x : 0; imageY = y > 0 ? y : 0; } /* * draws the image at imageX,imageY after clearing the applet area */ public void drawPictures(Graphics page) { page.clearRect(0, 0, appletWidth, appletHeight); page.drawImage(theImage, imageX, imageY, IMAGE_WIDTH, IMAGE_HEIGHT, this); } /* * called to paint the applet area */ public void paint (Graphics page) { drawPictures(page); } /** * Handles the mouse click event by changing where image is displayed * @param me the mouse event containing location and modifier keys */ public void mouseClicked(MouseEvent me) { setImageLoc(me.getX( ), me.getY( )); // System.out.println("Mouse click at " + me.getX( ) + "," + me.getY( )); repaint( ); } ///////// these are not used here but are needed for mouse listener /////// /** * Handles the mouse entered event by printing a message. * @param me the mouse event containing location and modifier keys */ public void mouseEntered(MouseEvent e) { } /** * Handles the mouse exited event by printing a message. * @param me the mouse event containing location and modifier keys */ public void mouseExited(MouseEvent e) { } /** * Handles the mouse down event by printing a message.. * @param me the mouse event containing location and modifier keys */ public void mousePressed(MouseEvent me) { } /** * Handles the mouse up event by printing a message. * @param me the mouse event containing location and modifier keys */ public void mouseReleased(MouseEvent me) { } }