0% found this document useful (0 votes)
25 views38 pages

UNIT-V Applets

Uploaded by

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

UNIT-V Applets

Uploaded by

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

UNIT-V 1

Introduction:

• An applet is a special Java program that can be embedded in HTML


documents.

• It is automatically executed by (applet-enabled) web browsers.

• In Java, non-applet programs are called applications.

UNIT-V 2
Life Cycle:

• init
– To initialize the applet each time it's loaded (or reloaded).
• start
– To start the applet's execution, such as when the applet's loaded
or when the user revisits a page that contains the applet.
• stop
– To stop the applet's execution, such as when the user leaves the
applet's page or quits the browser.
• destroy
– To perform a final cleanup in preparation for unloading.

Not every applet needs to override every one of these methods.

UNIT-V 3
Life Cycle:

start( )

init( ) destroy( )

stop( )

UNIT-V 4
paint( ):

paint( )

repaint( )

update( )

UNIT-V 5
Hierarchy:

Component

Container

Window Panel

Frame Applet

UNIT-V 6
Sample Applet Program:
import java.applet.*;
import java.awt.*;
/*
<applet code="Sample1" width=500 height=500>
</applet>
*/
public class Sample1 extends Applet {
String msg;
public void init( ) {
msg="Hello";
}
public void start( ) {
msg+="Start";
}
public void paint(Graphics g) {
msg+="paint";
g.drawString(msg,50,50);
}
} UNIT-V 7
HTML program to run an Applet on Webbrowser:

<!– Simple html Program -->


<html>

<head>
<title> The Life Cycle of an Applet </title>
</head>

<body>
<applet code=“Sample1" width="500“ height=“500">
</applet>
</body>

</html>

UNIT-V 8
Alternate way to run an Applet:

C:\...\>appletviewer Sample1.java

UNIT-V 9
The <APPLET> Tag:

< APPLET
[CODEBASE = codebaseURL]
CODE = appletFile
[ALT = alternateText]
[NAME = appletInstanceName]
WIDTH = pixels
HEIGHT = pixels
[ALIGN = alignment]
[VSPACE = pixels]
[HSPACE = pixels]
>
[< PARAM NAME = appletParameter1 VALUE = value >]
[< PARAM NAME = appletParameter2 VALUE = value >]
...
[alternateHTML]
</APPLET>

UNIT-V 10
Methods of Applet class:
Applet class is in java.applet package.

Some methods: void resize(int width, int height)


void init() void resize(Dimension d)
void start()
void stop()
void destroy()
URL getCodeBase()
URL getDocumentBase()
String getAppletInfo()
String getParameter(String pname)
String [ ] [ ] getParameterInfo()
boolean isActive()
UNIT-V 11
void showStatus(String str)
Working with colors:

To set colors for a component, Component class have defined two methods:

void setBackground(Color newColor)

void setForeground(Color newColor)

Color class defines constants and constructors to work with colors.

UNIT-V 12
Color class:
Constants defined in Color class are: Constructors defined in Color class are:
Color.black
Color(int red, int green, int blue)
Color.blue
Color(int rgbValue)
Color.cyan
Color(float red, float green, float blue)
Color.gray
Color.darkGray Methods defined in Color class are:

Color.lightGray int getRed()


Color.green int getGreean()
Color.magenta int getBlue()
Color.orange int getRGB()
Color.pink
Color.red
Color.white
UNIT-V 13
Color.yellow
Graphics class:
Graphics class is in java.awt package.
Methods:
void drawLine(int startX, int startY, int endX, int endY)
void drawRect(int top, int left, int width, int height)
void fillRect(int top, int left, int width, int height)
void drawRoundRect(int top, int left, int width, int height, int xDiam, int yDiam)
void fillRoundRect(int top, int left, int width, int height, int xDiam, int yDiam)
void drawOval(int top, int left, int width, int height)
void fillOval(int top, int left, int width, int height)
void drawArc(int top, int left, int width, int height, int startAngle, int sweepAngle)
void fillArc(int top, int left, int width, int height, int startAngle, int sweepAngle)

void drawPolygon(int x[ ], int y[ ], int numPoints) void setColor(Color newColor)


void fillPolygon(int x[ ], int y[ ], int numPoints)
UNIT-V Color getColor( ) 14
Working with fonts:

To set a different font for a component, Component class have defined one
method:

void setFont(Font fontObj)

UNIT-V 15
Font class:
The general form of Font class constructor is:
Font(String fontName, int fontStyle, int pointStyle)
fontName can be Dialog, DialogInput, Sans Serif, Serif, Monospaced & Symbol.
fontStyle can be any of the constants: Font.PLAIN, Font.BOLD, Font.ITALIC.
fontStyle can be combine style in the form Font.PLAIN|Font.ITALIC.

Methods of Font class:


static Font decode(String str) int getSize()
boolean equals(Object fontObj) int getStyle()
String getFamily() int hashCode()
static Font getFont(String pr) boolean isBold()
static Font getFont(String pr,Font dfFont) boolean isItalic()
String getFontName() boolean isPlain()
String getName() UNIT-V String toString() 16
AWT Controls:
Controls are components that allow a user to interact with our application.

The AWT supports the following types of controls:

• Labels
• Buttons
• Check boxes
• Choice lists
• Lists
• Scroll bars
• Text editing

All these controls are subclasses of Component class.

UNIT-V 17
Adding Controls:
Steps to add a control to a container (Applet or Frame):

1. Create an instance of the desired control.


2. Add it to the container by calling the add method defined in Container class.
Component add(Component obj)
obj is the instance of the control.
Once a control is added, it will automatically visible on the container.

UNIT-V 18
Removing Controls:

To remove a control, Container class defined a method:


void remove(Component obj)

UNIT-V 19
Label:
Labels are passive controls that do not support any interaction with the user.

Label class Constructors: Methods:


Label( ) void setText(String str)
Label(String str) String getText( )
Label(String str, int how) void setAlignment(int how)
str is the text of the Label int getAlignment( )
how specifies the alignment
Label.LEFT
Label.RIGHT
Label.CENTER

UNIT-V 20
Button:
A button (push button) is a component that contains a label and that generates
an event when it is pressed.

Button class Constructors:


Button( )
Button(String str)

Methods:
void setLabel(String str)
String getLabel( )

UNIT-V 21
Checkbox:
A check box is a control that is used to turn an option on or off (check mark).
Each time a check box is selected or deselected, an item event is generated.
Checkbox class Constructors:
Checkbox( )
Checkbox(String str)
Checkbox(String str, boolean on)
Checkbox(String str, boolean on, CheckboxGroup cbg)
Checkbox(String str, CheckboxGroup cbg, boolean on)
Checkbox methods: CheckboxGroup methods:
void setLabel(String str) Checkbox getSelectedCheckbox( )
String getLabel( ) void setSelectedCheckbox(Checkbox
cb)
void setState(boolean on)
boolean getState( ) UNIT-V 22
Choice:
The Choice is used to create a pop-up list of items.
Each time a choice is selected, an item event is generated.
Choice class contains only the default constructor.
Choice methods:
void addItem(String name)
void add(String name)
String getSelectedItem( )
int getSelectedIndex( )
int getItemCount( )
void select(int index)
void select(String name)
String getItem(int index)
UNIT-V 23
List:
The List provides a compact, multiple-choice, scrolling selection list.
A list shows a number of choices in the visible widow. It also allow multiple
selections.

List constructors:
List( )
List(int numRows)
List(int numRows, boolean multipleSelect)

List methods: String[ ] getSelectedItems( )


void add(String name) int[ ] getSelectedIndexes( )
void add(String name, int index) int getItemCount( )
String getSelectedItem( ) void select(int index)
int getSelectedIndex( ) String getItem(int index)
UNIT-V 24
List:
Each time an item in the list is selected or deselected with a single click, an
item event is generated.
When a list item is double-clicked, an action event is generated.

UNIT-V 25
Scrollbar:
Scrollbars are used to select continuous values between a specified minimum
and maximum.
Scrollbar constructors:
Style can be,
Scrollbar( )
Scrollbar.VERTICAL
Scrollbar(int style)
Scrollbar.HORIZONTAL
Scrollbar(int style, int initialValue, int thumbSize, int min, int max)
Scrollbar methods:
void setValues(int initialValue, int thumbSize, int min, int max)
int getValue( )
void setValue(int newValue)
int getMinimum( )
int getMaximum( )
void setUnitIncrement(int newIncr)
UNIT-V 26
void setBlockIncrement(int newIncr)
TextField:
TextField is a single line text entry area. TexField is subclass of TextComponent.

TextField constructors:
TextField( )
TextField(int numChars)
TextField(String str)
TextField(String str, int numChars)
TextField methods:
int getSelectionStart( )
String getText( )
int getSelectionEnd( )
void setText(String str)
void setEchoChar(char ch)
String getSelectedText( )
boolean echoCharIsSet( )
void select(int startIndex, int endIndex)
char getEchoChar( )
boolean isEditable( )
UNIT-V 27
void setEditable(boolean canEdit)
TextArea:
TextArea is a multiline editor. TextArea is subclass of TextComponent.

TextArea constructors:
TextArea( )
TextArea(int numLines, int numChars)
TextArea(String str)
TextArea(String str, int numLines, int numChars)
TextArea(String str, int numLines, int numChars, int scrBars)

scrBars can be
SCROLLBARS_BOTH
SCROLLBARS_NONE
SCROLLBARS_HORIZONTAL_ONLY
SCROLLBARS_VERTICAL_ONLY
UNIT-V 28
TextArea:

TextArea methods:
String getText( )
void setText(String str)
String getSelectedText( )
void select(int startIndex, int endIndex)
boolean isEditable( )
void setEditable(boolean canEdit)
int getSelectionStart( )
int getSelectionEnd( )
void append(String str)
void insert(String str, int index)
void replaceRange(String str, int startIndex, ine endIndex)
UNIT-V 29
Menu Bars and Menus:
Menus in Java are implemented by the use of the following classes:
MenuBar
Menu
MenuItem
CheckboxMenuItem

To create a menu bar, just create an instance on MenuBar class.


MenuBar class defines only the default constructor.

UNIT-V 30
Menu:
Constructors:
Menu( )
Menu(String optionName)
Menu(String optionName, boolean removable)

MenuItem:
Constructors:
MenuItem( )
MenuItem(String itemName)
MenuItem(String itemName, MenuShortcut keyAccel)

UNIT-V 31
CheckboxMenuItem:
Constructors:
CheckboxMenuItem( )
CheckboxMenuItem(String itemName)
CheckboxMenuItem(String itemName, boolean on)

MenuShortcut:

Constructors:

MenuShortcut(int key)

MenuShortcut(int key, boolean useShiftMidifier)

UNIT-V 32
Layout Managers:
A layout manager is used by a container to position each of the components
with in it.
Each container has a layout manager associated with it.
A layout manager is an instance of any class that implements the interface
LayoutManager.
The layout manager is set by:
void setLayout(LayoutManager layoutObj)
Java defines the following layout manager classes:
1. FlowLayout
2. BorderLayout
3. GridLayout
4. CardLayout
5. GridbagLayout
UNIT-V 33
FlowLayout:
FlowLayout is the default layout manager.
With this components are laid out from the upper-left corner, left to right and
top to bottom line by line.

Constructors:
FlowLayout( )
FlowLayout(int how)
FlowLayout(int how, int horz, int vert)
Valid how values are
FlowLayout.LEFT
FlowLayout.CENTER
FlowLayout.RIGHT

UNIT-V 34
BorderLayout:
With BorderLayout, a conatiner has four narrow fixed-width components at
the edges and one large are in the center. The four sides are referred to as
north, south, east and west. The middle area is called the center.

Constructors:
BorderLayout( )
BorderLayout(int horz, int vert)
To add components use the following add method:
void add(Component compObj, Object region)
The region can be
BorderLayout.CENTER
BorderLayout.EAST
BorderLayout.WEST
BorderLayout.NORTH
UNIT-V 35
BorderLayout.SOUTH
GridLayout:
GridLayout lays out components in a two-dimensional grid.

Constructors:
GridLayout( )
GridLayout(int numRows, int numColumns)
GridLayout(int numRows, int numColumns, int horz, int vert)

UNIT-V 36
CardLayout:

Constructors:
CardLayout( )
CardLayout(int horz, int vert)
To add cards to a panel:
void add(Component panelObj, Object name)
Methods:
void first(Conatiner deck)
void last(Conatiner deck)
void next(Conatiner deck)
void previous(Conatiner deck)
void show(Conatiner deck, String cardName)

UNIT-V 37
GridbagLayout

Sometimes we need a tabular arrangement(Grid) of the components where


columns have different sizes or one component spans multiple columns. To
handle these situations, a complex layout manager called GridbagLayout is
used.

UNIT-V 38

You might also like