Applet 1

AMITY UNIVERSITY

Amity Institute of Information Technology

  • Course: BCA B,C & D 247

  • Semester: III

  • Subject: Object Oriented Programming Using Java

  • Instructor: Prof (Dr) Laxmi Ahuja

Learning Objectives

  • Impart Java Programming Skills: Develop fundamental programming skills in Java.

  • Understanding Object-Oriented Paradigm: Gain knowledge of object-oriented principles as applied in Java.

  • Designing Desktop Applications: Learn to use the latest Java-based APIs to create desktop applications.

Contents to be Covered

  • Introduction to Applets

  • Applet Life Cycle

  • Applet Font and Color Class

  • Applet Graphics

JAVA LANGUAGE

  • Definition: Java is a general-purpose, object-oriented programming language.

  • Types of Java Programs:

    • Stand-alone Applications: Software that runs independently on a local computer.

    • Web Applets: Java programs that run within a web browser.

  • Execution Steps for Stand-alone Programs:

    1. Compile: Convert source code into bytecode using the javac compiler.

    2. Run: Execute the compiled bytecode using the Java interpreter.

APPLETS AND APPLICATIONS

  • Java Application: A stand-alone program invoked from the command line that includes a main method.

  • Java Applet: An embedded program within a web page, executed in a Java-enabled browser, lacks a main method.

APPLET OVERVIEW

  • Characteristics:

    • Must be a subclass of java.applet.Applet.

    • Runs inside a web page and is embedded in HTML.

    • Requires JVM to view.

    • Adheres to strict security rules enforced by web browsers.

ADVANTAGES AND DISADVANTAGES

  • Advantages:

    • Works at the client-side providing less response time.

    • Secure execution environment.

    • Cross-platform compatibility.

  • Drawbacks:

    • Requires a plugin in the browser to execute applets.

APPLET IMPLEMENTATION

Required Imports

  • import java.applet.*;

  • import java.awt.*;

  • import java.io.*;

APPLET HIERARCHY

  • Applet subclass structure:

    • Applet is a subclass of Panel, which extends Container, itself a subclass of Component.

RUNNING AN APPLET

  • Methods to Run an Applet:

    • Via HTML File: Embedding applet using HTML tags.

    • Using AppletViewer: A tool for testing applet code.

APPLET LIFE CYCLE

  • Lifecycle Methods:

    • init(): Initializes applet, called once.

    • start(): Invoked when the applet becomes active.

    • paint(Graphics g): For rendering graphics on the applet.

    • stop(): Called when the applet is no longer active.

    • destroy(): Executes when the applet is removed from memory.

DRAWING WITH GRAPHICS CLASS

Graphics Methods

  • Basic Drawing Methods:

    • drawLine(int x1, int y1, int x2, int y2): Draws a line.

    • drawRect(int x, int y, int width, int height): Draws a rectangle.

    • drawOval(int x, int y, int width, int height): Draws an oval.

    • fillRect(int x, int y, int width, int height): Fills a rectangle with color.

    • Other methods for various graphics tasks (polygons, arcs, etc.).

PASSING PARAMETERS TO APPLETS

  • Using PARAM tags: Parameters can be passed in NAME=VALUE format within <PARAM> tags.

  • Retrieving Parameters: Use `getParameter(