An applet is a program written in the Java programming language that can be included in an HTML page or can be test viewed in an Applet viewer. An applet includes all the features provided by a Java Development Kit. But since an applet operates from a client’s computer, accessing user hard drive information is disabled for security purposes.
Open a Notepad document in Windows XP, with a file name of ‘demoApp.java’ (a Java file should always be named with .java extension)
Type the text below in Notepad (text specified after ‘//’ is for understanding and is not part of the program).
import java.awt.*; //Imports java advanced window toolkit package into the current program.
import java.applet.Applet; //Imports java applet package into the program.
//the below line is commented so that the compiler ignores it, but is essential to view the //applet using applet viewer so do not remove it. It specifies the file name, the window’s //height and width.
//<applet code=demoApp.class width=400 height=400>
//an applet class should always be declared under public and class name should be the same
//as the filename.
public class demoApp extends Applet
{
public void paint(Graphics g)
{
g.drawString("hey it’s my first applet",30,40);
//draws the string mentioned at x co-ordinates 30 and y co-ordinates 40
}
}// an interesting fact about applets is that they do not contain main class
now save the file and click start button -> run -> type cmd and click ok
now change your directory to the folder where the file is saved
For Ex: D:\>cd folder path
type ‘javac demoApp.java’
type ‘appletviewer demoApp.java’
this is it the message “hey it’s my first applet” will be displayed in a window with the height and width as 400.
view the gallery for a pictorial instruction.
for information about implementing applets: http://java.sun.com/applets/
Tips
1. Practice basic programs first and then keep adding functions to your program. Once your confident and feel comfortable with the format, you can develop complex programs.
Warnings
1. Applets do not contain main class, so do not confuse.
2. Applets cannot access or manipulate files stored in user hard drive for security reasons.
Comments
No Comments Exist
Be the first, drop a comment!