Friday, 30 August 2019

Java Applet Hello World Example | Java Applet Hello World Program

                  In this post, we will see Java Applet Hello World Example | Java Applet Hello World Program. 


Find details of Life Cycle of Applet in this link: https://www.comrevo.com/2018/10/life-cycle-of-applet.html.

Explanation: 
                  Applet program does not have main() method. Hence, it can not be used for creating stand-alone application. It is to be used with other applications like webpages created by JSP, Servlet.
                  We need to extend class Applet given by package java.applet.
                  We can override init(), start(), stop(), destroy() methods given by Applet class. We may also need paint() method to redraw applet. paint() method is given by Component class which is given by java.awt package. paint() method accepts Graphics class object as parameter.

                  Go through the following programs:

Program (HelloWorldApplet.java)

import java.applet.*;
import java.awt.*;

public class HelloWorldApplet extends Applet
{
  public void paint(Graphics g)
  {
    g.drawString("Hello World", 100, 100);
  }
}

Program (HelloWorldApplet.html)

<html>
<head><title>Hello World Applet</title></head>
<body>
<applet code = "HelloWorldApplet.class" width = "300" height = "300">
If your browser is Java enabled, then only you can see Applet in your browser. If it is not Java enabled, then you need to add Java plug-ins in your browser or use appletviewer to view applet.
</applet>
</body>  
</html>
  


 How to run from terminal
parag@parag-Inspiron-N4010:~/Desktop/programs/applet$ javac HelloWorldApplet.java
parag@parag-Inspiron-N4010:~/Desktop/programs/applet$ appletviewer HelloWorldApplet.html
  

Output:
 

Find Life Cycle of Applet in following link:
https://www.comrevo.com/2018/10/life-cycle-of-applet.html .

No comments:

Post a comment