Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Introduction to Java Applet

This article will let you get started with concepts of Applet. We will also discuss some small applet application.

Actually Applets are great way of using the power of java over web. Before starting with programming section let’s get some idea about Applet and why are they so useful in web.

What is Applet?

An applet is a program which is written in Java and can be easily included in HTML.Actually applet run on client side using the browser JVM. To run an applet it is necessary to have java installed.They can be included in HTML files.The tag used for adding them is .We will discuss it later in the discussion.

Uses of Applet

There are too many benefits of Applet, some of which are:

  1. Since you write applet in Java so applet can perform various things which cannot be done by HTML alone.The main benefit of Applet is that it can talk to your browser via Javascript.It can take input from user ,extract information and then pass it on to your browser for displaying the results.This makes it very useful.
  2. Applet can be used to make games,interactive teaching,quiz and many more thing
  3. You can even perform delicate file operation on user computer after obtaining user permission.That is user need to allow the applet to run with high permission otherwise applet will be denied from access(Done for security purpose).
  4. Applet can run on multiple platforms

Disadvantage of Applet

  1. It cannot run without Java plugin installed on client machine
  2. It is really difficult to realize if a given applet can be trusted or not

Security Risk in Applet

You must never allow applet from untrusted sites.This is because the applet may contain malicious codes which may infect your computer.Only allow those applets which are from trusted sites

Now when we know a bit about applet,then we can start with applet programming.Actually its really easy if you are comfortable with Java.We will discuss on making applet and multiple ways of running it on browser.

Listing 1: Applet Program

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

public class AppletProg extends Applet {
	  
	  public void init()
	  {
		  System.out.println("Our applet has initialized");
	  }
	 
	  public void start()
	  {
		  System.out.println("Our applet has started");
	  }
	  
	  public void stop()
	  {
		  System.out.println("Our applet has stopped");
	  }
	 
	  public void destroy()
	  {
		  System.out.println("Our applet object has been destroyed");
	  }
	  
	  public void paint(Graphics g) {
	    g.drawString("Hello, world!", 20,10);
	  }
	}

Applets have mainly 5 methods in its lifecycle.We discuss each one by one

  1. init : This method is for applet initialization like for variable initializing etc
  2. start :This method is called when applet is first displayed on screen.
  3. paint : It is the most important method.It is used to draw on applet output.We can draw several shapes using the Graphics object passed as an argument in paint method.
  4. stop : It is called when browser leave page where applet is embedded
  5. destroy : It frees up all resource used by applet and hence clean up the memory.It is called before applet is unloaded.

In the program we have extended the class by JApplet so as to include the functionality of Applets.

Listing 2: Applet HTML Program

<HTML>
<HEAD>
<TITLE>HelloWorld.html</TITLE>
</HEAD>
<BODY>
<H1>Applet</H1>
<APPLET code="AppletProg.class" WIDTH="200" HEIGHT="100">
</APPLET>
</BODY>
</HTML>

Actually there are 2 ways of running an applet.

  1. You may use the appletviewer to view your applets directly.
  2. In the program we have made a html program in which we added a Applet tag .The Applet contain the code from the Java program ie print Hello World.We have also defined the sizes of applet

So here we have understood some basic usage of Applet .We also discussed simple applet programs.This is all for this Article.See you soon with a new and exciting article



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login