0% found this document useful (0 votes)
3 views19 pages

Applets in Java

The document provides an overview of Java programming with a focus on Java applets, including their types, lifecycle methods, and examples of applet creation. It explains the differences between standalone and web applications, outlines the lifecycle of applets, and provides code snippets for creating and embedding applets in HTML. Additionally, it includes references for further reading on Java programming and applet development.

Uploaded by

okokji4201
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
3 views19 pages

Applets in Java

The document provides an overview of Java programming with a focus on Java applets, including their types, lifecycle methods, and examples of applet creation. It explains the differences between standalone and web applications, outlines the lifecycle of applets, and provides code snippets for creating and embedding applets in HTML. Additionally, it includes references for further reading on Java programming and applet development.

Uploaded by

okokji4201
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 19

Name of the School: School of Computer Science and

Engineering
Course Code: E1UC307C Course Name:Java Programming

DEPARTMENT OF COMPUTER SCIENCE


& ENGINEERING
Subject Name: Java Programming
Topics Covered: Java Applets

1
Faculty Name: Programe Name:
Types of Java
Application
1. Stand alone application(Java application)
2. Web Application( Applet & JSP/Servlet)

Web applications are the applications users can use through a browser. Standalone
applications are known as Desktop/ Offline applications and can be accessed
offline.

Java applications
Run in stand-alone mode
No additional software required (such as a Web browser)

Java applets
Compiled Java class files
 Run within a Web browser (or an appletviewer)
Loaded from anywhere on the Internet
2
Applet
An Applet in Java is a small application that runs within a web browser or an
applet viewer. Applets are written in Java and are typically used to provide
interactive features in a web page, such as animations, games, or interactive
forms.
A main() method is not invoked on an applet, and an applet class will not define
main(). Applets are designed to be embedded within an HTML page.

Here is a basic example of how to create an applet in Java:


Steps to Create a Java Applet:

1.Import the necessary packages.


2.Extend the Applet class.
3.Override the init(), start(), stop(), and destroy() methods as needed.
4.Use the paint() method to draw content on the applet window.
3
5.Create an HTML file to embed the applet in a webpage (for older browsers).
Life Cycle Methods of Applet
Following are the methods for a full applet cycle.

init() method
start() method
paint() method
stop() method
destroy() method

4
In the life-cycle of execution, the
Life Cycle of Applet applet exists (lives) in one of these
4 states.
Applet Life Cycle There are methods that can
changes the state of the applet. All
init() Born of these methods have a name and
Begin
they are called as callback methods.
stop()
start() These methods are named so
because they are called
automatically by the browser when
Idle required for smooth execution of the
Running applet.
destroy() Here, programmers write the
paint() start() above-mentioned methods with
some code but never calls.
End
Dead

5
Life Cycle steps
Four methods in the Applet class give you the framework on which you build any applet:
 init: This method is intended for whatever initialization is needed for your applet. It is
called after the param tags inside the applet tag have been processed.
 start: This method is automatically called after the browser calls the init method. It is
also called whenever the user returns to the page containing the applet after having
gone off to other pages.
 stop: This method is automatically called when the user moves off the page on which
the applet sits. It can, therefore, be called repeatedly in the same applet.
 destroy: This method is only called when the browser shuts down normally. Because
applets are meant to live on an HTML page, you should not normally leave resources
behind after a user leaves the page that contains the applet.
 paint: Invoked immediately after the start() method, and also any time the applet
needs to repaint itself in the browser. The paint() method is actually inherited from the
6 java.awt.
 init and destroy are only
called once each
init()
 start and stop are called
whenever the browser
enters and leaves the page start()
 do some work is code called
by your listeners paint
 paint is called again when do other work
the applet needs to be
repainted stop()

destroy()

7
Applet Program
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorld extends Applet {
public void paint(Graphics g) {
g.drawString("Hello World!", 50, 25);
}
}
Compile this file HelloWorld.java and mention the path in HTML file
Now you have to create an HTML File that Includes the Applet.
Using a text editor, create a file named Hello.html in the same directory that
contains HelloWorld.class. This HTML file should contain the following text:
8
Embedding Applet in Web page
<HTML>
<HEAD>
<TITLE>
Hello World Applet
</TITLE>
</HEAD>

<body>
<h1>Hi, This is My First Java Applet on the Web!</h1>
<APPLET CODE="HelloWorld.class" width=500 height=400></APPLET>
</body>

</HTML>
9
Compile and Run an Applet
To compile: javac HelloWorld.java  Generates
HelloWorld.class

To run:
a) Use the appletviewer from JDK

open command prompt and reach to folder


C:\desktop> appletviewer index.html

b) Open page from browser:

index.html
10
Hello
import java.applet.Applet;
import java.awt.*; World.java
public class GraphicsDemo extends Applet{
public void paint(Graphics g){
g.setColor(Color.red);
g.drawString("Welcome",50, 50);
g.drawLine(20,30,20,300);
g.drawRect(70,100,30,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);
g.setColor(Color.pink);
g.fillOval(170,200,30,30);
g.drawArc(90,150,30,30,30,270);
g.fillArc(270,150,30,30,0,180); }
11
}
Displaying Image in Applet
import java.awt.*;
import java.applet.*;
public class DisplayImage extends Applet {
Image picture;
public void init() {
picture = getImage(getDocumentBase(),“abc.jpg");
}
public void paint(Graphics g) {
g.drawImage(picture, 30,30, this);
}
}
12
Passing Parameters to Applet
<HTML>
<HEAD><TITLE> Hello World Applet</TITLE></HEAD>

<body>
<h1>Hi, This is My First Communicating Applet on the Web!</h1>

<APPLET
CODE="HelloAppletMsg.class" width=500 height=400>

<PARAM NAME="Greetings" VALUE="Hello Friend, How are you?">

</APPLET>

</body>

</HTML>
13
Applet Program Accepting Parameters
//HelloAppletMsg.java
import java.applet.Applet;
import java.awt.*;
public class HelloAppletMsg extends Applet {
String msg;
public void init()
{
msg = getParameter("Greetings");
if( msg == null)
msg = "Hello";
}
public void paint(Graphics g) {
g.drawString (msg,10, 100);
}
This is name of parameter specified in PARAM tag;
} This method returns the value of parameter.
14
Output

15
Interactive Applet Program
import java.applet.Applet;
import java.awt.*; public void paint(Graphics g) {
public class SumNumsInteractive extends int num1 = 0;
Applet { int num2 = 0;
int sum;
TextField text1, text2; String s1, s2, s3;
public void init() g.drawString("Input a number in each
{ box ", 10, 50);
text1 = new TextField(10); try {
s1 = text1.getText();
text2 = new TextField(10); num1 = Integer.parseInt(s1);
text1.setText("0"); s2 = text2.getText();
text2.setText("0"); num2 = Integer.parseInt(s2);
add(text1); }
catch(Exception e1)
add(text2); {}
}

16
Displaying Numeric Value
//SumNums.java
import java.applet.Applet;
import java.awt.*;

public class SumNums extends Applet {


public void paint(Graphics g) {
int num1 = 10;
int num2 = 20;
int sum = num1 + num2;

String str = "Sum: "+String.valueOf(sum);


g.drawString (str,100, 125);
}
}
17
References:
 https://www.geeksforgeeks.org/
 https://www.javatpoint.com/exception-handling-in-java
 https://www.tutorialspoint.com/java/java_exceptions.htm
 The complete reference, eleventh edition, available at:
https://gfgc.kar.nic.in/sirmv-science/GenericDocHandler/1
38-a2973dc6-c024-4d81-be6d-5c3344f232ce.pdf

18
Thank you

19

You might also like