Basic Java Applet Programs

How to run applet in cmd !!

javac nameofprogram.java
appletviewer nameofprogram.html


//Java Applet to print "Hello World"

import java.applet.*; 
import java.awt.*; 


public class Hello extends Applet 
{

  public void paint(Graphics g) 
     {  
     
      g.drawString("Hello World",20,40);
     }




<html>
<head> <title> Applet </title> </head>
<body>
<applet code="Hello.class" CodeBase="" width=300 height=400></applet>
</body>
</html>

//Java Applet Lifecycle


import java.applet.Applet;
import java.awt.*;

public class LifeCycle extends Applet
{
     String output = "";
     String event;

     public void init()
     {
          event = "Initializing...";          
    printOutput();
     }

     public void start()
     {
          event = "Starting..."; 
    printOutput();
     }

     public void stop()
     {
          event = "Stopping...";
    printOutput();
     }

     public void destroy()
     {
          event = "Destroying...";
    printOutput();
     }

     private void printOutput()
     {
          System.out.println(event);
    output += event;
    repaint();
     }

     public void paint(Graphics g)
     {
          g.drawString(output, 40, 40);
          setBackground(Color.PINK);

     }

  
}

<html>
<head> <title> Applet </title> </head>
<body>
<Applet code="LifeCycle.class" width=300 height=300></Applet>
</body>
</html>


//Java Applet demonstrating parameter passing

import java.applet.*; 
import java.awt.*; 
public class ParameterPassing extends Applet
 {
  private String strDefault = "Hello! Java Applet for Parameter Passing";
  public void paint(Graphics g)
   {
  String strParameter = this.getParameter("Message");
  if (strParameter == null)
  strParameter = strDefault;
  g.drawString(strParameter, 50, 25);
  } 
}

<HTML>
<HEAD>
<TITLE>Parameter Passing</TITLE>
</HEAD>
<BODY>

<APPLET code="ParameterPassing.class" width="800" height="100">
<PARAM name="Message" value="Welcome!!This applet demonstrates parameter passing">
</APPLET>

</BODY>
</HTML>

//Java Applet with a pink background


import java.applet.Applet;
import java.awt.*;

public class LC extends Applet
{
     String output = "";
     String event;

     public void init()
     {
          event = "Initializing...";          
          printOutput();
     }

     public void start()
     {
          event = "Starting..."; 
          printOutput();
     }

     public void stop()
     {
          event = "Stopping...";
          printOutput();
     }

     public void destroy()
     {
          event = "Destroying...";
          printOutput();
     }

     private void printOutput()
     {
          System.out.println(event);
          output += event;
          repaint();
     }

     public void paint(Graphics g)
     {
          g.drawString(output, 40, 40);
          setBackground(Color.PINK);

     }

  
}

<html>
<head> <title> Applet </title> </head>
<body>
<Applet code="LC.class" width=300 height=300></Applet>
</body>
</html>


Basic JAVA tutorials can be found at :
Tutorials Point

Comments

Popular posts from this blog

IT resources @ DAIICT

I blinked and the day passed ...

The world is what it is