Professional Documents
Culture Documents
CH5 JPR
CH5 JPR
2
Java Applets
• Built using one of general definitions of applets
– Applet class
– JApplet class
• Java applets
– Draws graphics in a defined screen area
– Plays sound
– Accept user input
– Create animation
– Enable user interaction with GUI elements
– It is a container which can hold GUI components
– An applet is a subclass of Panel
– There is no main method.
3
Applets
• Applet
– Program that runs in
• appletviewer
• Web browser
– Executes when HTML (Hypertext Markup Language)
document containing applet is opened
• For security reasons, applets run in a sandbox: they
have no access to the client’s file system
• Two types of Applet
1) Local Applet
2)Remote Applet
4
Applets
• Local Applet
– Developed loacally and stored in local system
– Internet Connection is not required
• Remote Applet
– Developed remotely and stored in remote system
connected to the Internet
– Internet Connection is required
– We must know the applets address on the web i.e
URL
The hierarchy of Applet
java.lang.Object
|
+----java.awt.Component
|
+----java.awt.Container
|
+----java.awt.Panel
|
+----java.applet.Applet
Applets and Web Pages – HTML
• Embedding Java applets
– Insert applet tags
<APPLET>
</APPLET>
• Call the specific applet by its file name
<APPLET CODE = "Whatever.class"
WIDTH = nnn HEIGHT = mmm>
<\APPLET>
Where nnn and mmm are specific pixel sizes
7
Applet methods
public void init ()
public void start ()
public void stop ()
public void destroy ()
public void paint (Graphics)
Also:
public void repaint()
public void update (Graphics)
public void showStatus(String)
public String getParameter(String)
Life Cycle of Applet
The applet states include:
1. Born or initialization
state
2. Running state
3. Idle or stopped state
4. Dead or Destroy state
public void init ( )
• Applet is in initialization state
• This is the first method to execute
• Executes only once in the applets life cycle
• place to initialize variables
• Load images, fonts or colors
• It is the best place to define the GUI Components
(buttons, text fields, scrollbars, etc.), lay them out,
and add listeners to them
• Almost every applet will have an init( ) method
public void start ( )
• Called after init( )
• Applet is in Running state
• May be called more than once in the applets life
cycle
• Called each time the page is loaded and restarted
• Used mostly in conjunction with stop( )
• start() and stop( ) are used when the Applet is
doing time-consuming calculations that you don’t
want to continue when the page is not in front
public void stop( )
• Called when the browser leaves the page
• Applet is in Idle state
• Called just before destroy( )
• May be called more than once in the applets life
cycle
• When we leave the page containing currently
running applet, stop() method called
automatically.
• Used mostly in conjunction with start()
public void destroy( )
• Called after stop( )
• Applet is in dead or destroy state
• Executes only once in the applets life cycle
• Applet is removed from memory
• Used to explicitly release system resources
(like threads)
• System resources are usually released
automatically
public void paint(Graphics g)
• Applet is in display state
• Called immediately after the applet enters into
running state
• paint() method is inherited from Component class
• Needed if we want to do any drawing or painting
• Displays the result of applet code on the screen
• It requires Graphics object as an argument
• Never call paint(Graphics), call repaint( )
repaint( )
Applet does not use main() method Application use main() method for
for initiating execution of code initiating execution of code
Applet cannot run any program from Application can run any program
local computer from local computer
Applet are restricted from using Application are not restricted from
libraries from other language such as using libraries from other language
C or C++ .
Applet tag
•The HTML <applet> tag specifies an applet.
The Applet access their attributes using the getParameter method. The
syntax is :
String getParameter(String name);
import java.awt.*;
import java.applet.*;
/*<Applet code = HelloUser.class width = 400 height = 400>
<PARAM NAME = "username " VALUE = "VVP" > </Applet>*/
public class HelloUser extends Applet
{
String str;
public void init()
{
str = getParameter("username");
str = "Hello "+ str;
}
public void paint(Graphics g)
{
g.drawString(str,10,100);
}
}
Graphics class
• AWT supports rich set of graphics methods.
• The origin of each window is at the top-left
corner and it is (0,0).
• Co-ordinates are specified in pixels.
• Graphics context is encapsulated by Graphics
class.
• It is passed to an applet when paint() or
update() is called.
Coordinate System
origin of each window
(top-left corner)
(0,0) (width,0)
1) drawString( )
• To draw string.
• Syntax:
void drawString(String str, int x, int y)
• Example:
g.drawString(“Hello”,50,100);
Graphics class methods
2) drawLine( )
• To draw line
• Syntax:
void drawLine(int startX,int startY,int endX, int endY)
• Example:
g.drawLine(10,10,50,50);
Graphics class methods
3) drawOval( )
• To draw an Ellipses or circles
• To draw Circle, width and height must be same.
• Syntax:
void drawOval(int top, int left, int width, int
height)
• Example:
g.drawOval(10,10,50,50);
Graphics class methods
4) fillOval( )
• To fill an Ellipses or circles
• To fill Circle, width and height must be same.
• Syntax:
void fillOval(int top, int left, int width, int height)
• Example:
g.fillOval(10,10,50,50);
Graphics class methods
5) drawRect( )
• To draw an rectangle or square
• To draw square, width and height must be same.
• Syntax:
void drawRect(int top, int left, int width, int
height)
• Example:
g.drawRect(10,10,50,50);
Graphics class methods
6) fillRect( )
• To fill an rectangle or square
• To fill square, width and height must be same.
• Syntax:
void fillRect(int top, int left, int width, int height)
• Example:
g.fillRect(10,10,50,50);
Graphics class methods
7) drawRoundRect( )
• To draw an rounded rectangle or square
• Syntax:
void drawRoundRect(int top, int left, int width, int height,
int xDiam, int yDiam)
• Example:
g.drawRoundRect(10,10,50,50,10,10);
Graphics class methods
8) fillRoundRect( )
• To fill an rounded rectangle or square
• Syntax:
void fillRoundRect(int top, int left, int width, int height,
int xDiam, int yDiam)
• Example:
g.fillRoundRect(10,10,50,50,10,10);
Graphics class methods
9) drawPolygon( )
• To draw any arbitrarily shaped figures
• Syntax:
void drawPolygon(int x[],int y[],int numPoints)
• Example:
int x[]={50,100,150};
(100,50)
int y[]={150,50,150};
g.drawPolygon(x,y,3);
(50,150) (150,150)
Graphics class methods
10) fillPolygon( )
• To fill any arbitrarily shaped figures
• Syntax:
void fillPolygon(int x[],int y[],int numPoints)
• Example:
int x[]={50,100,150};
(100,50)
int y[]={150,50,150};
g.fillPolygon(x,y,3);
(50,150) (150,150)
Graphics class methods
void fillArc(int top, int left, int width, int height, int
startAngle,sweepAngle)
drawArc( ) and fillArc()
• start_angle is starting angle of arc
• sweep_angle is degree around the arc
• It is drawn from startAngle through the angular
distance specified by sweepAngle.
• It is drawn anticlockwise if sweepAngle is positive
and clockwise if sweepAngle is negative.
900
1800 1800 00
2700
• Example: g.drawArc(10, 10, 30, 40, 40, 90);
900
450
1800 00
-1350
2700
Color class
• Java deals with color through the Color class
defined in java.awt package
• provides methods for setting the current
foreground and background colors
• color model uses 24-bit color
• color is represented as a combination of red,
green, and blue values
• Each component of the color can have a
number between 0 and 255
• For ex: 0,0,0 is black, 255,255,255 is white
Using Color objects
• The Color class defines a set of standard color
objects. Color Name RGB Value
Color.white 255,255,255
Color.black 0,0,0
Color.lightGray 192,192,192
Color.gray 128,128,128
Color.darkGray 64,64,64
Color.red 255,0,0
Color.green 0,255,0
Color.blue 0,0,255
Color.yellow 255,255,0
Color.magenta 255,0,255
Color.cyan 0,255,255
Color.pink 255,175,175
Color.orange 255,200,0
Using Color objects
• We can use any combination of red, green,
and blue values to construct a color object
• Constructor:
Color(int redValue,int greenValue,int blueValue)
• For ex:
– create a new color object:
Color c = new Color(140,140,140);
Setting background and foreground colors
• set the background and foreground colors using the
setBackground() and setForeground() methods.
• Both of these methods are defined in the
java.awt.Component class
1) setBackground()
• The setBackground() method sets the background
color of the applet.
• It takes a single argument, a Color object:
• Syntax: setBackground(Color c)
• For ex:
setBackground(Color.green);
Setting background and foreground colors
2) setForeground()
• The setForeground() method sets the
foreground color of the applet.
• It affects everything that has been drawn on
the applet
• It takes a single argument, a Color object:
• Syntax: setForeground(Color c)
• For ex:
setForeground(Color.red);
Setting current colors
3) setColor()
• to set the current color to be that color object,
Use the setColor() method (a method for
Graphics objects)
• After setting the current color, all drawing
operations will occur in that color.
• It takes a single argument, a Color object:
• Syntax: setColor(Color c)
• For ex: g.setColor(Color.green);
Getting colors
4) getColor() : to retrieve the current graphics
color, use the getColor() method (a method for
Graphics objects)
• Syntax: Color getColor()
• For ex: Color c=g.getColor();
5) getBackground()
• to retrieve the current background color, use
getBackground() method.
• Syntax: Color getBackground()
• For ex: Color c=getBackground();
Getting colors
6) getForeground()
• to retrieve the current foreground color, use
getForeground() method
• Syntax: Color getForeground()
• For ex: Color c=getForeground();
Font class
• Font class defined in java.awt package
• The Font class represents a given font-its name,
style, and point size.
• Fonts class requires foll. 3 things:
import java.awt.*;
import java.applet.*;
import java.awt.*;
import java.applet.*;
Font fnt[]=ge.getAllFonts();
int y = 20;
for(int i=0; i <fnt.length; i++)
{
g.drawString(fnt[i]+"", 10, y);
y += 20;
}
}
}