1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

A Simple Java Applet

Discussion in 'Java' started by pradeep, Jul 28, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    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:
    <APPLET CODE="ClassName.class" HEIGHT=h WIDTH=w>
     Alternate text
     </APPLET>
    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.

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

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

    test.HelloWorld.html

    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:

    HelloWorld.java

    Here is the Java code:
    (HelloWorld):

    Code:
    import java.applet.*;
     
     import java.awt.*;
     
     public class HelloWorld extends Applet {
     
           public void init() {
     
           resize(150,25);
     
           }//init
     
           public void paint(Graphics g) {
     
           g.setFont(new Font("Helvetica", Font.PLAIN, 8));
     
           g.drawString("Hello world!", 50, 25);
     
           }//paint
     
       }//HelloWorld
     
    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.
     
  2. vivek_nagathan

    vivek_nagathan New Member

    Joined:
    Aug 10, 2006
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    How to Disable View Source in HTML page

    Hi,
    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
     
  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    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:
     //  Disables Right-click on this page...
        var message="Function  Disabled!";
        function clickIE4(){
         if  (event.button==2){
          alert(message);
          return  false;
         }
        }
        function clickNS4(e){
         if  (document.layers||document.getElementById&&!document.all){
          if  (e.which==2||e.which==3){
           alert(message);
           return  false;
          }
         }
        }
        if  (document.layers){
         document.captureEvents(Event.MOUSEDOWN);
         document.onmousedown=clickNS4;
        }  else if  (document.all&&!document.getElementById){
         document.onmousedown=clickIE4;
        }
        document.oncontextmenu=new  Function("alert(message);return false");
      
     

Share This Page