applet. 2 introduction to java applet programs applications are stand alone programs ◦ executed...
Post on 05-Jan-2016
249 Views
Preview:
TRANSCRIPT
APPLET
2
Introduction to Java Applet Programs
Applications are stand alone programs◦executed with Java interpreter
Applet is a small program◦can be placed on a web page◦will be executed by the web browser◦give web pages “dynamic content”
Advantages of Applet
There are many advantages of applet. They are as follows:It works at client side so less response
time.SecuredIt can be executed by browsers running
under many plateforms, including Linux, Windows, etc
4
Java AppletsJava applets are usually graphical
◦Draw graphics in a defined screen area◦Enable user interaction with GUI elements
Applet class is one of the Abstract Windowing Toolkit (AWT) components
6
Java AppletsApplets are Java programs that can be
embedded in HTML documents◦ To run an applet you must create a .html file which
references the applet◦ Ready to Program also will run an applet
When browser loads Web page containing applet◦ Applet downloads into Web browser◦ begins execution
Can be tested using appletviewer program
7
AppletsApplet
◦ Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator)
◦ Executes when HTML (Hypertext Markup Language) document containing applet is opened
◦ Applications run in command windows
8
Applets and Web Pages – HTML Applets embedded in a web page
◦Executed when web page loaded by browserWeb pages structured with HTML codes
◦HyperText Mark-up LanguageSyntax<command> . . .</command>
Turns format onTurns format on
Turns the format offTurns the format off
9
Applets and Web Pages – HTMLEmbedding Java applets
◦Insert applet tags<APPLET></APPLET>
Call the specific applet by its file name<APPLET CODE = "Whatever.class" WIDTH = nnn HEIGHT = mmmm><\APPLET>
Where nnn and mmm are specific pixel sizes
10
Applets and Web Pages – HTML
Create the web page code using a text editor
Save it with an .html suffix
Open this file with appletviewer or with a web browser that supports Java
Java Plug-in must be installed (part of J2SDK 1.4.1 from Sun)
<HTML>
<HEAD>
</HEAD>
<BODY>
<APPLET CODE = . . . >
</APPLET>
</BODY>
</HTML>
11
Applets and Web Pages – HTMLClient Web browser anywhere can access
this web page from its host serverEmbedded Java applet runs on client
browser (of any type platform)This means a client anywhere on any type
of platform can run a piece of software developed on any other type of platform
Platform Independence
The following is a simple applet named HelloWorldApplet.java:
import java.applet.*;
import java.awt.*;
public class HelloWorldApplet extends Applet {
public void paint (Graphics g) {
g.drawString ("Hello World", 25, 50);
}
}
These import statements bring the classes into the scope of our applet class:
java.applet.Applet.java.awt.Graphics.Without those import statements, the Java
compiler would not recognize the classes Applet and Graphics, which the applet class refers to.
Invoking an Applet:
An applet may be invoked by embedding directives in an HTML file and viewing the file through an applet viewer or Java-enabled browser.
The <applet> tag is the basis for embedding an applet in an HTML file.
Below is an example that invokes the "Hello, World" applet:
<html>
<title>The Hello, World Applet</title> <hr>
<applet code="HelloWorldApplet.class" width="320" height="120">
If your browser was Java-enabled, a "Hello, World" message would appear here.
</applet>
<hr> </html>
The code attribute of the <applet> tag is required. It specifies the Applet class to run.
Width and height are also required to specify the initial size of the panel in which an applet runs.
The applet directive must be closed with a </applet> tag.
Non-Java-enabled browsers do not process <applet> and </applet>. Therefore, anything that appears between the tags, not related to the applet, is visible in non-Java-enabled browsers.
Lifecycle of Java Applet
Applet is initialized.Applet is started.Applet is painted.Applet is stopped.Applet is destroyed.
Lifecycle methods for Applet:
The java.applet.Applet class 4 life cycle methods and java.awt.Component class provides 1 life cycle methods for an applet.
java.applet.Applet classFor creating any applet java.applet.Applet class must
be inherited. It provides 4 life cycle methods of applet.public void init(): is used to initialized 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 stop or browser is minimized.public void destroy(): is used to destroy the Applet. It
is invoked only once.
java.awt.Component classThe 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.
Passing parameters to an appletTo pass parameters to an applet, two
things are required- a special parameter tag in HTML file and the code in the applet to parse those parameters.
Import java.awt.*;
Import java.applet.*;
Public class ParamtestApplet extends Applet{
Font f=new Font(“Times Roamn”,Font.BOLD,40);
String name;
Public void init(){
name= getParameter(“name”);
If(name==null)
{
name=“friend”;
}
Name=“Have a nice day”+name;
}
Public void Paint(Graphics g){
g.setFont(f);
g.setColor(Color.darkGray);
g.drawString(name,50,50);
} }
ParamtestApplet.html
<APPLET CODE=“ParamtestApplet.class” CODEBASE=“/test” WIDTH=400 HEIGHT=400>
<PARAM NAME=name VALUE=“Abi”></APPLET>
Example of Applet by appletviewer tool:
//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet
{ public void paint(Graphics g)
{ g.drawString("welcome to applet",150,150);
} }
/*
<applet code="First.class" width="300" height="300">
</applet>
*/
To execute the applet by appletviewer tool, write in command prompt:
c:\>javac First.java
c:\>appletviewer First.java
import java.applet.Applet;
import java.awt.*;
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);
}
}
<html>
<body>
<applet code="GraphicsDemo.class" width="300" height="300">
</applet>
</body>
</html>
Methods used to load and display image in an applet
drawImage(Image, int x, int y, ImageObserver): is used draw the specified image at the given coordinates.Image referes to picture to be drawn.x,y are the coordinates where the image is to drawn.Image observer is usually the applet itself.
getImage(URL , String ):Loads an image from an URL.URL specifies the location from which the image is to be loaded.String is usually the name of the image file.
Helper functions in getImage() method
getDocumentBase(): is used to return the URL of the HTML document in which applet is embedded.
getCodeBase(): is used to return the path( URL) of the .class file.
import java.awt.*;
import java.applet.*;
public class DisplayImage extends Applet {
Image picture;
public void init() {
picture = getImage(getCodeBase(),“rabbit.gif");
}
public void paint(Graphics g) {
g.drawImage(picture, 30,30, this);
}
}
<html>
<body>
<applet code="DisplayImage.class" width="300" height="300">
</applet>
</body>
</html>
getImage(getCodeBase(),“rabbit.gif");
Loads the file rabbit.gif from the directory where the .class file is located and stores it in the instance variable img.The paint() method of the applet,displays
the image at the coordinates mentioned using drawImage() method.
The parameter this(the applet itself) passed in the drawImage() function calls the image observer which monitors the drawing of the image.
ClipplingClipping is a technique by which the
drawing area can be restricted to a small portion of the screen.A clipping region is an area where drwaing is allowed.
Clipper.java
Import java.awt.*;
Import java.applet.*;
public class clipper extends Applet{
public void paint(Graphics g){
g.clipRect(10,10,150,100);
G.setFont(new Font(“TimesRoman”,Font.ITALIC,28);
g.fillOval(00,60,80);
g.drawString(“Happy New Year”,50,30)
Clipper.html<APPLET CODE=“Clipper.class”
WIDTH=300 HEIGHT=150></APPLET>
top related