Applets

404 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
404
On SlideShare
0
From Embeds
0
Number of Embeds
15
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Applets

  1. 1. Java Applet Handouts<br /><ul><li>To create an applet from BlueJ, simply click new class then the applet radio button.
  2. 2. Note that the classes automatically imported are:
  3. 3. import java.awt.*;
  4. 4. import javax.swing.*;
  5. 5. All applets generated will extend the JApplet class thus having to implement the ff methods:
  6. 6. public void init() { }
  7. 7. public void start() { }
  8. 8. public void stop() { }
  9. 9. public void paint(Graphics g) { }
  10. 10. public void destroy(){ }</li></ul>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 /><ul><li>Import necessary classes:
  11. 11. import java.applet.*;
  12. 12. import java.awt.*;
  13. 13. import java.awt.event.*;
  14. 14. import javax.swing.*;
  15. 15. Add interfaces
  16. 16. MouseListener
  17. 17. MouseMotionListener
  18. 18. Implements necessary methods
  19. 19. public void mouseEntered( MouseEvent e ) {
  20. 20. // called when the pointer enters the applet's rectangular area
  21. 21. }
  22. 22. public void mouseExited( MouseEvent e ) {
  23. 23. // called when the pointer leaves the applet's rectangular area
  24. 24. }
  25. 25. public void mouseClicked( MouseEvent e ) {
  26. 26. // called after a press and release of a mouse button
  27. 27. // with no motion in between
  28. 28. // (If the user presses, drags, and then releases, there will be
  29. 29. // no click event generated.)
  30. 30. }
  31. 31. public void mousePressed( MouseEvent e ) {
  32. 32. // called after a button is pressed down
  33. 33. }
  34. 34. public void mouseReleased( MouseEvent e ) {
  35. 35. // called after a button is released
  36. 36. }
  37. 37. public void mouseMoved( MouseEvent e ) {
  38. 38. // called during motion when no buttons are down
  39. 39. }
  40. 40. public void mouseDragged( MouseEvent e ) {
  41. 41. // called during motion with buttons down
  42. 42. }
  43. 43. Add the interface in the init function
  44. 44. addMouseListener( this );
  45. 45. addMouseMotionListener( this );
  46. 46. Additional MouseEvent Methods
  47. 47. Int getClickCount()           Return the number of mouse clicks associated with this event.
  48. 48. Int getX()           Returns the horizontal x position of the event relative to the source component.
  49. 49. Int getY()           Returns the vertical y position of the event relative to the source component.

×