Androidberry logo Androidberry text logo
AndroidBerry Home
  • Learn Java
  • Java Programs
  • Interview Questions
  • Androidberry facebook
  • Androidberry twitter
  • Androidberry google plus

Java Tutorials


Basic Introduction

Decision Making

Looping

Classes and Objects

Inheritance

Interface

Packages

Exception Handling

Multithreading

Java Multithreading
Creating a Thread
Thread Life Cycle
Thread Exceptions
Thread Synchronization

Java Applets

What is Applet?
Java Applet vs Application
Creating a Applet
Displaying Applet on Webpage
Applet Lifecycle
Applet Tag
Passing Parameters to Applet
Setting Color to applet
Update and Repaint method
Using Fonts in Applet

Graphics Programming

Intro. to Graphics class
Drawing - Lines , Rectangles
Drawing - Circles and Ellipse
Drawing - Arcs
Drawing - Polygons
Drawing - Bar Charts

More on Java

Java Programs
Java Keywords Dictionary
Java Interview Questions

Java Creating a Applet


Java Applet

In java, Applet can be executed on local computer or Web page of browser.

In this topic we will see how to compile and run the applet on local computer.

Steps

  • Write import statement
  • Extend the class to Applet class
  • Write the program code in paint() method
  • Write applet tag in comments
  • Compile the program using javac
  • Run the applet using command appletviewer

Let's see the applet syntax to understand how to create an Applet.

Syntax

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

public class AppletClassName extends Applet
{
    //declarations
    
    public void paint(Graphics g)
    {
        // Actual applet operation code
        ....
        ....
    }
}
/* <applet code="AppletClassName.class" width="300" height="300"> 
</applet> */ 

Syntax Explaination

import statements

  • import java.applet.*; - This import statement imports (includes) applet package, which contain the class Applet.
  • import java.awt.*; - This import statement imports the Abstract Window Toolkit (AWT) used for user interaction. Ex- creating Graphical User Interface (GUI).

Extending Applet class

  • public class AppletClassName extends Applet - This statement inherits or Accesses the properties of Applet class into AppletClassName class.
  • The Applet class provides life and behaviour to applet throw it's methods such as init(), start() and paint().

Writting paint() method

  • Java applet does not contain the main() method but it has it's own method called paint().
  • When applet is loaded then java automatically calls the paint() method for starting the applet.
  • The paint() method displays the result of applet code on the screen like text,images,shapes etc.

Graphics g parameter

  • The output operations of an applet are performed with the help of methods defined in Graphics class.
  • In Graphics g, g is the object which is used to access all the method of Graphics class.

Applet tag in comments

  • The applet tag gives the name of the applet to be loaded and tells the browser how much space the applet required.
  • This tag is required when we are compiling our program using appletviewer tool. It only required while displaying applet on webpage.

Example

Displaying Hello World on Applet window

To print the message on the applet window, java has drawString() method stored in the Graphics class.

drawString(String,int,int) has 3 parameters - where String is the message to display, second is integer for x-axis and third integer for y-axis to place the message on window.

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

public class AppletProgram extends Applet
{
    public void paint(Graphics g)
    {
	g.drawString("Hello Applet", 15, 120);
    }
}
/*<applet code="AppletProgram.class" width=500 height=500> </applet> */

Compile and run as

javac AppletProgram.java

appletviewer AppletProgram.class

Output - Hello Applet

<< Previous Next >>

See Also

All Java Programs
Java Keywords
Java Interview Questions

AndroidBerry Support

About us
Contact us
Suggest us
Questions?

Follow us

Androidberry FacebookFacebook
Androidberry TwitterTwitter
Androidberry GooglePlusGoogle+
Back to top
Androidberry Name Logo