Java Applet Handouts<br />To create an applet from BlueJ, simply click new class then the applet radio button.
Note that the classes automatically imported are:
import java.awt.*;
import javax.swing.*;

Applets

  • 1.
    Java Applet Handouts<br/>To create an applet from BlueJ, simply click new class then the applet radio button.
  • 2.
    Note that theclasses automatically imported are:
  • 3.
  • 4.
  • 5.
    All applets generatedwill extend the JApplet class thus having to implement the ff methods:
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
    public void destroy(){ }Methods will run in sequence displayed.<br />To customize the look of the applet, call Graphics methods in the paint method.<br />abstract  voidcopyArea(int x, int y, int width, int height, int dx, int dy)           Copies an area of the component by a distance specified by dx and dy. voiddraw3DRect(int x, int y, int width, int height, boolean raised)           Draws a 3-D highlighted outline of the specified rectangle.abstract  voiddrawArc(int x, int y, int width, int height, int startAngle, int arcAngle)           Draws the outline of a circular or elliptical arc covering the specified rectangle. voiddrawBytes(byte[] data, int offset, int length, int x, int y)           Draws the text given by the specified byte array, using this graphics context's current font and color. voiddrawChars(char[] data, int offset, int length, int x, int y)           Draws the text given by the specified character array, using this graphics context's current font and color.abstract  booleandrawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer)           Draws as much of the specified image as is currently available.abstract  booleandrawImage(Image img, int x, int y, ImageObserver observer)           Draws as much of the specified image as is currently available.abstract  booleandrawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer)           Draws as much of the specified image as has already been scaled to fit inside the specified rectangle.abstract  booleandrawImage(Image img, int x, int y, int width, int height, ImageObserver observer)           Draws as much of the specified image as has already been scaled to fit inside the specified rectangle.abstract  booleandrawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer)           Draws as much of the specified area of the specified image as is currently available, scaling it on the fly to fit inside the specified area of the destination drawable surface.abstract  booleandrawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)           Draws as much of the specified area of the specified image as is currently available, scaling it on the fly to fit inside the specified area of the destination drawable surface.abstract  voiddrawLine(int x1, int y1, int x2, int y2)           Draws a line, using the current color, between the points (x1, y1) and (x2, y2) in this graphics context's coordinate system.abstract  voiddrawOval(int x, int y, int width, int height)           Draws the outline of an oval.abstract  voiddrawPolygon(int[] xPoints, int[] yPoints, int nPoints)           Draws a closed polygon defined by arrays of x and y coordinates. voiddrawPolygon(Polygon p)           Draws the outline of a polygon defined by the specified Polygon object.abstract  voiddrawPolyline(int[] xPoints, int[] yPoints, int nPoints)           Draws a sequence of connected lines defined by arrays of x and y coordinates. voiddrawRect(int x, int y, int width, int height)           Draws the outline of the specified rectangle.abstract  voiddrawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)           Draws an outlined round-cornered rectangle using this graphics context's current color.abstract  voiddrawString( HYPERLINK \" http://java.sun.com/j2se/1.4.2/docs/api/java/text/AttributedCharacterIterator.html\" \o \" interface in java.text\" AttributedCharacterIterator iterator, int x, int y)           Draws the text given by the specified iterator, using this graphics context's current color.abstract  voiddrawString(String str, int x, int y)           Draws the text given by the specified string, using this graphics context's current font and color. voidfill3DRect(int x, int y, int width, int height, boolean raised)           Paints a 3-D highlighted rectangle filled with the current color.<br />abstract  voidfillArc(int x, int y, int width, int height, int startAngle, int arcAngle)           Fills a circular or elliptical arc covering the specified rectangle.abstract  voidfillOval(int x, int y, int width, int height)           Fills an oval bounded by the specified rectangle with the current color.abstract  voidfillPolygon(int[] xPoints, int[] yPoints, int nPoints)           Fills a closed polygon defined by arrays of x and y coordinates. voidfillPolygon(Polygon p)           Fills the polygon defined by the specified Polygon object with the graphics context's current color.abstract  voidfillRect(int x, int y, int width, int height)           Fills the specified rectangle.abstract  voidfillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)           Fills the specified rounded corner rectangle with the current color.abstract  ColorgetColor()           Gets this graphics context's current color.abstract  FontgetFont()           Gets the current font.abstract  voidsetColor(Color c)           Sets this graphics context's current color to the specified color.abstract  voidsetFont(Font font)           Sets this graphics context's font to the specified font.<br />How to add Mouse Events<br />Import necessary classes:
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
    // called whenthe pointer enters the applet's rectangular area
  • 21.
  • 22.
    public void mouseExited(MouseEvent e ) {
  • 23.
    // called whenthe pointer leaves the applet's rectangular area
  • 24.
  • 25.
  • 26.
    // called aftera press and release of a mouse button
  • 27.
    // with nomotion in between
  • 28.
    // (If theuser presses, drags, and then releases, there will be
  • 29.
    // no clickevent generated.)
  • 30.
  • 31.
    public void mousePressed(MouseEvent e ) {
  • 32.
    // called aftera button is pressed down
  • 33.
  • 34.
  • 35.
    // called aftera button is released
  • 36.
  • 37.
    public void mouseMoved(MouseEvent e ) {
  • 38.
    // called duringmotion when no buttons are down
  • 39.
  • 40.
    public void mouseDragged(MouseEvent e ) {
  • 41.
    // called duringmotion with buttons down
  • 42.
  • 43.
    Add the interfacein the init function
  • 44.
  • 45.
  • 46.
  • 47.
    Int getClickCount()           Returnthe number of mouse clicks associated with this event.
  • 48.
    Int getX()           Returnsthe horizontal x position of the event relative to the source component.
  • 49.
    Int getY()           Returnsthe vertical y position of the event relative to the source component.