Applets IST 311 MIS 307 Applets A Java

  • Slides: 13
Download presentation
Applets IST 311 / MIS 307

Applets IST 311 / MIS 307

Applets A Java applet is a Java program that is embedded in an HTML

Applets A Java applet is a Java program that is embedded in an HTML document (i. e. , a Web page) Browser that executes an applet is called an applet container – Java 2 Software Development Kit (J 2 SDK) includes the appletviewer applet container – Some browsers do not support Java 2 directly Microsoft Internet Explorer requires a Java Plug-in

Applets Because applets execute under control of a browser, you do not write statements

Applets Because applets execute under control of a browser, you do not write statements to: – Instantiate the applet – Make it visible – Terminate its execution Applets do not have a title bar Applets do not have a constructor method – Methods which takes it’s place is init( )

Applets Security Restrictions Java security restrictions prevent destructive programs from damaging the system on

Applets Security Restrictions Java security restrictions prevent destructive programs from damaging the system on which the browser is running. – Not allowed to read from, write to, the file system of the computer. Prevents file damage and virus spreading – Not allowed to run programs on the browser’s computer. Prevent damage to local system.

Applets Security Restrictions – Not allowed to establish connections between user computer and any

Applets Security Restrictions – Not allowed to establish connections between user computer and any other computer except the server where the applets are stored. Prevents applet from connecting to another computer without the user’s knowledge.

Applets The Applet’s methods serve to define an interface between the applet and its

Applets The Applet’s methods serve to define an interface between the applet and its browser environment These methods are: – init( ): used in similar way as constructor; initialize actions during launch of applet – start( ): called when user returns to web page after surfing other pages; resume animation – stop( ): method is invoked when the user leaves the web page; might stop an animation

– destroyed( ): invoked when browser exits normally; release any resources used by applet;

– destroyed( ): invoked when browser exits normally; release any resources used by applet; usually won’t need to override this method Applet Life-Cycle Methods

Applets An Applet is a subclass of Panel – Which is a type of

Applets An Applet is a subclass of Panel – Which is a type of Container – A Container is a Component that can contain other Components Like JButtons, JText. Fields, JLabels, etc.

Applets import javax. swing. *; Import declaration indicates that the Applet class will be

Applets import javax. swing. *; Import declaration indicates that the Applet class will be used public class On. Off. Applet extends JApplet Use inheritance to extend JApplet class public void init( ) method is called once, automatically, when the applet is loaded into the JVM to initialize the applet’s interface and variables used

Applets No need to make window visible No need to handle closing window as

Applets No need to make window visible No need to handle closing window as this is handles by the browser No method main( )

Applets 1. Practice writing On. Off. Applet now. 2. Embed On. Off. Applet in

Applets 1. Practice writing On. Off. Applet now. 2. Embed On. Off. Applet in an html document.

Applets Because the JVM will execute this applet in a browser, you must create

Applets Because the JVM will execute this applet in a browser, you must create a small HTML file – Specify the name of the applet bytecode file – Specify the size of the panel in pixels – Specify the title of the HTML document

Applets <html> <head><title>On/Off Applet</title></head> <body> <applet code =“On. Off. Applet. class" width=400 height=300></applet> </body>

Applets <html> <head><title>On/Off Applet</title></head> <body> <applet code =“On. Off. Applet. class" width=400 height=300></applet> </body> </html>