130 likes | 354 Views
Applets. Applets. Design of Applets. Design of Applets. Sun wrote Java to be executable within a hosting application browser The applications are applets. An applet is downloaded to the local client application and run on the local java-enabled machine
E N D
Applets • Design of Applets
Design of Applets • Sun wrote Java to be executable within a hosting application • browser • The applications are applets. • An applet isdownloaded to the local client application and run on the local java-enabled machine • All ordinary capability except file output and database input or output is easily available
Design of Applets • An Applet is run inside of another application • Browser • AppletViewer • The programmer adds panels with their controls to browser’s content panel (the web page)
Design of Applets • There are five major methods in an Applet: • init () called when Applet starts • start () called when page is displayed • stop () called when user exits • destroy () called when resources are lost • paint () called when Applet is refreshed
Design of Applets • Sample code:public class Program4 extends JApplet • { • public void init () • { • getContentPane ( ).add(myPanel); • } • } • Where myPanel is the one of the panels containing controls
Design of Applets • Design of Hosting Web Page (use of Applet tag) • <body> • <applet code=myservlets.AdderApplet.class width=550 height=400> • <param name=host value= • "http://localhost:8080/user002/servlet/myservlets.AdderServlet"> • </applet> • </body> • Where host is an argument sent to the Applet from the web page
GUI Applications • Using a frame: public class Example extends JFrame { public Example ( ) { getContentPane( ).add(myPanel); } public static void main (String args [ ]) { Example example = new Example (“Example”); example.setTitle (aTitle); example.setSize (300, 250); example.setDefaultCloseOperation (3); example.setVisible(true); } • }
GUI Application as Applet • Init and handle events public class Example extends JApplet { public void init ( ) { getContentPane( ).add(myPanel); setTitle (aTitle); setSize (300, 250); setDefaultCloseOperation (3); setVisible(true); } • See ConvertTemperatures applet, and note change from application
Summary • Applets are a window system • One can also place windows in a frame and use them in an application • Applets significantly bring web-based applications to life by moving the work to the client machine • Error checking can be decentralized from the server to the clients