We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7
SUBJECT: Java TOPIC: Applets
WRITTEN BY: SRINIVAS YADLAPATY, MTech,(PHD) in
Computer Science Engineering --------------------------------------------------------------------------------- What is Applet? An applet is a Java program that can be embedded into a web page. It runs inside the web browser and works at client side. An applet is embedded in an HTML page using the APPLET or OBJECT stag and hosted on a web server. Applets are used to make the web site more dynamic and entertaining. Later part of JDK i.e., after 1.8 version onwards Applets are not supported and so we cannot run appletviewer command. Important points : 1. All applets are sub-classes (either directly or indirectly) of java.applet.Applet class. 2. Applets are not stand-alone programs. Instead, they run within either a web browser or an applet viewer. JDK provides a standard applet viewer tool called applet viewer. 3. In general, execution of an applet does not begin at main() method. 4. Output of an applet window is not performed by System.out.println(). Rather it is handled with various AWT methods, such as drawString(). Life cycle of an applet :
It is important to understand the order in which the various methods shown
in the above image are called. When an applet begins, the following methods are called, in this sequence: 1. init( ) 2. start( ) 3. paint( ) When an applet is terminated, the following sequence of method calls takes place: 1. stop( ) 2. destroy( ) Let’s look more closely at these methods. 1. init( ) : The init( ) method is the first method to be called. This is where you should initialize variables. This method is called only once during the run time of your applet. 2. start( ) : The start( ) method is called after init( ). It is also called to restart an applet after it has been stopped. Note that init( ) is called once i.e. when the first time an applet is loaded whereas 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( ). 3. paint( ) : The paint( ) method is called each time an AWT-based applet’s output must be redrawn. This situation can occur for several reasons. For example, the window in which the applet is running may be overwritten by another window and then uncovered. Or the applet window may be minimized and then restored. paint( ) is also called when the applet begins execution. Whatever the cause, whenever the applet must redraw its output, paint( ) is called. 1. The paint( ) method has one parameter of type Graphics. This parameter will contain the graphics context, which describes the graphics environment in which the applet is running. This context is used whenever output to the applet is required. Note: This is the only method among all the methods mention above, which is parametrised. It’s prototype is public void paint(Graphics g) where g is an object reference of class Graphic. 2. 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. 3. 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( ). There are two standard ways in which you can run an applet : 1. Executing the applet within a Java-compatible web browser. 2. Using an applet viewer, such as the standard tool, applet-viewer. An applet viewer executes your applet in a window. This is generally the fastest and easiest way to test your applet. 1. Using java enabled web browser : To execute an applet in a web browser we have to write a short HTML text file that contains a tag that loads the applet. We can use APPLET or OBJECT tag for this purpose. Using APPLET, here is the HTML file that executes HelloWorld : <applet code="HelloWorld" width=200 height=60> </applet> The width and height statements specify the dimensions of the display area used by the applet. The APPLET tag contains several other options. After you create this html file, you can use it to execute the applet. NOTE : Chrome and Firefox no longer supports NPAPI (technology required for Java applets). 2. Using appletviewer : This is the easiest way to run an applet. To execute HelloWorld with an applet viewer, you may also execute the HTML file shown earlier. For example, if the preceding HTML file is saved with RunHelloWorld.html,then the following command line will run HelloWorld : 3. appletviewer RunHelloWorld.html Program: // Mouse event and MouseListener /* <applet code="MouseeEvent.class" width=400 height=400> </applet> */ import java.awt.event.*; import javax.swing.*; public class MouseeEvent extends JApplet implements MouseListener,MouseMotionListener{ public void init() { addMouseListener(this); addMouseMotionListener(this); } public void mouseClicked(MouseEvent e) { showStatus("Mouse has been clicked at:"+e.getX()+","+e.getY()); } public void mouseEntered(MouseEvent e) { showStatus("Mouse has been entered at:"+e.getX()+","+e.getY()); } public void mouseExited(MouseEvent e) { showStatus("Mouse has been exited at"+e.getX()+","+e.getY()); } public void mousePressed(MouseEvent e) { showStatus("Mouse pressed at"+e.getX()+","+e.getY()); } public void mouseReleased(MouseEvent e) { showStatus("Mouse released at"+e.getX()+","+e.getY()); } public void mouseDragged(MouseEvent e) { showStatus("Mouse dragged at"+e.getX()+","+e.getY()); } public void mouseMoved(MouseEvent e) { showStatus("Mouse moved at"+e.getX()+","+e.getY()); } } Note: All the above methods should be defined. If any one out of those are undefined when we compile we will get an error. To overcome this that is if we want to go with any one method are so then we have to go with an adapter class. Program: // Adapter demonstration. import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="AdapterDemo" width=200 height=100> </applet> */ public class AdapterDemo extends Applet { public void init() { addMouseListener(new EHMouseAdapter(this)); addMouseMotionListener(new EHMouseMotionAdapter(this)); }} class EHMouseAdapter extends MouseAdapter { AdapterDemo ad; public EHMouseAdapter(AdapterDemo ad) { this.ad=ad; } // Handle mouse clicked. public void mouseClicked(MouseEvent me) { ad.showStatus("Mouse clicked"); } } class EHMouseMotionAdapter extends MouseMotionAdapter { AdapterDemo ad; public EHMouseMotionAdapter(AdapterDemo ad) { this.ad=ad; } // Handle mouse dragged. public void mouseDragged(MouseEvent me) { ad.showStatus("Mouse dragged"); } } Program: // Inner class demonstration. import java.applet.*; import java.awt.event.*; /* <applet code="InnerClassDemo" width=200 height=100> </applet> */ public class InnerClassDemo extends Applet{ // outer class started public void init(){ addMouseListener(new MyMouseAdapter()); } // init() is closed but not outer class class MyMouseAdapter extends MouseAdapter{ // inner class started public void mousePressed(MouseEvent me){ showStatus("Mouse pressed"); } // mousePressed() closed } // inner class closed } // outer class closed Program: // Key events and KeyListener. /* <applet code="KeyTest" width=400 height=400> </applet> */ import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class KeyTest extends Applet implements KeyListener { public void init() { Label lab=new Label("Enter characters:"); add(lab); TextField tf=new TextField(20); add(tf); tf.addKeyListener(this); } public void keyPressed(KeyEvent e) { System.out.println("CSE"); }