Chapter 1 Java Applets
Introduction • Part of Java’s success comes from the fact that it is the first language specifically designed to take advantage of the power of the World-Wide Web • In addition to more traditional application programs, Java makes it possible to write small interactive programs called applets that run under the control of a web browser
• Applet – Program that runs in • Web browser (IE, Communicator) • appletviewer – a program designed to run an applet as a stand-alone program – Executes when HTML (Hypertext Markup Language) document containing applet is opened and downloaded – Can not run independently – normally run within a controlled environment (sandbox) – Used in internet computing. – Every browser implements security policies to keep applets from compromising system security Introduction
Applications vs. Applets • Similarities – Both applets and applications are Java programs – Since JFrame and JApplet both are subclasses of the Container class, all the user interface components, layout managers, and event-handling features are the same for both classes.
Applications vs. Applets • Differences – Applications are invoked from the static main method by the Java interpreter, and applets are run by the Web browser. • The Web browser creates an instance of the applet using the applet’s no-arg constructor and controls and executes the applet through the init, start, stop, and destroy methods. – Applets have security restrictions – Applications run in command windows whereas applets run in web browsers – Web browser creates graphical environment for applets, GUI applications are placed in a frame. – There is no main() method in an Applet.
Security Restrictions on Applets • Applets are not allowed to read from, or write to, the file system of the computer viewing the applets. • Applets are not allowed to run any programs on the browser’s computer. • Applets are not allowed to establish connections between the user’s computer and another computer except with the server where the applets are stored. • An applet cannot load libraries or define native methods. • It cannot read certain system properties
Applet class • From Component, an applet inherits the ability to draw and handle events • From Container, an applet inherits the ability to include other components and to have a layout manager control the size and position of those components • Every applet is implemented by creating a subclass of the Applet class
Life Cycle of Applet • An applet actually has a life cycle – It can initialize itself. – It can start running. – It can stop running. – It can perform a final cleanup, in preparation for being unloaded.
The Applet Class •When the applet is loaded, the Web browser creates an instance of the applet by invoking the applet’s no-arg constructor. •The browser uses the init, start, stop, and destroy methods to control the applet. •By default, these methods do nothing. To perform specific functions, they need to be modified in the user's applet so that the browser can call your code properly.
Browser Calling Applet Methods Browser invokes start() Destroyed Browser invokes destroy() Browser invokes stop() Loaded Initialized Browser invokes init() Started Stopped Created Browser creates the applet JVM loads the applet class Browser invokes stop() Browser invokes start()
The init() Method Invoked when the applet is first loaded and again if the applet is reloaded. A subclass of Applet should override this method if the subclass has an initialization to perform. The functions usually implemented in this method include creating new threads, loading images, setting up user-interface components, and getting string parameter values from the <applet> tag in the HTML page.
The start() Method Invoked after the init() method is executed; also called whenever the applet becomes active again after a period of inactivity (for example, when the user returns to the page containing the applet after surfing other Web pages). A subclass of Applet overrides this method if it has any operation that needs to be performed whenever the Web page containing the applet is visited. An applet with animation, for example, might use the start method to resume animation.
The stop() Method The opposite of the start() method, which is called when the user moves back to the page containing the applet; the stop() method is invoked when the user moves off the page. A subclass of Applet overrides this method if it has any operation that needs to be performed each time the Web page containing the applet is no longer visible. When the user leaves the page, any threads the applet has started but not completed will continue to run. You should override the stop method to suspend the running threads so that the applet does not take up system resources when it is inactive.
The destroy() Method Invoked when the browser exits normally to inform the applet that it is no longer needed and that it should release any resources it has allocated. A subclass of Applet overrides this method if it has any operation that needs to be performed before it is destroyed. Usually, you won't need to override this method unless you wish to release specific resources, such as threads that the applet created.
The JApplet Class •The Applet class is an AWT class and is not designed to work with Swing components. •To use Swing components in Java applets, it is necessary to create a Java applet that extends javax.swing.JApplet, which is a subclass of java.applet.Applet. •JApplet inherits all the methods from the Applet class. In addition, it provides support for laying out Swing components.
Writing Applets • Always extends the JApplet class, which is a subclass of Applet for Swing components. • Override init(), start(), stop(), and destroy() if necessary. By default, these methods are empty. • Add your own methods and data if necessary. • Applets are always embedded in an HTML page.
Java Applet Skeleton /* Program MyFirstApplet An applet that displays the text "I Love Java" and a rectangle around the text. */ import java.applet.*; import java.awt.*; public class MyFirstApplet extends JApplet { public void paint( Graphics graphic) { graphic.drawString("I Love Java",70,70); graphic.drawRect(50,50,100,30); } } Comment Import Statements Class Name Method Body
First Simple Applet // WelcomeApplet.java: Applet for //displaying a message import javax.swing.*; public class WelcomeApplet extends JApplet { /** Initialize the applet */ public void init() { add(new JLabel("Welcome to Java", JLabel.CENTER)); } }
First Simple Applet <html> <head> <title>Welcome Java Applet</title> </head> <body> <applet code = "WelcomeApplet.class" width = 350 height = 200> </applet> </body> </html>
1 // Fig. 3.6: WelcomeApplet.java 2 // A first applet in Java. 3 4 // Java core packages 5 import java.awt.Graphics; // import class Graphics 6 7 // Java extension packages 8 import javax.swing.JApplet; // import class JApplet 9 10 public class WelcomeApplet extends JApplet { 11 12 // draw text on applet’s background 13 public void paint( Graphics g ) 14 { 15 // call inherited version of method paint 16 super.paint( g ); 17 18 // draw a String at x-coordinate 25 and y-coordinate 25 19 g.drawString( "Welcome to Java Programming!", 25, 25 ); 20 21 } // end method paint 22 23 } // end class WelcomeApplet import allows us to use predefined classes (allowing us to use applets and graphics, in this case). extends allows us to inherit the capabilities of class JApplet. Method paint is guaranteed to be called in all applets. Its first line must be defined as above. A Simple Java Applet: Drawing a String
The <applet> HTML Tag <applet code=classfilename.class width=applet_viewing_width_in_pixels height=applet_viewing_height_in_pixels [archive=archivefile] [codebase=applet_url] [vspace=vertical_margin] [hspace=horizontal_margin] [align=applet_alignment] [alt=alternative_text] > <param name=param_name1 value=param_value1> </applet>
Passing Parameters to Applets <applet code = "DisplayMessage.class" width = 200 height = 50> <param name=MESSAGE value="Welcome to Java"> <param name=X value=20> <param name=Y value=20> alt="You must have a Java-enabled browser to view the applet" </applet>

Advanced Programming, Java Programming, Applets.ppt

  • 1.
  • 2.
    Introduction • Part ofJava’s success comes from the fact that it is the first language specifically designed to take advantage of the power of the World-Wide Web • In addition to more traditional application programs, Java makes it possible to write small interactive programs called applets that run under the control of a web browser
  • 3.
    • Applet – Programthat runs in • Web browser (IE, Communicator) • appletviewer – a program designed to run an applet as a stand-alone program – Executes when HTML (Hypertext Markup Language) document containing applet is opened and downloaded – Can not run independently – normally run within a controlled environment (sandbox) – Used in internet computing. – Every browser implements security policies to keep applets from compromising system security Introduction
  • 4.
    Applications vs. Applets •Similarities – Both applets and applications are Java programs – Since JFrame and JApplet both are subclasses of the Container class, all the user interface components, layout managers, and event-handling features are the same for both classes.
  • 5.
    Applications vs. Applets •Differences – Applications are invoked from the static main method by the Java interpreter, and applets are run by the Web browser. • The Web browser creates an instance of the applet using the applet’s no-arg constructor and controls and executes the applet through the init, start, stop, and destroy methods. – Applets have security restrictions – Applications run in command windows whereas applets run in web browsers – Web browser creates graphical environment for applets, GUI applications are placed in a frame. – There is no main() method in an Applet.
  • 6.
    Security Restrictions onApplets • Applets are not allowed to read from, or write to, the file system of the computer viewing the applets. • Applets are not allowed to run any programs on the browser’s computer. • Applets are not allowed to establish connections between the user’s computer and another computer except with the server where the applets are stored. • An applet cannot load libraries or define native methods. • It cannot read certain system properties
  • 7.
    Applet class • FromComponent, an applet inherits the ability to draw and handle events • From Container, an applet inherits the ability to include other components and to have a layout manager control the size and position of those components • Every applet is implemented by creating a subclass of the Applet class
  • 8.
    Life Cycle ofApplet • An applet actually has a life cycle – It can initialize itself. – It can start running. – It can stop running. – It can perform a final cleanup, in preparation for being unloaded.
  • 9.
    The Applet Class •Whenthe applet is loaded, the Web browser creates an instance of the applet by invoking the applet’s no-arg constructor. •The browser uses the init, start, stop, and destroy methods to control the applet. •By default, these methods do nothing. To perform specific functions, they need to be modified in the user's applet so that the browser can call your code properly.
  • 10.
    Browser Calling AppletMethods Browser invokes start() Destroyed Browser invokes destroy() Browser invokes stop() Loaded Initialized Browser invokes init() Started Stopped Created Browser creates the applet JVM loads the applet class Browser invokes stop() Browser invokes start()
  • 11.
    The init() Method Invokedwhen the applet is first loaded and again if the applet is reloaded. A subclass of Applet should override this method if the subclass has an initialization to perform. The functions usually implemented in this method include creating new threads, loading images, setting up user-interface components, and getting string parameter values from the <applet> tag in the HTML page.
  • 12.
    The start() Method Invokedafter the init() method is executed; also called whenever the applet becomes active again after a period of inactivity (for example, when the user returns to the page containing the applet after surfing other Web pages). A subclass of Applet overrides this method if it has any operation that needs to be performed whenever the Web page containing the applet is visited. An applet with animation, for example, might use the start method to resume animation.
  • 13.
    The stop() Method Theopposite of the start() method, which is called when the user moves back to the page containing the applet; the stop() method is invoked when the user moves off the page. A subclass of Applet overrides this method if it has any operation that needs to be performed each time the Web page containing the applet is no longer visible. When the user leaves the page, any threads the applet has started but not completed will continue to run. You should override the stop method to suspend the running threads so that the applet does not take up system resources when it is inactive.
  • 14.
    The destroy() Method Invokedwhen the browser exits normally to inform the applet that it is no longer needed and that it should release any resources it has allocated. A subclass of Applet overrides this method if it has any operation that needs to be performed before it is destroyed. Usually, you won't need to override this method unless you wish to release specific resources, such as threads that the applet created.
  • 15.
    The JApplet Class •TheApplet class is an AWT class and is not designed to work with Swing components. •To use Swing components in Java applets, it is necessary to create a Java applet that extends javax.swing.JApplet, which is a subclass of java.applet.Applet. •JApplet inherits all the methods from the Applet class. In addition, it provides support for laying out Swing components.
  • 16.
    Writing Applets • Alwaysextends the JApplet class, which is a subclass of Applet for Swing components. • Override init(), start(), stop(), and destroy() if necessary. By default, these methods are empty. • Add your own methods and data if necessary. • Applets are always embedded in an HTML page.
  • 17.
    Java Applet Skeleton /* ProgramMyFirstApplet An applet that displays the text "I Love Java" and a rectangle around the text. */ import java.applet.*; import java.awt.*; public class MyFirstApplet extends JApplet { public void paint( Graphics graphic) { graphic.drawString("I Love Java",70,70); graphic.drawRect(50,50,100,30); } } Comment Import Statements Class Name Method Body
  • 18.
    First Simple Applet //WelcomeApplet.java: Applet for //displaying a message import javax.swing.*; public class WelcomeApplet extends JApplet { /** Initialize the applet */ public void init() { add(new JLabel("Welcome to Java", JLabel.CENTER)); } }
  • 19.
    First Simple Applet <html> <head> <title>WelcomeJava Applet</title> </head> <body> <applet code = "WelcomeApplet.class" width = 350 height = 200> </applet> </body> </html>
  • 20.
    1 // Fig.3.6: WelcomeApplet.java 2 // A first applet in Java. 3 4 // Java core packages 5 import java.awt.Graphics; // import class Graphics 6 7 // Java extension packages 8 import javax.swing.JApplet; // import class JApplet 9 10 public class WelcomeApplet extends JApplet { 11 12 // draw text on applet’s background 13 public void paint( Graphics g ) 14 { 15 // call inherited version of method paint 16 super.paint( g ); 17 18 // draw a String at x-coordinate 25 and y-coordinate 25 19 g.drawString( "Welcome to Java Programming!", 25, 25 ); 20 21 } // end method paint 22 23 } // end class WelcomeApplet import allows us to use predefined classes (allowing us to use applets and graphics, in this case). extends allows us to inherit the capabilities of class JApplet. Method paint is guaranteed to be called in all applets. Its first line must be defined as above. A Simple Java Applet: Drawing a String
  • 21.
    The <applet> HTMLTag <applet code=classfilename.class width=applet_viewing_width_in_pixels height=applet_viewing_height_in_pixels [archive=archivefile] [codebase=applet_url] [vspace=vertical_margin] [hspace=horizontal_margin] [align=applet_alignment] [alt=alternative_text] > <param name=param_name1 value=param_value1> </applet>
  • 22.
    Passing Parameters toApplets <applet code = "DisplayMessage.class" width = 200 height = 50> <param name=MESSAGE value="Welcome to Java"> <param name=X value=20> <param name=Y value=20> alt="You must have a Java-enabled browser to view the applet" </applet>