Java Assignment3
Java Assignment3
ASSigment 3
SDS-S-08J
cplet te ycle .
1g - 5. 2}
DraOin Pectangle
Draoing
3 Pecangle L sa2-
iline J
Ls58 -5.60
Creatin9 Choice
s L 5 8 3 -584, 5 -5.
Lajoue Nannage,
5.05
Rules
() Initialization state occurs only once in the ife c) CyOe
should be H
overridden in our apple.
5.06
(i) Running state
Rules
() start() method be called any number of
can
times.
That is, if we move from current
page to previous
page, the start() method automatically
brings that
applet in the page to running state.
Rules
(i) Dead state occurs only once in the life cycle.
i) Destroy() method is present in the base class Applet
and it should be overrided in our applet.
(v) Display state
This state is the fifth
stage of the applet life cycle.
This state comes when the
applet displays some thing on the
screen. This is achieved
by calling paint() method. The
general form is,
public void paint(Graphics obj)
5.08
Rules
paint() metho
can be called any number of times.
called only in th running
This can be state.
method is a must in all applets.
l
(ii)
oaint()
Example
(a) Graphics g;
g.drawRect (10, 10, 50, 20);
(10, 10) 50
20
/0
(b) Write a program to draw a red rectangle of width a
height 40
g.setColor(c1);
g.drawRect(10,10,70,40:
Where
X,Y-starting point
W width of the rectangle
Wand height h from the starting point (x, y). The arc
(x. y)
ah
aw W
Example
Graphics g:
g.drawRoundRect (10, 10, 50, 20, 10, 10)
10, 10 50
20
(i) 3D rectangle
draw3DRect() method is used to draw 3Drectanges
The general form is
void draw3DRect(intx, int y, int w, int h, boolean b)}:
Where:
Example
Graphics g:
g.drawRect (10, 10, 50, 20, true);
(10, 10) 50
20
Filling rectangle
X, y starting point
width of the
h
rectangle
height of the rectangle
Example
la Graphics g
gfilRect(10, 10,
50, 20)
5.23
mpot java.awt."
lappkt
importjiava.applet.":
cla fillrect extends Applet
public
g.setColor(c1);
g.fillRect(10,10,50,20);
x, y starting point
W width of the rectangle
h height of the rectangle
aw a r c width
ah arc height
tne
rmine
tNample
Graphics.g;
(a)
Rect(1 10, 50, 20, 10, 10);
.fillRound
Drawing a n d
filling 3D rectangle
Where
starting point
x, Y
width of the rectangle
W
Example
Graphics g
g.fill3DRect(10, 10, 50, 20, true);
Drawing and filling circles
Drawing circles
Event class name
e
object to receivethe
event object
generated
(MouseListenerinterface
This interface has five methods.
used to listen and process the different
These methods ar
such as mouse click, mouse nters
types of oUse are
a events
component, evie.
component, pressed or released. The general formo
Are
void mouseClicked (mouseEvent
e);
void mouseEntered (mouseEVent
e);
void mouseExited (mouseEvent e);
void mousePressed (mouseEvent e);
void mouseReleased (mouseEvent e);
Where
mouseClicked
mouseEntered
mouseExited method name
mousePressed
mouseReleased
mouseEvent event class name
generated
e the
object to receive
event object
5.42
Listener interface
K e y l L i
(vi)
This interface has three methods.
listen and process he These methods
Are u s e d t o
different types of
typed. Thekeyboard
as keypressed, released and
nts such
forms are general
void keyPressed (keyEvent e);
void keyReleased (keyEvente);
void key1yped (keyEvent e);
Where
keyPressed
obj.add("Name");|
Where
- choice object
obj
name - name of the item to add
Method Description
String getSelectedltem()| This method is used to read the
selected item from the list
Layout managers
Layout managers are used to arrange o
() FlowLayout
This is the default manager. This is used to
layout
the GUI components from left to right
in the order they
place
are added in the container.
Constructors in flowlayout
Constructor Description_
This creates a default
flow layout. Five
FlowLayout()
are left between
pixels of space
components.
Components
the
FlowLayout(int L )| This is used to place
value of L. L can
as indicated by
the
value.
of the following
take any one
5.84
FlowLayout.LEFT -
to align left
FlowLayout.RIGHT -
to align right
FlowLayout.CENTER to align center
FlowLayout(int L, The value of h and L is used to specity
int h, int v) the horizontal and vertical space
between components.
(ii) BorderLayout
This layout manager is used to arrange the Gul
Constructor Description
BorderLayout() This creates a default border layout.
BorderlLayout(int L | This is used to place the components
as indicated by the value of L.L can
take any one following value
of the
BorderLayout.CENTER to align center
BorderLayout.NORTH t o align north
BorderLayout.SOUTH to align south
BorderLayout.EAST toalign east
BorderLayout.WEST to align west
5.86
Where
componentobject
-
region
-
five regions such BorderlLayout
as
Example
C
iii) GridLayout
This layout manager is used to divide the window into
a grid so that components can be placed in rows
and
columns.
Constructors in gridlayout
Constructor Description
GridiLayout() This creates a single column grdlayout