Creating an executable applet in java
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.
- Buliding an applet code(.java file)
- Creating an executable applet(.class file)
- Designing a web page using HTML
- Preparing <Applet Tag>
- Incorporating <Applet> tag into the web page.
- Creating HTMl file.
- 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
- //First.java
- import java.applet.Applet;
- import java.awt.Graphics;
- public class First extends Applet{
- public void paint(Graphics g){
- g.drawString(“Hellow world”,150,150);
- }
- }
HelloApplet.html
- <html>
- <body>
- <applet code=”First.class” width=”300" height=”300">
- </applet>
- </body>
- </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.
- //First.java
- import java.applet.Applet;
- import java.awt.Graphics;
- public class First extends Applet{
- public void paint(Graphics g){
- g.drawString(“welcome to applet”,150,150);
- }
- }
- /*
- <applet code=”First.class” width=”300" height=”300">
- </applet>
- */
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