Vous êtes sur la page 1sur 42

Applet

introduction
 In todays era of internen, we need to write
web based applications that can be
embedded in a webpage.
 For this purpose java introduces another style
of programming called Applet.
What is an applet
 An applet is a program that appears embedded in a
web document.
 it is meant to be hosted by a web browser, which
provides a home for the applet.
 Any applet depends on a java capable browser in
order to run it.
 It can also be viewed using a tool called the
appletviewer.
 Java applets are embedded in a web page using
HTML.
Applet and Applications
Applets Applications

1. Applets can be embedded Applications have no special


in html pages and support in HTML for
downloaded over the internet embedding of downloading.
or intranet.

2. Applets can only be Applications can be executed


executed inside a java from the command line with
compatible container, such as the help of an executed file
a modern web Browser. called java.exe
Applets execute under strict Application have no inherent
security limitations that disallow security restrictions
certain operations, such as
accessing files or systems
services on the user’s computer

Applets are the programs Applications are standalone


written especially for distribution programs and require main()
over a network. These programs method for their execution
are not standalone and do not
require main() method for their
execution
Creating Applets
 To create am applet you have to use a class
called Applet, which is present inside the
package java.applet.
 The applet class must be superclass of all
applets that are embedded on a web page.
A simple program
import java.applet.Applet;
import java.awt.*;

public class first extends Applet


{
public void paint(Graphics g)
{
g.drawString("hello world",50,100);
}
}
Html
<html>
<body>
<applet code="first" width= 400 height=200>
</applet>
</body>
</html>
Appletviewer
 We can also run an applet in appletviewer

 Appletviewer first.java
 Appletviewer trial.html
Applets and HTML
<applet
code=class file name
Codebase=url
Width=n
Height=n
HSPACE=n
VSPACE=n>
</applet>
CODE and CODEBASE
 CODE
 Code is used to indicate the name of the class file
that holds the current applet.
 If code alone is used in the <APPLET> tag, the
class file is searched for is searched for in the
same directory as the HTML file that references
it.
 CodeBase
 CodeBase is used to indicate pathname where
classes are contained.
HSPACE and VSPACE
 The Hspace and Vspace attributes set the
amount of space, in pixels, between an applet
and its surrounding text.
 Hspace controls the horizontal space
 Vspace controls the vertical space
WIDTH and HEIGHT
 These attributes are mandatory and specify
the width and height of an applet in pixel.
Basic applet life cycle
Init()

start()

paint()

stop()

destroy()
Basic applet life cycle
 An applet context calls methods in the applet
at appropriate times during the life cycle of
the applet .
To initialize the applet
 Whenever the applet is loaded initialization process
occurs by default.
 Initialization may contain
 creating the objects the applet needs,
 loading images or fonts ,
 setting up an initial state or
 setting up parameters.
 To provide the behavior for the initialization of an applet
we have to override the init() method :-
public void init()
{
//code for the init method
}
To start the applet
 Starting occurs only after the applet is initialized or
the applet was previously stopped.
 Starting can occur several times in an applet’,
lifecycle but initialization occurs only once.
 In order to provide startup behavior to the applet we,
override the start() method.
Public void start()
{
//code for start method
}
To make an applet
 This is done through the method paint().
 This method helps in displaying the text or
drawing an image on to the applet.
 It takes an argument , which is the instance of
the java.awt.Graphics class.
Public void paint( Graphics g)
{
//code for paint
}
Paint()
Paint method is invoked automatically by the
applet context only for the first time.

In order to invoke the paint() method again , you have to


use repaint() method.

The repaint method() internally calls the update()


method to clear the screen of an applet.

The update method in turn calls the paint method to


repaint the contents on the current frame
To stop the applet
 Stopping occurs under two conditions
 When the user leaves the page that contains a
currently running applet.
 When an applet stops itself by calling stop()
method directly.
Public void stop()
{
//code for stop method
}
To destroy an applet

 Destruction enables the applet to cleanup just before the browser


exits or the applet is freed from memory
 This is done by destroy() method.
 The applet context calls the stop method prior to calling the destroy
method.
 All resources are destroyed when the browser terminates.
 In order to enable cleanup behavior for the applet we must override
the destroy() method .
Public void destroy()
{
// code for the destroy method.
}
public class first extends Applet {
String s="the";
public void init() {
s=s+"new";
System.out.println("inside paint");
}
public void start() {
s=s+"applet";
System.out.println("inside start");
}
public void stop() {
s=s+"stops";
System.out.println("inside stop");
}
public void destroy() {
s=s+"here";
System.out.println("inside destroy");
}
public void paint(Graphics g) {
g.drawRect(25,25,100,100);
g.drawString(s,25,40);
}
}
/* <applet code = "first.class" codebase= "c:\" width= 400 height=200> </apple> */
Working with parameters in an applet
 Applets can get different inputs from the html
file that contains the <APPLET> tag through
the use of applet parameters.
 To setup and handle parameters in an applet
you need two things:
 A special parameter tag in the HTML file
called the <param> tag.
 A code in your applet to parse those
parameters.
Using <param> tag in an applet
 Applet parameters come in two parts:
 A user defined name which indicates what that
parameter is and
 a value which determines the value of that particular
parameter.

 You can indicate the color of the text in an applet by


using a parameter with the name color and value red

 You can determine an animation’s speed using a


parameter with the name speed and value 5
Applet tag

<applet code = "first.class" width= 400 height=200>

<param name = "font" value= "TimesNewRoman">


<param name = "size" value="36">

</applet>
Receiving parameters passed by <param> tag

 Inside applet , you need the values passed


through the param tags with the getParameter()
method of the java.applet.Applet
 This method takes only one argument that is the
name of parameter.
 public class first extends Applet
 {
 String font;
 String size;
 public void init()
 {
 System.out.println("inside paint");

 }
 public void paint(Graphics g)
 {
 font=getParameter("font");
 size=getParameter("size");
 Font f=new
Font(font,Font.BOLD,Integer.parseInt(size));
 g.setFont(f);
 g.setColor(Color.red);
 g.drawString("parameter",25,40);
The Color class
 The Color class contains methods that control
the colors of the user interface components,
change and query the existing foreground
and background colors.
 There are 13 predefined colors designated by
the constants in the Color class:
 Color.black, Color.blue, Color.cyan,
Color.darkGray, Color.gray, Color.green…….
 The color class defines various constructors.
The most popular constructor is:
 Color( int red, int green, int blue);
 R,g,b can be between 0 to 255
 If all are 0 it means black
 If all are 255 it means white
Setting colors
 Certain methods use the object of color class to set
the foreground and background colors of an applet.
 setColor(Color.blue);
 This method is declared in Graphic class. It is used to

change foreground color.


 setBackground(Color.red)
 This is the method which is inherited by the Applet

class to change the background color.


 setForeground(Color.red)
 This is the method which is inherited by the Applet

class to change the Foreground color.


Determining the color
 The following methods return the object of the
class which represents the color of the
background and foreground
 getForeground()
 It returns the foreground color of an applet in
RGB format
 getBackground()
 It returns the Background color of an applet in
RGB format
The font class
 The font class allows the programmer to set
and use fonts in java programs.
 The graphics class enables java to print text
on the screen , in conjunction with the font
class.
Creating font object
 To change the default font to the desired one,
create an instance.
 Font f= new Font(“Helvetica”,Font.BOLD,14);
 If java cannot find the specified font it will use
the default font( usually Courier).
 Font styles are constants defined by the font
class and can be used as Font.PLAIN,
Font.BOLD, Font.ITALIC.
 The two styles can be used together using +
symbol as Font.BOLD+Font.ITALIC
Font methods
getName() Returns the name of current font

getSize() Returns the current font size

getStyle() Returns the current font style

isPlain() Returns true or false


isItalic()
isBold()
Getting and setting fonts
 getFont();
 setFont();
Graphics class
 Graphics in java.awt package encapsulates a
small collection of rendering primitives, that
can be used dynamically to generate images
at runtime.
 Drawing in java is based on a coordinate
system of pixels.
 Graphics class does not have a constructor.
 Drawing graphics in java can be done in the
method of any other method except init()
because paint overwrites the contents.
Graphics methods
 To draw string
 drawString(String s, int col, int row);
 To draw Lines
 drawLines( int x, int y, int x1, int y1);
 To draw rectangle
 drawRect( int x, int y, int height, int width);
 To draw filled rectangle
 fillRect( int x, int y, int height, int width);
 To draw Round Rectangle
 drawRoundRect( int x, int y, int height, int width, int
arcwidth, int archeight);
Graphics methods
 To draw Ovals
 drawOval(int x, int y, int height, int width);
 To draw filled Oval
 fillOval( int x, int y, int height, int width);
 To draw Arcs
 drawArc( int x, int y, int height, int width, int
startangle, int arcangle);
Overriding update()
public void update( Graphics g)
{
}
public void paint( Graphics g)
{
update(g);
}
Applets writes to its window only when its update() or
paint() method is called by the awt.
Repaint()
 Repaint method() is defined by the awt .
 It causes the awt runtime system to execute a
call to your applet’s update() method, which , in
its default implementation calls paint().
 Repaint method has four forms
 Void repaint();
 Void repaint( int left, int top, int width, int height);
 Void repaint( long maxdelay);
 Void repaint( long maxdelay, int left, int top, int
width, int height);
Using status window
 showStatus( String s);
getDocumentBase() & getCodeBase()

Vous aimerez peut-être aussi