CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Introduction to Applets Course Lecture Slides 29 th July 2010
Applets Applets are small applications accessed on an Internet server, transported over the networks, automatically installed and run as part of a web document. At the client, applets get limited access to resources to be able to produce graphics, multimedia UI, run computations, etc. without risk of viruses or breaching data integrity.
Applets Applet Code Example: HelloWorld.java import java.awt.Graphics; import java.applet.Applet; public class HelloWorld extends Applet{ public void paint(Graphics g){ g.drawString("Hello World!", 120, 80); } Every applet must be a subclass of Applet.
Applets Web document: HelloWorld.html Applet example
Abstract Window Toolkit (AWT) The Abstract Window Toolkit (AWT) is Java's original platform-independent windowing, graphics, and user- interface widget toolkit. Applets interact with the user through the AWT, not through the console-based I/O classes. AWT is a part of Java Foundation Classes (JFC), the standard API for providing a graphical user interface (GUI) for a Java program.
Abstract Window Toolkit (AWT) AWT contains support for a window-based, graphical interface. AWT supports graphics through components (visible UI elements-buttons/ labels/ textfields) and containers (components that contain and organize other components). All containers inherit from the java.awt.Container base class, which itself inherits from java.awt.Component
Applets Do not have main() method Instead applet begins execution when the name of its class is passed to an applet viewer or to a network browser. Compile the applet in the same way as a normal java application) In the example, the paint() method defined by AWT and overridden by the applet helps to redraw output on screen.
Applets Applets are event-driven. User initiates interaction with an applet. These interactions are sent to the applet as “events” to which the applet must respond. Example: When the user clicks on a button in the applet window, a button-click event is generated.
Applets In your applet override a set of methods that help the browser or applet-viewer to interface to the applet and control its execution. Applet class defines – init(), start(), stop(), destroy() AWT Component class defines paint() method.
Applet Skeleton import java.awt.*; import java.applet.*; public class MyAppletSkeleton extends Applet { // browser calls init when applet is first loaded public void init( ){ /* initialization */ } // called second, after init. Also called whenever applet is restarted public void start( ){ /* start or resume execution */ } //called when the applet is stopped public void stop( ){ /* suspends execution */ } //called when applet is terminated. Last method executed public void destroy( ){ /* perform shutdown operations */ } //called when an applet’s window must be restored (redrawn) public void paint(Graphics g){ /* redisplay contents of window */ } }
Applet Skeleton import java.awt.*; import java.applet.*; public class MyAppletSkeleton extends Applet { // browser calls init when applet is first loaded public void init( ){ /* initialization */ } // called second, after init. Also called whenever applet is restarted. public void start( ){ /* start or resume execution */ } //called when the applet is stopped. public void stop( ){ /* suspends execution */ } //called when applet is terminated. Last method executed. public void destroy( ){ /* perform shutdown operations */ } //called when an applet’s window must be restored (redrawn) public void paint(Graphics g){ /* redisplay contents of window */ } }
Applets Further examples.
Get more info! Java docs: java.applet summary.htmlhttp://download-llnw.oracle.com/javase/1.5.0/docs/api/java/applet/package- summary.html Java docs: java.awt JFC