Applets in Java |
|
|
An applet is a small program that is intended to be embedded inside another application such as a browser. The JApplet class must be the superclass of any applet that is to be embedded in a Web page or viewed by the Java Applet Viewer (appletviewer.exe). The JApplet class provides a standard interface between applets and their environment. JApplet hierarchy is as follows: java.lang.Object java.awt.Component java.awt.Container java.awt.Panel java.applet.Applet javax.swing.JApplet Applets Structure - There is no main method. - Two methods that are called automatically- init() and paint() - The init method initializes variables and objects; if you don't have one you will inherit one from the JApplet class. - Use paint to draw screen - A lot of methods exist in JApplet class so the "extends" keyword inherits everything that the class has. In the above example JApplet is parent class and shellapplet is the subclass so use the keyword "extends" to create inheritance. - You have to import JApplet and java.awt.Graphics (abstract windowing toolkit) to get Graphics to paint. - All applets must inherit JApplet - Use the "super" keyword in subclass to invoke method in the superclass. - super.paint( g ); this says uses the paint method from JApplet in my paint class and I'm not adding anything to it. Example: import javax.swing.JApplet; import java.awt.Graphics; public class AnyApplet extends JApplet { // declare variables here public void init( ) { // data initialization goes here } public void paint( Graphics g ) { super.paint( g ); // your code goes here } } - The window gets drawn by invoking the paint class of the applet, which calls the superclass paint which draws the actual window you've given the size for. - Other applet methods - repaint() – repaints when the window which the applet resides re-gains focus
- stop() called when applet is no longer visible. Signature: public void stop(). Stopt is called by the browser or applet viewer to inform this applet that it should stop its execution. It is called when the Web page that contains this applet has been replaced by another page, and also just before the applet is to be destroyed. A subclass of JApplet should override this method if it has any operation that it wants to perform each time the Web page containing it is no longer visible. The implementation of stop method provided by the JApplet class does nothing.
- start() called when the applet is visible. The signature for the start method is as follows: public void start(). Start is called by the browser or applet viewer to inform this applet that it should start its execution. It is called after the init method and each time the applet is revisited in a Web page. A subclass of JApplet should override this method if it has any operation that it wants to perform each time the Web page containing it is visited. The implementation of start method provided by the JApplet class does nothing.
- estroy() when hosting window is closed (exit). Tha signature is as follows: public void destroy(). Destory method is called by the browser or applet viewer to inform this applet that it is being cultivated and that it should destroy any resources that it has allocated. The stop method will always be called before destroy. A subclass of JApplet should override this method if it has any operation that it wants to perform before it is destroyed. The implementation of destory method provided by the JApplet class does nothing.
|
|
|
|
|
|
|
|
|