A Simple Java Applet

pradeep's Avatar author of A Simple Java Applet
This is an article on A Simple Java Applet in Java.
Rated 1.00 By 1 users
An Applet is a small program that can be sent across the Internet and interpreted on a client machine. To give permission for remote access it must be a public class. Typically it is a class that inherits and/or defines a special set of functions needed to run an applet. These are part of the class Applet. So all Java applets are public classes that extend Applet.

You can not run a Java applet unless it is also an application or you have a WWW page that refers to it.
The page needs HTML like the following to call the compiled code, in the same directory:

HTML Code:
<APPLET CODE="ClassName.class" HEIGHT=h WIDTH=w>
 Alternate text
where h and w are the HEIGHT and WIDTH of the box in which the applet outputs its response. The alternate text appears when a browser can not handle APPLETs. The code in "ClassName.class" above, is the result of compiling a file called "ClassName.java" that contains a public class called ClassName which extends an Applet.

Here is a suitable piece of HTML to test a simple HelloWorld class:

HTML Code:
 <APPLET CODE="HelloWorld.class" HEIGHT=150 WIDTH=150>
 You can not see this brilliant Java Applet.
Put this in a file called:


The code for the HelloWorld applet has to be a public class called "HelloWorld" that extends Applet and is in in a Java file called:


Here is the Java code:

Code: Java
import java.applet.*;
 import java.awt.*;
 public class HelloWorld extends Applet {
       public void init() {
       public void paint(Graphics g) {
       g.setFont(new Font("Helvetica", Font.PLAIN, 8));
       g.drawString("Hello world!", 50, 25);

The Applet is compiled just like any other program:

javac HelloWorld.java

This will generate a set of files with extension/suffix ".class". Notice that the compiler forces you to name the file "HelloWorld.java", the Class "HelloWorld", and generates the binary bytecode in a file called "HelloWorld.class". Sun has written a special program to test applets in page:

appletviewer test.HelloWorld.html

However you can not use 'java' to run the HelloWorld class - it has no 'main'. Neither can 'java' "run" the WWW pages like the test.HelloWorld.html file.
vivek_nagathan's Avatar
Newbie Member
How can I disable the view source option in an HTML page.Will it be possible to disable the view source option in the pop up window which appears when we click the right mouse button
pradeep's Avatar, Join Date: Apr 2005
Team Leader
We can use JavaScript to disable right click on a web page, so the context menu won't pop up.But 'View Source' will still be accessible from View menu. It's good to disable right click, only experience web users will know about the View menu.

Code: JavaScript
//  Disables Right-click on this page...
    var message="Function  Disabled!";
    function clickIE4(){
     if  (event.button==2){
      return  false;
    function clickNS4(e){
     if  (document.layers||document.getElementById&&!document.all){
      if  (e.which==2||e.which==3){
       return  false;
    if  (document.layers){
    }  else if  (document.all&&!document.getElementById){
    document.oncontextmenu=new  Function("alert(message);return false");