Download Java Programming: Understanding Java Applets and Their Life Cycle and more Slides Java Programming in PDF only on Docsity!
Java
Java Applets and Applications
- (^) Java applications
- (^) Run in stand-alone mode
- (^) No additional software required (such as a Web browser)
- (^) Java applets
- (^) Compiled Java class files
- (^) Run within a Web browser (or an appletviewer)
- (^) Loaded from anywhere on the Internet
- (^) security restrictions!
- (^) Bytecode verification
- Forces loaded Java applets to undergo a rigorous set of checks in order to run on the local system
- (^) The verifier checks each bytecode before it is executed to make sure that it is not going to perform an illegal operation
- (^) Client-side precautions
- (^) Most Web browsers preclude Java applets from doing file access or communicating with any computer on the Internet other than the computer that the applet was loaded from
- Enforced by the client Web browser (or other applet loader) but done by a part of the Java runtime engine known as the class loader
First Java Applet
import java.awt.*;
import java. applet.*;
public class simple applet extends Applet
public void paint (Graphics g)
g. drawString(“first applet”,20,20);
Applet’s Life Cycle
- (^) Each applet has four major events in its lifetime:
- (^) Initialization --- init()
- (^) Starting --- start()
- (^) Painting --- paint(Graphics)
- (^) Stopping --- stop()
- (^) Destroying --- destroy()
- (^) init ( ) method:
- (^) Called when applet is loaded onto user’s machine.
- (^) To initialise the Applet.
- start ( ) method:
- (^) Called when applet becomes visible (page called up).
- (^) Called every time applet becomes visible.
- (^) stop ( ) method:
- (^) Called when applet becomes hidden (page loses focus).
- (^) This method can be called multiple times in the cycle of an Applet.
- (^) destroy ( ) method:
- (^) Guaranteed to be called when browser shuts down.
- (^) This method is called only once in the life cycle of the applet when applet is destroyed.
- (^) Running the applet
- (^) Compile
- (^) javac WelcomeApplet.java
- (^) If no errors, bytecodes stored in WelcomeApplet.class
- (^) Create an HTML file
- (^) Loads the applet into appletviewer or a browser
- (^) Ends in .htm or .html
- (^) To execute an applet
- (^) Create an HTML file indicating which applet the browser (or appletviewer ) should load and execute
THE HTML APPLET TAG
- (^) You can only run an applet in an HTML page
- (^) The HTML looks something like this:
import java.applet.; import java.awt.; public class FontApplet extends Applet { public void paint (Graphics g) { String fontName = getParameter ("font"); int fontSize = Integer.parseInt (getParameter("size")); Font f = new Font (fontName, Font.BOLD, fontSize); g.setFont (f); g.drawString("Welcome to CS423", 25, 50); } } PASSING PARAMETERS :
ATTRIBUTES
CODE
- (^) Designates the filename of the Java class file to load
- (^) Filename interpreted with respect to directory of current HTML page (default) unless CODEBASE is supplied (^) WIDTH and HEIGHT
- (^) Specifies area the applet will occupy
- (^) Values can be given in pixels or as a percentage of the browser window (width only). (^) ALIGN It specifies the alignment of the applet. The possible values are left,right,top,bottom,middle,baseline etc. (^) VSPACE It specifies the space in pixels above and below the applet.
GRAPHICS
(^) drawString(string, left, bottom) Draws a string in the current font and color with the bottom left corner of the string at the specified location. (^) drawRect(left, top, width, height) Draws the outline of a rectangle (1-pixel border) in the current color (^) fillRect(left, top, width, height) Draws a solid rectangle in the current color (^) drawLine(x1, y1, x2, y2) Draws a 1-pixel-thick line from (x1, y1) to (x2, y2) (^) drawOval, fillOval Draws an outlined and solid oval, where the arguments describe a rectangle that bounds the oval.
(^) drawPolygon, fillPolygon Draws an outlined and solid polygon whose points are defined by arrays or a Polygon (a class that stores a series of points) By default, polygon is closed; to make an open polygon use the drawPolyline method (^) drawImage Draws an image Images can be in JPEG or GIF (including GIF89A) format. (^) setColor, getColor Specifies the foreground color prior to drawing operation By default, the graphics object receives the foreground color of the window AWT has 16 predefined colors (Color.red, Color.blue, etc.)
GRAPHICS FONTS
(^) setFont, getFont
- (^) Specifies the font to be used for drawing text
- (^) Determine the size of a character through FontMetrics (in Java 2 use LineMetrics)
- (^) Setting the font for the Graphics object does not persist to subsequent invocations of paint
- (^) Set the font of the window (I.e., call the applet’s setFont method) for permanent changes to the font
- (^) In JDK 1.1, only 5 fonts are available: Serif (aka TimesRoman), SansSerif (aka Helvetica), Monospaced (aka Courier), Dialog, and DialogInput
GRAPHICS BEHAVIOR
(^) Browser calls repaint method to request redrawing of applet
- (^) Called when applet first drawn or applet is hidden by another window and then reexposed. repaint() update(Graphics g) paint(Graphics g) “Sets flag” Clears screen , calls paint