Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Applets in java programming, Slides of Java Programming

An introduction to Java Applets, which are special programs embedded in webpages to generate dynamic content. It explains the lifecycle of Java Applets, the architecture and skeleton of an Applet, and the Java classes used to create Applets. It also covers the display methods and requesting repaint, as well as passing parameters to Applets. sample code and syntax for creating and using Applets. useful for students studying Java programming and web development.

Typology: Slides

2021/2022

Available from 01/17/2022

desu-harshith
desu-harshith 🇮🇳

3 documents

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java Applet
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Applets in java programming and more Slides Java Programming in PDF only on Docsity!

Java Applet

Java Applet

  • (^) Applet is a special type of program that is embedded in the webpage to generate the dynamic content.
  • (^) It runs inside the browser and works at client side.
  • (^) An applet is a Java program that runs in a Web browser.

Drawback of Applet:

  • (^) Plugin is required at client browser to execute applet.
  • (^) Java Plug-in extends the functionality of a web browser , allowing applets or Java Beans.

Lifecycle of Java Applet /Architecture/Skeleton

  1. Applet is initialized.
  2. Applet is started.
  3. Applet is painted.
  4. Applet is stopped.
  5. Applet is destroyed.

java.awt.Component class :

  • (^) The Component class provides 1 life cycle method of applet.
  • (^) public void paint(Graphics g): is used to paint the Applet.
  • (^) It provides Graphics class object that can be used for drawing oval, rectangle, arc etc.

Life cycle program:

  • (^) import java.awt.*;
  • (^) import java.applet.*;
  • (^) /*
  • */
  • public class AppletTest extends Applet
  • {
  • public void init() { //initialization }
  • (^) public void start () { //start or resume execution }
  • (^) public void stop() { //suspend execution }
  • (^) public void destroy() { //perform shutdown activity }
  • (^) public void paint (Graphics g) { //display the content of window }
  • (^) }

Program:

  • (^) import java.applet.Applet;
  • (^) import java.awt.Graphics;
  • (^) /*
  • (^)
  • (^) */
  • (^) public class First extends Applet{
  • (^) public void paint(Graphics g){
  • (^) g.drawString("welcome",150,150);
  • (^) }
  • (^) }

APPLET DISPLAY METHODS:

  1. void drawstring(String message, int x, int y)
  2. void setBackground(Color newColor)
  3. void setForeground(Color newColor)
  • (^) To set the background color of an applet’s window, we use setBackground( ).
  • (^) To set the foreground color, we use setForeground().
  • (^) These methods are defined by Component, and have the general forms: void setBackground(Color newColor) void setForeground(Color newColor) Here, newColor specifies the new color.
  • (^) The class Color defines the following values that can be used to specify colors:
  • (^) Color.black Color.magenta Color.blue Color.orange Color.cyan Color.pink Color.darkGray Color. red Color.gray Color.white Color. green Color.yellow Color.lightGray
  • (^) For example, this sets the background color to blue and the text color to yellow: 2. setBackground(Color.blue);
  • (^) 3. setForeground(Color.yellow);

Requesting Repaint:

  • (^) The repaint( ) method is defined by the AWT.
  • (^) whenever your applet needs to update the information displayed in its window, it simply calls repaint().
  • (^) It has defined as follows:
  1. void repaint( )
  2. void repaint(int left , int top , int width , int height )

Repaint() program:

import java.awt.; import java.applet.; /* */ public class SampleBanner extends Applet implements Runnable{ String msg = "A Simple Moving Banner."; Thread t = null; int state; boolean stopFlag; public void init() { setBackground(Color.cyan); setForeground(Color.red); }

public void stop() { stopFlag = true; t = null; } public void paint(Graphics g) { g.drawString(msg, 50, 30); showStatus("This is shown in the status window."); } }

Passing Parameters to Applets :

  • (^) We can get any information from the HTML file as a parameter. For this purpose, Applet class provides a method named getParameter(). Syntax: public String getParameter(String parameter Name) The get Parameter() method of the Applet class can be used to retrieve the parameters passed from the HTML page