How to Write and Implement a Java Applet in Windows XP

Mar 1, 2011 12:48 AM

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

Screen shot 2



now save the file and click start button -> run -> type cmd and click ok

Screen shot 3



now change your directory to the folder where the file is saved

             For Ex: D:\>cd folder path

Screen shot 4



type ‘javac demoApp.java’

Screen shot 5



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.

Screen shot 6

view the gallery for a pictorial instruction.

634339590432834366.jpg

Step 1

634339590642993306.jpg

step 2

634339590833308242.jpg

step 3

634339590985028934.jpg

step 4

634339591111124970.jpg

step 5

634339591594571752.jpg

step 6 & step 7

634339590432834366.jpg

Step 1

634339590642993306.jpg

step 2

634339590833308242.jpg

step 3

634339590985028934.jpg

step 4

634339591111124970.jpg

step 5

634339591594571752.jpg

step 6 & step 7

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!