0% found this document useful (0 votes)
80 views34 pages

Unit No.1

This document provides an overview of Java applets: 1. Applets are small Java programs that run in web browsers to provide interactive functionality on web pages. They differ from standalone Java applications in that they cannot access local files or network resources directly. 2. Developing an applet involves writing Java code, compiling it into a class file, embedding the class in an HTML page using applet tags, and testing it using a web browser or applet viewer. Key applet methods include init(), start(), paint(), and destroy(). 3. Parameters can be passed to applets to customize their behavior. The getParameter() method retrieves parameter values specified in the HTML applet tag.

Uploaded by

Aman Mugut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views34 pages

Unit No.1

This document provides an overview of Java applets: 1. Applets are small Java programs that run in web browsers to provide interactive functionality on web pages. They differ from standalone Java applications in that they cannot access local files or network resources directly. 2. Developing an applet involves writing Java code, compiling it into a class file, embedding the class in an HTML page using applet tags, and testing it using a web browser or applet viewer. Key applet methods include init(), start(), paint(), and destroy(). 3. Parameters can be passed to applets to customize their behavior. The getParameter() method retrieves parameter values specified in the HTML applet tag.

Uploaded by

Aman Mugut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Advanced JAVA Programming

UNIT :- I
Applet
Applet

Java Programs are available in two flavours


Application :- It is similar to all other kind of programs like in C , C++ etc. to solve
a real time problem
Applet :- Applet is small Java programs that are primarily used in internet
computing
They can be transported over the Internet from one computer to another and run
using the Applet viewer or any web browser that supports Java.
An applet , like any application program, can do many things for us. It can perform
arithmetic operations, display graphics, play sounds, accept user input, create
animation and play interactive games.
A web page can now contain not only a simple text or a static image but also a Java
applet which run graphics, sounds and moving images.
Local and Remote Applet

We can embed applets into web pages in two ways


One , we can write our own applet and embed them into web pages
Second, we can download an applet from a remote computer system and
then embed into a web page.
How Applets Differ from Applications
Applets and stand alone applications are Java programs
Significant difference between them:- Applets are not full featured application
programs.
They are usually written to complete a small task or a component of a task.
They are usually designed for use on the internet

1. Applets do not use the main() method for initiating the execution of the code
2. Unlike stand alone application, applets can not be run independently
3. Applets can not read form or write to the files in the local computer
4. Applets can not communicate with other servers on the network
5. Applets can not run any program from the local computer
Preparing to Write Applets

Before we try to write applets, we must make sure that Java is installed properly, and also
ensure that either the Java appletviewer or a Java enabled browser is available
Steps involved in developing and testing in applet are

1. Building an applet code (.java file)


2. Creating an executable applet (.class file)
3. Designing a web page using HTML tag
4. Preparing <APPLET>tag
5. Incorporating <APPLET> tag into the web page
6. Creating HTML file
7. Testing the applet code
Building Applet Code

Our applet code use the services of two classes, namely, Applet and Graphics from the
Java class library.
Applet class which is contained in the java.applet package provides life and behaviour of
applet through its methods such as init(), start(), paint(), close().
Therefore Applet class is the life cycle of applet.
paint () method of the Applet class, when it is called, actually displays the result of the
applet code on the screen. Output may be text, graphics, sound etc.
paint() method, which requires a Graphics object as an argument is defined as
public void paint(Graphics g)
This requires that the applet code import the java.awt package that contain the Graphics
class.
All output operation of an applet are performed using the methods defined in the Graphics
class.
Building an Applet Code
//File Name is :- AppletClassName.java
import java.applet.Applet;
 Process-1) Import Applet and Graphics import java.awt.Graphics;
class from applet and awt package
respectively public class AppletClassName extends
 Process -2) Define applet class from
Applet
{
extending the properties of Applet class
-----------------
 Process-3) Override the method paint public void paint(Graphics g)
that shows the result of applet code {
 Process -4) By using object of Graphics -----------
class access the methods that perform -----------
the all operation of applet }
• Compile this file to creating a .class file ---------------
---------------
for execution of applet code
}
Testing a Applet Code
We know the applet are programs that reside on web pages. In order to run a
Java applet, it is first necessary to have a web page that references that applet.
Basically web page made up of text and HTML tags that can be interpreted by
web browser or an applet viewer.
A web page also called as HTML page
A web page is marked by an opening HTML tag <HTML> and a closing
HTML tag </HTML> and its divided into three major sections:
1. Comment Section (Optional)
2. Head Section ( Optional)
3. Body Section
Testing a Applet Code

<HTML>
Comment Section
<!
---- -------
-------------
>
Head Section
<HEAD>
Title Tag
</HEAD>

Body Section
<BODY>
Applet Tag <applet code = “AppletClassName.class” width =“300”
</BODY> Height=“300”>
</applet>
</HTML>
Save as .html and use appletviewer filename.html commaned to
Applet Life Cycle
Basic Methods of Applet

public void init() – To initialize or pass input to an applet

public void start()- start() called after the init() method and it starts an applet

public void stop()- To stop running applet

public void paint(Graphics G)- To draw something within an applet

Public void destroy() – To remove an applet from memory completely


Example :- Print the Hello Java on Applet Window

import java.awt.*;
import java.applet.*;
public class HelloJava extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello Java", 100,100);
}
}

/*
<html>
<body>
<applet code= "HelloJava.class" width ="300" height="300"
>
</applet>
</body>
</html>
*/
*/
Example

import java.awt.*;
import java.applet.*;
public class HelloJava extends Applet
{
public void init()
{
resize(200,200);
setBackground(Color.cyan);
}
public void paint(Graphics g)
{
g.drawString("Hello Java", 100,100);
}
}
/*
<html>
<body>
<applet code= "HelloJava.class" width ="300" height="300" >
</applet>
</body>
</html>
*/
import java. applet.*; }
import java. awt.*; }

public class Sample extends Applet


{ /*
String msg; <html>
public void init() <body>
{ <applet code="Sample.class" width="300" height="300">
setBackground(Color.red); // set background and forground </applet>
color </body>
of applet window </html>
setForeground(Color.black); */
msg="Hello Friends .... "; // initialize the string to
dispaly
}
public void start()
{
msg += "How are you?....";
}
public void paint(Graphics g)
{
msg+= "Bye…….";
g.drawString(msg,10,30);
import java. applet.*; msg+= "Bye…….";
import java. awt.*; g.setFont(f1);
g.drawString(msg,10,30);
public class Sample extends Applet }
{ }
String msg;
Font f1;
public void init() /*
{ <html>
setBackground(Color.red); // set background and forground <body>
color <applet code="Sample.class" width="300" height="300">
of applet window </applet>
setForeground(Color.black); </body>
msg="Hello Friends .... "; // initialize the string to </html>
display */
f1 =new Font (“Aerial”,Font.BOLD, 18);
}
public void start()
{
msg += "How are you?....";
}
public void paint(Graphics g)
{
Passing a Parameter to Applet
import java.applet.Applet; /*
import java.awt.Graphics; <html>
<body>
public class Test extends Applet <applet code="Test.class" width="300"
{ height="300">
String str; <param name="string" value="Applet">
public void init() </applet>
{ </body>
</html>
str=getParameter("string"); */
if (str ==null)
str= "Java";
str="Hello"+str;
}
public void paint(Graphics g)
{
g.drawString(str,10,100);
}
}
Passing a Parameter to Applet
import java.awt.Graphics; /*
import java.applet.Applet; <html>
<body>
public class Rectangle extends Applet <applet code="Rectangle.class" width="300"
{ height="300">
int x,y,w,h; <param name="xValue" value="20">
public void init() <param name="yValue" value="40">
{ <param name="wValue" value="100">
x=Integer.parseInt(getParameter("xValue")); <param name="hValue" value="120">
y=Integer.parseInt(getParameter("yValue")); </applet>
w=Integer.parseInt(getParameter("wValue")); </body>
h=Integer.parseInt(getParameter("hValue")); </html>
} */

public void paint(Graphics g)


{
g.drawRect(x,y,w,h);
}
}
Passing a Parameter to Applet
import java.awt.*; g.drawString("Input a number in }
import java.applet.*; each box",10,50); /*
<html>
public class UserIn extends Applet try <body>
{ { <applet code ="UserIn.class"
TextField text1, text2; s1=text1.getText(); width="300" height="300">
public void init() x= Integer.parseInt(s1); </applet.
{ s2=text2.getText(); </body>
text1=new TextField(8); y=Integer.parseInt(s2); <html>
text2=new TextField(8); } */
add(text1); catch(Exception e)
add(text2); {}
text1.setText("0"); z=x+y;
text2.setText("0"); s= String.valueOf(z);
} g.drawString("The sum
public void paint(Graphics g) is:",10,75);
{ g.drawString(s,100,75);
int x=0,y=0,z=0; }
String s1,s2,s;
Passing a Parameter to Applet
Reference Book
E. Balagurusamy, “Programming With JAVA” , Tata McGraw Hill, 6 th
Edition

Herbert Schildt, “ JAVA:- The Complete Reference, Tata McGraw Hill,


11th Edition

Pune Institute of Computer Technology E&TC Department


getCodeBase() and getDocumentBase()

1) getCodeBase() :- JAVA will allow the applet to load the directory from
which the applet’s class file was loaded
Get the base url

2) getDocumentBase():- JAVA will allow the applet to load the data from the
directory holding the HTML file that started the applet (document base)
Get URL of the document in which the applet is embedded.
JApplet

 Called as Swing Applet


 Second type of program that commonly uses swing is applet
 Swing based applets are similar to AWT based applet but with an
important difference
 Swing applet extends the JApplet rather than Applet
 JApplet includes all of the functionality founds in Applet and adds support
for Swing.
 Swing applets use the same four life cycle methods as init(), start(), stop(),
destroy()
 Refer Applet_Example program
swing

 Called as Swing Applet


 Second type of program that commonly uses swing is applet
 Swing based applets are similarto AWT based applet but with an important
difference
 Swing applet extends the JApplet rather than Applet
 JApplet includes all of the functionality founds in Applet and adds support
for Swing.
 Swing applets use the same four life cycle methods as init(), start(), stop(),
destroy()
 Refer Applet_Example program
Assignment

 Create an applet with three text fields and four buttons add, substract, multiply and
divide. User will enter two values in the Text Fields. When any button is pressed, the
corresponding operation is performed and the results is displayed in the third text fields.

 Assignment Deadline :-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy