際際滷

際際滷Share a Scribd company logo
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.*;

More Related Content

Applets

  • 1. Java Applet Handouts<br />To create an applet from BlueJ, simply click new class then the applet radio button.
  • 2. Note that the classes automatically imported are:
  • 5. All applets generated will extend the JApplet class thus having to implement the ff methods:
  • 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(intx, inty, intwidth, intheight, intdx, intdy) Copies an area of the component by a distance specified by dx and dy.voiddraw3DRect(intx, inty, intwidth, intheight, booleanraised) Draws a 3-D highlighted outline of the specified rectangle.abstract voiddrawArc(intx, inty, intwidth, intheight, intstartAngle, intarcAngle) Draws the outline of a circular or elliptical arc covering the specified rectangle.voiddrawBytes(byte[]data, intoffset, intlength, intx, inty) Draws the text given by the specified byte array, using this graphics context's current font and color.voiddrawChars(char[]data, intoffset, intlength, intx, inty) Draws the text given by the specified character array, using this graphics context's current font and color.abstract booleandrawImage(Imageimg, intx, inty, Colorbgcolor, ImageObserverobserver) Draws as much of the specified image as is currently available.abstract booleandrawImage(Imageimg, intx, inty, ImageObserverobserver) Draws as much of the specified image as is currently available.abstract booleandrawImage(Imageimg, intx, inty, intwidth, intheight, Colorbgcolor, ImageObserverobserver) Draws as much of the specified image as has already been scaled to fit inside the specified rectangle.abstract booleandrawImage(Imageimg, intx, inty, intwidth, intheight, ImageObserverobserver) Draws as much of the specified image as has already been scaled to fit inside the specified rectangle.abstract booleandrawImage(Imageimg, intdx1, intdy1, intdx2, intdy2, intsx1, intsy1, intsx2, intsy2, Colorbgcolor, ImageObserverobserver) 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(Imageimg, intdx1, intdy1, intdx2, intdy2, intsx1, intsy1, intsx2, intsy2, ImageObserverobserver) 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(intx1, inty1, intx2, inty2) Draws a line, using the current color, between the points (x1,y1) and (x2,y2) in this graphics context's coordinate system.abstract voiddrawOval(intx, inty, intwidth, intheight) Draws the outline of an oval.abstract voiddrawPolygon(int[]xPoints, int[]yPoints, intnPoints) Draws a closed polygon defined by arrays of x and y coordinates.voiddrawPolygon(Polygonp) Draws the outline of a polygon defined by the specified Polygon object.abstract voiddrawPolyline(int[]xPoints, int[]yPoints, intnPoints) Draws a sequence of connected lines defined by arrays of x and y coordinates.voiddrawRect(intx, inty, intwidth, intheight) Draws the outline of the specified rectangle.abstract voiddrawRoundRect(intx, inty, intwidth, intheight, intarcWidth, intarcHeight) Draws an outlined round-cornered rectangle using this graphics context's current color.abstract voiddrawString( HYPERLINK quot; http://java.sun.com/j2se/1.4.2/docs/api/java/text/AttributedCharacterIterator.htmlquot; quot; interface in java.textquot; AttributedCharacterIteratoriterator, intx, inty) Draws the text given by the specified iterator, using this graphics context's current color.abstract voiddrawString(Stringstr, intx, inty) Draws the text given by the specified string, using this graphics context's current font and color.voidfill3DRect(intx, inty, intwidth, intheight, booleanraised) Paints a 3-D highlighted rectangle filled with the current color.<br />abstract voidfillArc(intx, inty, intwidth, intheight, intstartAngle, intarcAngle) Fills a circular or elliptical arc covering the specified rectangle.abstract voidfillOval(intx, inty, intwidth, intheight) Fills an oval bounded by the specified rectangle with the current color.abstract voidfillPolygon(int[]xPoints, int[]yPoints, intnPoints) Fills a closed polygon defined by arrays of x and y coordinates.voidfillPolygon(Polygonp) Fills the polygon defined by the specified Polygon object with the graphics context's current color.abstract voidfillRect(intx, inty, intwidth, intheight) Fills the specified rectangle.abstract voidfillRoundRect(intx, inty, intwidth, intheight, intarcWidth, intarcHeight) 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(Colorc) Sets this graphics context's current color to the specified color.abstract voidsetFont(Fontfont) Sets this graphics context's font to the specified font.<br />How to add Mouse Events<br />Import necessary classes:
  • 19. public void mouseEntered( MouseEvent e ) {
  • 20. // called when the pointer enters the applet's rectangular area
  • 21. }
  • 22. public void mouseExited( MouseEvent e ) {
  • 23. // called when the pointer leaves the applet's rectangular area
  • 24. }
  • 25. public void mouseClicked( MouseEvent e ) {
  • 26. // called after a press and release of a mouse button
  • 27. // with no motion in between
  • 28. // (If the user presses, drags, and then releases, there will be
  • 29. // no click event generated.)
  • 30. }
  • 31. public void mousePressed( MouseEvent e ) {
  • 32. // called after a button is pressed down
  • 33. }
  • 34. public void mouseReleased( MouseEvent e ) {
  • 35. // called after a button is released
  • 36. }
  • 37. public void mouseMoved( MouseEvent e ) {
  • 38. // called during motion when no buttons are down
  • 39. }
  • 40. public void mouseDragged( MouseEvent e ) {
  • 41. // called during motion with buttons down
  • 42. }
  • 43. Add the interface in the init function
  • 47. Int getClickCount() Return the number of mouse clicks associated with this event.
  • 48. Int getX() Returns the horizontal x position of the event relative to the source component.
  • 49. Int getY() Returns the vertical y position of the event relative to the source component.