150 likes | 162 Views
Applets. Two Types of Applets. It is important to state at the outset that there are two varieties of applets .
E N D
Two Types of Applets • It is important to state at the outset that there are two varieties of applets. • The first are those based directly on the Applet class. These applets use the Abstract Window Toolkit (AWT) to provide the graphic user interface (or use no GUI at all). This style of applet has been available since Java was first created. • The second type of applets are those based on the Swing class JApplet. Swing applets use the Swing classes to provide the GUI. Swing offers a richer and often easier-to-use user interface than does the AWT. Thus, Swing-based applets are now the most popular. • Thus, both AWT- and Swing-based applets are valid.
Applet Basics • Applets are not stand-alone programs. Instead, they run within either a web browser or an applet viewer. The illustrations shown are created with the standard applet viewer, called appletviewer, provided by the JDK. But we can use any applet viewer or browser we like. • Execution of an applet does not begin at main( ).Instead, execution of an applet is started and controlled with an entirely different mechanism. • To use Swing components in Java applets, you need to create a Java applet that extends javax.swing.JApplet, which is a subclass of java.applet.Applet.
Applet Basics • To use an applet, it is specified in an HTML file. One way to do this is by using the APPLET tag. • The applet will be executed by a Java-enabled web browser when it encounters the APPLET tag within the HTML file. • To run an applet from a browser, you need to create an HTML file with the <applet>tag. Here is an example of such a comment: /* <applet code="MyApplet" width=200 height=60> </applet> */ <applet>is the tag name, and code, width, and height are attributes. The width and height attributes specify the rectangular viewing area of the applet.
Applet Life-Cycle Methods • Applets are actually run from the applet container, which is a plug-in of a Web browser. • The Applet class contains the init(), start(), stop(), and destroy() methods, known as the life-cycle methods. • These methods are called by the applet container to control the execution of an applet. They are implemented with an empty body in the Applet class. So, they do nothing by default. • You may override them in a subclass of Applet to perform desired operations.
An Applet Skeleton • init( ) • The init method is invoked after the applet is created. • This method is called only once during the run time of your applet. • start( ) • The start( ) method is called after init( ). It is also called to restart an applet after it has been stopped. Whereas init( ) is called once—the first time an applet is loaded—start( ) is called each time an applet’s HTML document is displayed onscreen. So, if a user leaves a web page and comes back, the applet resumes execution at start( ).
An Applet Skeleton • stop( ) • The stop( ) method is called when a web browser leaves the HTML document containing the applet—when it goes to another page, for example. When stop( ) is called, the applet is probably running. • You should use stop( ) to suspend threads that don’t need to run when the applet is not visible. You can restart them when start( ) is called if the user returns to the page. • destroy( ) • The destroy( ) method is called when the environment determines that your applet needs to be removed completely from memory. At this point, you should free up any resources the applet may be using. • The stop( ) method is always called before destroy( ).
An Applet Skeleton // An Applet skeleton. import javax.swing.JApplet; /* <applet code="AppletSkel" width=300 height=100> </applet> */ public class AppletSkel extends JApplet { // Called first. public void init() { // initialization } /* Called second, after init(). Also called whenever the 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. This is the last method executed. */ public void destroy() { // perform shutdown activities } }
Key Event • Key events enable the use of the keys to control and perform actions or get input from the keyboard. • A key event is fired whenever a key is pressed, released, or typed on a component. • Java provides the KeyListener interface to handle key events. • Every key event has an associated key character or key code that is returned by the getKeyChar() or getKeyCode() method in KeyEvent. • Only a focused component can receive KeyEvent. To make a component focusable, set its is Focusable property to true. setFocusable(true)
The MouseEvent Class • A mouse event is fired whenever a mouse is pressed, released, clicked, moved, or dragged on a component. • Java provides two listener interfaces, MouseListener and MouseMotionListener, to handle mouse events, • Two commonly used methods in this class are getX( ) and getY( ). These return the X and Y coordinates of the mouse within the component when the event occurred. Their forms are shown here: intgetX( ) intgetY( ) • The getClickCount( ) method obtains the number of mouse clicks for this event. Its signature is shown here: intgetClickCount( )
The simplest possible applet MyApplet.java import javax.swing.JApplet; public class MyApplet extends JApplet { } MyApplet.html <applet code=“MyApplet.class” width=200 height=100> </applet>
Running the applet • Compile • javac MyApplet.java • If no errors, bytecodes stored in MyApplet.class • Create an HTML file • ends in .htm or .htm • To execute an applet • appletviewer