Creating an executable applet in java

sarang ravate
3 min readApr 12, 2019

--

What is java applet?

An applet is a small Internet-based program written in Java, a programming language for the Web, which can be downloaded by any computer.

Types of applet?

1)Stand alone applet

2)Local applet

Creating an executable applet

Executable applet is nothing but the .class file of applet, which is obtained by compiling the source code of the applet. Compiling the applet is exactly the smae as compiling an application using following command.

javac appletname.java

The compiled output file called appletname.class should be placed in the smae directory as the source file.

The following are the steps that are involved in developing and testing and applet.

  1. Buliding an applet code(.java file)
  2. Creating an executable applet(.class file)
  3. Designing a web page using HTML
  4. Preparing <Applet Tag>
  5. Incorporating <Applet> tag into the web page.
  6. Creating HTMl file.
  7. Testing the applet code.

Format for creating Applet

import java.awt.*;

import java.applet.*;

___________________

___________________

public class applet classname extends Applet

{

……………………………

…………………………..statements

…………………………..

…………………………..

public void paint (Graphics g)

{

……………………..

……………………..//Applet operations code

…………………….

}

………………….

…………………

}

How to add applet in html

We have to use it’s basic programming

First we give the heading

__________________________________________

<Head>

<Title> Welcome to Java Applet </Title>

</Head>

__________________________________________

When heading section over body section came

_______________________________________________________________ <Body>

<Center>

<H1>

Welcome to Java >

</H1>

</Center>

<BR>

<APPLET — — ->

</APPLET>

</Body>

_______________________________________________________________

Applet tag

The <Applet…< tag supplies the name of the applet to be loaded and tells the browser how much space the applet requires. The ellipsis in the tag <Applet…> indicates that it contains certain attributes that must specified.

<Applet
code = Hellojava.class
width = 400
Height = 200 >
</Applet>

Running java applet

To execute an applet with an applet viewer, you may also execute the HTML file in which it is enclosed, eg.

c:\>appletviewer HellowApplet.html

following are the examples of java applet

  1. //First.java
  2. import java.applet.Applet;
  3. import java.awt.Graphics;
  4. public class First extends Applet{
  5. public void paint(Graphics g){
  6. g.drawString(“Hellow world”,150,150);
  7. }
  8. }

HelloApplet.html

  1. <html>
  2. <body>
  3. <applet code=”First.class” width=”300" height=”300">
  4. </applet>
  5. </body>
  6. </html>

the output given as follow

note : We can run applet by another method i.e given as follow

Simple example of Applet by appletviewer tool:

To execute the applet by Appletviewer tool, create an applet that contains applet tag in comment and compile it. After that run it by: appletviewer First.java. Now Html file is not required but it is for testing purpose only.

  1. //First.java
  2. import java.applet.Applet;
  3. import java.awt.Graphics;
  4. public class First extends Applet{
  5. public void paint(Graphics g){
  6. g.drawString(“welcome to applet”,150,150);
  7. }
  8. }
  9. /*
  10. <applet code=”First.class” width=”300" height=”300">
  11. </applet>
  12. */

To execute the applet by appletviewer tool, write in command prompt:

following are the commands for execute appletviewer tool

c:\>javac First.java
c:\>appletviewer First.java

--

--