• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Applets » Working with Applets
Next →
← Prev

Working with Applets

By Dinesh Thakur

Applets can be executed in two ways: from Browser or from Appletviewer. The JDK provides the Appletviewer utility.

Browsers allow many applets on a single page, whereas applet viewers show the applets in separate windows.

Program, named SampleApplet, demonstrates the life-cycle methods of applets:

Program: A demonstration of life-cycle methods of applets.

import java.applet*;

import java.awt.*;

import java.awt.event.*;

/*<APPLETcode = “SampleApplet” width=300 height=150> </APPLET>*/

public class SampleApplet extends Applet implements ActionListener

{

    Button b = new Button (“Click Me”);

    boolean flag = false;

    public void init ()

    {

         System.out.println (“init () is called”);

         add(b);

         b.addActionListener (this );

    }

     public void start ()

     {

         System.out.println (“start () is called”);

    }

     public void destroy ()

     {

         System.out.println(“destroy () is called”);

     }

     public void stop ()

     {

        System.out.println(“stop () is called”);

     }

     public void actionPerformed (ActionEvent ae)

    {

         flag = true;

         repaint ();

     }

     public void paint(Graphics g)

     {

          System.out.println (“paint () is called”);

          if (flag)

          g.drawString (”This is a simple Applet”,50,50);

     }

}

For executing the program using the AppletViewer or the web browser, first the program is to be compiled in the same way as the Java application program as shown below:

C:\>Javac SampleApplet.java

We’ll be covering the following topics in this tutorial:

  • Running the applet using AppletViewer
  • Running the applet using the web browser

Running the applet using AppletViewer

AppletViewer is a program which provides a Java run-time environment for applets. It accepts a HTMLfile as the input and executes the <applet> reference, ignoring the HTML statements. Usually, AppletViewer is used to test Java applets.

To execute any applet program using AppletViewer the applet tag should be added in the

comment lines of the program. These comment lines will not be considered when the programs are compiled. The command AppletViewer <appletfile.java> is used to view the applet .To execute Program, we can invoke the same as follows:

c:\>appletviewer SampleApplet.java

The reader can thus execute Program. It is suggested as an exercise.

The drawstring () method draws the String ‘This is a sample applet’ on the applet panel, starting at the coordinate (50, 50). Note that the applet panel provides a graphical environment in which the drawing will paint rather than print. Usually, the panel is specified in terms of pixels in which the upper left comer is the origin (0, 0).

As mentioned earlier, applets interact with the user through AWT. In Program we have used one AWT component-button. Observe the SampleApplet class header:

public class SampleApplet extends Applet implements ActionListener

The ActionListener interface is implemented by a class to handle the events generated

by components such as buttons. The method actionPerformed () available in this interface is overridden so that some action is performed when the button is pressed. In Program, it can be observed that when the applet is first executed, the paint () method does not display anything on the applet window. This is because flag is set to false. When the button is pressed (observe the actionPerformed () method) the flag is set to true and repaint () method is called. This time since the flag is set to true, the repaint () method will display the string ‘This is a simple applet’.

In Program we use the System.out.println () method to print the sequence of the methods executed when the applet is running.

The output shown in the console is in the following:

init () is called;

start () is called;

paint () is called;

stop () is called;

start () is called;

paint () is called;

stop () is called;

destroy () is called;

When the applet program is first executed, three methods-init (), start (), paint () are executed. Later, if the applet window is minimized, the stop () method is executed. If the applet window is maximized again, the start () and the paint () methods will be executed simultaneously, When the applet window is closed, the stop () and the destroy () methods will be called simultaneously.

Running the applet using the web browser

The browsers that support applets include Netscape Navigator, Internet Explorer and Hot Java. The applet can be viewed from a web browser by writing the APPLET tag within the HTML code. To run the Program in the web browser, the following html code has to be written:

<html>

<head>

<title> A sample Applet</title >

</head>

<body>

<applet code=”SampleApplet” width=200 height=200>

</applet>

</body>

</html>

To run the applet, the browser, which could be the Internet Explorer for instance, has to be opened, the options File     Open have to be selected. Then, the applet code HTML file is opened by using the Browse button.

To show the difference between running the applet in the applet viewer and the web browser Program is modified a little to the form shown in Program:

Program An alternate way of running an applet.

import java.applet.*;

import java.awt. *;

public class SampleApplet extends Applet

{

     String msg = “”;

     public void init ()

     {

        msg = msg + “init ();”

     }

     public void start ()

     {

          msg = msg+ “start ();”

     }

     public void destroy ()

     {

          msg = msg+ “destroy ();”

     }

     public void stop ()

     {

        msg = msg+ “stop ();”

    }

    public void paint(Graphics g)

    {

         msg = msg+ “paint ();”

         g.drawString(msg,10,20);

    }

}

The button is removed and the life-cycle methods are displayed on the applet window instead of on the console. The difference between the output of the above applet when called using the AppletViewer and the web browser is in Figure.

The output of Program shows the applet with the sequence of the methods executed. When the applet program is first executed, the init (), start () and paint () methods are called.

When the web browser is minimized, the paint () method is called, and when it is maximized, paint () is called again. In the case of the applet viewer the stop (), start () and paint () methods are called when the applet is maximized. The same things happen when the applet viewer and the web browser are’ closed. That is, the stop () method and the destroy () method are called. When moving from the web page containing the applet to another web page and back, the start () method in the applet is called.

You’ll also like:

  1. How to Passing Parameters to Applets.
  2. Class Hierarchy for Applets
  3. Differences Between Applications and Applets
  4. Java Applications Versus Java Applets
  5. How to Working with CallableStatement
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW