Internet and WWW
Internet is a massive networking infrastructure and
World Wide Web (WWW) is information sharing
model that uses Hyper Text Transfer Protocol (HTTP)
to transfer web documents (web pages) between inter-
connected nodes in a network.
Applets & Graphics - Dr. S.E 1
Static Web Page
Web site is designed by a collection of web pages. Hyper Text Markup
Language(HTML) is the standard markup language used for creating web
pages.
Hyper Text stands for linked text and Markup language stands for tagged
language. HTML elements are delimited by tags, written using angular
brackets.
HTML documents are interpreted and displayed by web browsers. HTML
documents generate only static web pages.
Applets & Graphics - Dr. S.E 2
Dynamic Web Page
• To build dynamic web pages, a collection of technologies
are used in the client side as well as in the server side to
enable user interaction and animation.
• Client side technologies include Java Script (scripting
language), CSS (presentation definition language), DOM
(Document Object Model) and Java Applets.
• Server side technologies include Java Servlets, Java Server
Pages (JSP), Hypertext Preprocessor (PHP) and Active
Server Pages(ASP).
Applets & Graphics - Dr. S.E 3
Applets
• Applet is a special type of tiny java program that
is embedded in to the webpage to generate the
dynamic web content.
• Applets does not contain main method since they
are not invoked using command line. It runs inside
the browser at client side.
• An Applet is a panel that allows GUI based
animation as well as user interaction.
• For security reasons, they have no access to the
client’s file system
• The Applet execution is controlled by Abstract
Window Toolkit (AWT) run time
Applets & Graphics - Dr. S.E 4
Applet Support
• Most modern browsers support Java Applets with
the provision of appropriate plugin
• The JDK tool appletviewer is used to test the
applet execution before viewed using browser
Applets & Graphics - Dr. S.E 5
The hierarchy of Applet
java.lang.Object
|
+----java.awt.Component
|
+----java.awt.Container
|
+----java.awt.Panel
|
+----java.applet.Applet
Applets & Graphics - Dr. S.E 6
1.Applet is initialized.
2.Applet is started.
3.Applet is painted.
4.Applet is stopped.
Applet Life cycle
5.Applet is destroyed.
1. Applet is initialized.
2. Applet is started.
3. Applet is painted.
4. Applet is stopped.
5. Applet is destroyed.
For creating any java applet, java.applet.Applet class must be inherited
For displaying the graphics, java.awt.Graphics class must be inherited
Applets & Graphics - Dr. S.E 7
Applets & Graphics - Dr. S.E 8
Applet life cycle methods
Applet class provides 4 methods of applet life cycle.
• public void init(): is used to initialize the Applet. It is
invoked only once.
• public void start(): is invoked after the init() method or
browser is maximized. It is used to start the Applet.
• public void stop(): is used to stop the Applet. It is invoked
when Applet is stopped or browser is minimized.
• public void destroy(): is used to destroy the Applet. It is
invoked only once.
Also Component class provides a method to applet life cycle
• public void paint(Graphics g): is used to paint the
Applet. It needs a Graphics class object that can be used
for drawing oval, rectangle, arc etc.
Applets & Graphics - Dr. S.E 9
public void init ( )
• This is the first method to execute
• It is an ideal place to initialize variables
• It is the best place to define the GUI Components
(buttons, text fields, scrollbars, etc.), lay them out,
and add listeners to them
• This method is executed only once
Applets & Graphics - Dr. S.E 10
public void start ( )
• Not always needed
• Called after init( )
• Called each time the page is maximized and
restarted
• Used mostly in conjunction with stop( )
Applets & Graphics - Dr. S.E 11
public void stop( )
• Not always needed
• Called when the browser leaves the page or taken
to task bar
• Called just before destroy( )
• Used mostly in conjunction with start()
Applets & Graphics - Dr. S.E 12
public void destroy( )
• Not often needed
• Called after stop( )
• Used to explicitly release system resources (like
threads)
Applets & Graphics - Dr. S.E 13
public void paint(Graphics g)
• Needed for drawing or painting any string or shapes
• By overriding this paint method, user can draw or fill
geometric shapes
Applets & Graphics - Dr. S.E 14
repaint( )
• repaint( ) method is called when the user has
changed something and wants the changes to appear
on the display screen
• repaint( ) schedules a call to update(Graphics g)
Applets & Graphics - Dr. S.E 15
Local Applet Remote Applet
Applet is developed by someone else
Applet is developed by the user and
and stored in a remote computer.
stored in the local computer. Class
Internet connection and URL address is
file is readily available in the local
required to use the remote applet
directory
<HTML>
<HTML> <APPLET CODE=MyFirstApplet.class
<APPLET CODE=MyFirstApplet.class CODEBASE=http://www.tpoint.com/appdir
WIDTH=500 HEIGHT=500> WIDTH=500 HEIGHT=500 >
</APPLET> </APPLET>
</HTML> </HTML>
Applets & Graphics - Dr. S.E 16
Applet Code
import java.applet.Applet;
import java.awt.Graphics;
public class MyFirstApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello",100,100);
}
}
HTML document
<HTML>
<APPLET CODE=MyFirstApplet.class WIDTH=500 HEIGHT=500>
</APPLET>
</HTML>
Applets & Graphics - Dr. S.E 17
Applet Interfaces
Applet class implements some of the methods
available in three interfaces
• AppletStub – inherited in Applet by default
• AppletContext – inherited in Applet by default
• AudioClip – Importing this interface, its methods
are invoked
Applets & Graphics - Dr. S.E 18
Interface java.applet.AppletStub
This interface serves as an intermediary between the applet
and the applet running environment.
• getCodeBase() Gets the code URL.
• getDocumentBase() Gets the document URL.
• getParameter(String) Gets a parameter of the applet.
Applets & Graphics - Dr. S.E 19
CodeBase and DocumentBase Illustration
import java.awt.Graphics;
import java.applet.Applet;
import java.net.URL;
public class CodeDocument extends Applet
{
public void paint(Graphics h)
{
URL u1=getCodeBase();//AppletStub method
URL u2=getDocumentBase();//AppletStub method
h.drawString("codebase is "+u1,200,200);
h.drawString("documentbase is "+u2,200,300);
}
}
<HTML>
<APPLET CODE=CodeDocument.class WIDTH=400 HEIGHT=400>
</APPLET>
</HTML>
Applets & Graphics - Dr. S.E 20
Passing parameter to applet
import java.awt.Graphics;
import java.applet.Applet;
public class ParamApp extends Applet
{
String str,str1,str2;
public void paint(Graphics h)
{
str1=getParameter("language");//AppletStub method
str2=getParameter("inventor");
str=str1+" was invented by "+str2;
h.drawString(str,20,20);
}
}
<HTML>
<APPLET CODE=ParamApp WIDTH=400 HEIGHT=400>
<PARAM NAME=language VALUE=C>
<PARAM NAME=inventor VALUE=Denis Ritche>
</APPLET>
</HTML>
Applets & Graphics - Dr. S.E 21
Interface java.applet.AppletContext
The methods in this interface can be used by an applet to
obtain information about its environment, which is usually
the browser.
• getAudioClip(URL) Gets an audio clip.
• getImage(URL) Gets an image.
• showStatus(String) Show a status string.
Applets & Graphics - Dr. S.E 22
Display an image in applet
import java.awt.Graphics;
import java.applet.Applet;
import java.awt.Image;
import java.net.URL;
public class Im extends Applet
{
public void paint(Graphics g)
{
URL u1=getCodeBase();//AppletStub method
Image i=getImage(u1,"se.jpg");//AppletContext method
g.drawImage(i,0,0,200,200,this);// Grphics method
g.drawString("VASUKI TAL - KEDARNATH",100,300);
}
}
<HTML>
<APPLET CODE=Im.class WIDTH=600 HEIGHT=400>
</APPLET>
</HTML>
Applets & Graphics - Dr. S.E 23
Interface java.applet.AudioClip
A very high level abstraction of audio. The AudioClip
interface is a simple abstraction for playing a sound clip.
• loop() Start playing the clip in a loop.
• play() Start playing the clip.
• stop() Stop playing the clip.
Applets & Graphics - Dr. S.E 24
Playing audio in applet
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;
public class Music extends Applet
{
AudioClip a1;
public void init()
{
URL u1=getCodeBase();//AppletStub method
a1=getAudioClip(u1,"song1.wav");//AppletContext method
}
public void start()
{
a1.play();//AudioClip method
}
public void stop()
{
a1.stop();//AudioClip method
}
} <HTML>
<APPLET CODE=Music.class WIDTH=800 HEIGHT=800>
</APPLET>
</HTML>
Applets & Graphics - Dr. S.E 25
Graphics & Applets
Most methods of the Graphics class can be divided into two basic groups:
a)Draw and fill methods, enabling you to render basic shapes, text, and images
b)Attributes setting methods, which affect how that drawing and filling appears
Graphics drawing methods include:
1)drawString – For drawing text
2)drawImage – For drawing images
3)drawLine, drawArc, drawRect, drawOval, drawPolygon – For drawing geometric
shapes
Applets & Graphics - Dr. S.E 26
Applets & Graphics - Dr. S.E 27
Applets & Graphics - Dr. S.E 28
Applets & Graphics - Dr. S.E 29
Applets & Graphics - Dr. S.E 30
Applets & Graphics - Dr. S.E 31
int x[]={20,120,200,20};
int y[]={20,120,30,20};
int n=x.length;
g.fillPolygon(x,y,n);
or
Polygon poly=new Polygon(x,y,n);
g.fillPolygon(poly);
or
Polygon poly=new Polygon();
poly.addPoint(30,30);
Applets & Graphics - Dr. S.E 32