/** * Applet class SmileyFace creates a screen where the user clicks and * drags their mouse. When the user lets go of the mouse, a smiley face * is drawn. The center of the face is where the user clicked, and the * radius is how far the user dragged the mouse. The smile is a 45 * degree arc, and the eyes are dots (single pixels). * * @author David Thibodeau * @version 16-July-2002 */ import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class SmileyFace extends Applet implements MouseListener, MouseMotionListener { private int APPLET_WIDTH = 500; private int APPLET_HEIGHT = 500; private Point startPoint = null; // holds where mouse was clicked private Point endPoint = null; // holds where mouse was dragged to /** * init method creates the mouse event listener object, sets the * background color and applet size. */ public void init() { addMouseListener (this); addMouseMotionListener (this); setBackground (Color.black); setSize (APPLET_WIDTH, APPLET_HEIGHT); } // end init method /** * This may be the most important method in your applet: Here, the * drawing of the applet gets done. "paint" gets called everytime the * applet should be drawn on the screen. So put the code here that * shows the applet. * * @param Graphics g - the Graphics object for this applet */ public void paint(Graphics g) { g.setColor(Color.yellow); if (startPoint != null && endPoint != null) { g.setColor(Color.yellow); int xradius = Math.abs(endPoint.x - startPoint.x); int yradius = Math.abs(endPoint.y - startPoint.y); g.drawOval(startPoint.x, startPoint.y + (yradius / 4), xradius, yradius); g.drawArc (startPoint.x, startPoint.y, xradius, yradius, 225, 90); // Smile g.fillOval(startPoint.x + (xradius / 4), startPoint.y + (yradius / 2), xradius / 10, yradius / 10); // Left eyeball g.fillOval(startPoint.x + xradius - (xradius / 3), startPoint.y + (yradius / 2), xradius / 10, yradius / 10); // Right eyeball } // end if (startPoint != null && endPoint != null) } // end paint(Graphics g) public void mousePressed (MouseEvent event) { startPoint = event.getPoint(); } // end mousePressed method public void mouseDragged (MouseEvent event) { endPoint = event.getPoint(); repaint(); } public void mouseClicked (MouseEvent event) {} // These events not public void mouseReleased (MouseEvent event) {} // needed for this public void mouseEntered (MouseEvent event) {} // applet, but must public void mouseExited (MouseEvent event) {} // be implemented public void mouseMoved (MouseEvent event) {} }