Pin It

How to run a simple Applet in Java ?.

Introduction
This blog post will guide you about the basics of Java Applet. This tutorial will teach you how to run a simple Applet in Java. So, What is an Applet in Java ? An Applet is a small java program that runs in a Java enabled web browser. Java Applet is a small piece of java code that is embedded into HTML page, which gets executed when the HTML page loads into the browser. Applet runs on the Client machine, while Servlet runs on Server side. They are rarely used to make website dynamic. Its secure and platform independent. They require JVM and Java enabled web browser such as IE, Mozilla etc, to get execute on client machine. Applets are embedded into HTML documents and has various methods in java.applet.Applet, which governs its life-cycle.

Program to demonstrate how to run a simple Applet in Java

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

/*
<applet code="SimpleApplet.class" 
         height="250" width="250">
</applet>
*/
public class SimpleApplet extends Applet {
  public void init() {
  }

  public void start() {
  }

  public void stop() {
  }

  public void destroy() {
  }

  public void paint(Graphics g) {
    g.setColor(Color.RED);
    g.drawString("Welcome to Hubberspot!", 50, 100);
  }
}

SimpleApplet.html file to run applet on Web Browser

<html>
  <head>
       <title>Simple Applet</title>
  </head>

  <body>
       <applet 
              code="SimpleApplet.class" 
              height="250" 
              width="250">
       </applet>
  </body>
</html>

Output of program :



Running an Applet in Java
Java Applet are compiled by javac command. It can be run by two ways, but not with using java command. It can be run by using either java tool- appletviewer or by loading Applet into web browser using applet tag in html. Let us look into more details below :

1) Using Web Browser :
To view the applet in a web browser, you will require a java enabled web browser. To enable java in the browser, go to browser advanced setting an enable java. The following steps should be followed :

a) Create an HTML file as above, containing the APPLET tag.
b) Compile the applet source code using javac.
c) Open the html file in the web browser.

2) Using appletviewer :
It is a java tool provided to view applets. It is like a small browser provided to have a preview of applet as they would look in browser.It understands APPLET tag and used in the creation phase. The APPLET tag should be written in source code file enclosing in comments. The following steps should be followed:

a) Write HTML APPLET tag in comments in the source file.
b) Compile the applet source code using javac.
c) Use appletviewer ClassName.class to view the applet.
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot