260 likes | 1.12k Views
Applet Architecture And Applet Class. Presented by: Judy A Pasi Ibansara Warlapih Jesson D Thangkhiew. What is an Applet ? . Applets are Java programs that may be embedded into HTML documents. The applet runs in the page as soon as it has been downloaded.
E N D
Applet Architecture AndApplet Class Presented by: Judy A Pasi IbansaraWarlapih Jesson D Thangkhiew
What is an Applet ? • Applets are Java programs that may be embedded into HTML documents. The applet runs in the page as soon as it has been downloaded. • More specifically Java applets are Java programs that inherit the Applet (AWT) or JApplet class (Swing).
1 . Request for Mypage.html . MyApplet.class 3 executeshere 2 . Sends Mypage.html and MyApplet.class Applet Architecture Applet Program MyApplet.class Webpage with applet Mypage.html Client Web Server
Creating an Applet • To create an Applet, a subclass of the Applet class (AWT) or JApplet class(Swing) should be created. • Example, public class MyApplet extends JApplet { ……… }
Methods of the Applet Class • init Method • Similar to a constructor • Called when applet is first loaded into the browser. • Not called every time the applet is executed.(except in certain browser versions) • start Method • The start method starts the execution of the applet – Called immediately after init. • Reinvoked each time the user returns to the page with the applet. • Used to start animation threads • paint Method • Called by the browser after init and start, and again whenever the browser redraws the screen, (typically when part of the screen has been obscured and then re exposed) • This method is where user-level drawing is placed
Methods of the Applet Class (contd.) • stop Method • Most applets that override the start should also override the stop method. • Called when browser leaves the page • Used to stop animation threads • The stop method suspends the applet's execution. • destroy Method Invoked when browser exits normally • used for applets that need to release additional resources.
Begin init() Born ( Load applet ) stop() start() Stopped Idle Running Display destroy() paint() start() End Dead Destroyed Life Cycle of An Applet
public void init() • { • // called when applet is first created • } • public void start() • { • // called when applet is started and then restarted • } • public void paint(Graphics G) • { • // invoked when screen needs to be drawn or redrawn • }
public void paint(Graphics G) • { • // invoked when screen needs to be drawn or redrawn • } • public void stop() • { • //called when the user leaves the page containing the applet
public void destroy() • { • // called when applet is permanently destroyed • }
A “Hello World” Applet import java.awt.*; import javax.swing.*; public class HelloApplet extends JApplet { public void paint(Graphics g) { super.paint(g); g.drawString(“Hello World”, 25, 25); // draw string at x=25 and y=25 } }
Adding Applets to an HTML Document <applet code=“MyApplet.class” height=“200” width=“100”> </applet>