Header Banner
wonderhowto.mark.png
Gadget Hacks Next Reality Food Hacks Null Byte The Secret Yumiverse Invisiverse Macgyverisms Mind Hacks Mad Science Lock Picking Driverless

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)

File save dialog box on a computer.



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></applet>

//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.

File save dialog box on a computer screen.

Step 1

Java applet code snippet in a programming environment.

step 2

Java applet code snippet showcasing basic applet setup and drawing instructions.

step 3

Command Prompt interface displaying a Windows XP session.

step 4

Command prompt window showing Windows XP with a Java compilation command.

step 5

How to Write and Implement a Java Applet in Windows XP

step 6 & step 7

File save dialog box on a computer screen.

Step 1

Java applet code snippet in a programming environment.

step 2

Java applet code snippet showcasing basic applet setup and drawing instructions.

step 3

Command Prompt interface displaying a Windows XP session.

step 4

Command prompt window showing Windows XP with a Java compilation command.

step 5

How to Write and Implement a Java Applet in Windows XP

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.

You already know how to use your phone. With Gadget Hacks' newsletter, we'll show you how to master it. Each week, we explore features, hidden tools, and advanced settings that give you more control over iOS and Android than most users even know exists.

Sign up for Gadget Hacks Weekly and start unlocking your phone's full potential.

Related Articles

Comments

No Comments Exist

Be the first, drop a comment!