0% found this document useful (0 votes)
3 views

Java Assignment3

Gajjsisjwksuwjiwhwiwhjsjjsjhjsjsjhjsjsjj.

Uploaded by

darkdevilmukilan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Java Assignment3

Gajjsisjwksuwjiwhwiwhjsjjsjhjsjsjhjsjsjj.

Uploaded by

darkdevilmukilan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Javg

ASSigment 3

SDS-S-08J
cplet te ycle .

1g - 5. 2}
DraOin Pectangle
Draoing

3 Pecangle L sa2-
iline J

MoLie -3}ener inerfaue


key Lstener Intertque
C41-542

Ls58 -5.60
Creatin9 Choice
s L 5 8 3 -584, 5 -5.
Lajoue Nannage,
5.05

(ii) Run the


compiled file
webbrowser opletviewer of aan
using appiets.

Applet life cycle


The different stages involve in the life
( ) applet are cycle t
(i) Initialization state

(ii) Running state

(ii) Idle or stopped state


(iv) Dead state

(v) Display state

(i) Initialization state


This state is the first state in the
is used to
applet life cycle ac
initialize
our applet variables.
done with the
Applet initializaicn s
help of init( ) method. The general form s
public void init()

Rules
() Initialization state occurs only once in the ife c) CyOe

() init() method is present in the base class


4ppleta"

should be H
overridden in our apple.
5.06
(i) Running state

This state comes


automatically after initialization state.
tisthe second stage of the applet life cycle. This state comes
when start() method Is called. The
general form is.
public void start()

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.

(ii) start() method is present in the base class


Applet
and it should be overrided in our
applet
ii) Idle or stopped state
This state is the third stage of the
applet life cycle.
This state comes when
stop() method is called implicitly or
explicitly.stop() method is called implicitly when we move from
one page to another. The general form is,
public void stop()
5.07

Rules the running state


ate.
called only in
is
() stop/)
method
number of time.
be
called any mes.
can
method
() stop()
present in the
base lass
class Applet
An.
method is
(ii) stop() overridden
in our applet
should be
and it

(iv) Dead state

the fourth stage of the applet life cycle.


cu
This state is

This state comes when destroy() method is called alled. In thi


from the memon.
state the applet is completely
removed
memory. The
general form is,
public void destroy()

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()

paint() method is present in the base class Applet


(iv) overrided in
and it should be
our applet.

All the above applet states can be shown in a transition


as given below.
diagram
stop ()

Running Idle dead


Initialization display
destory ()
start ()
start ()
paint ()
Drawing and filling rectangles
Drawing rectangle
We can draw the following types of rectangles using
the methods
available in graphics class. They are
5.19

() Sharp corner rectangle


(i) Round corner reclangle
(ii) 3D rectangle

(i) Sharp corner rectangle

drawRect() method is used to


draw
form is
sharp Cofe
oner
rectangles. The general
void drawRect(int x, int y, int w, int h); |
Where
x, y-starting point
W width of the rectangle
h height of the rectangle
This command draws a rectangle of width w and
height h from the starting point (x, y).

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

applet code="rect" width=500 height=300></applet


importjava.awt.*;
importjava.applet.*;
public class rect extends Applet
5.20

cl=newColor(255,0,0); // creating color object with


ardeolor
paint(Graphics g)
Publ
lic void

g.setColor(c1);

g.drawRect(10,10,70,40:

Round corner rectangle

drawRoundRect() method is used to draw round

omer rectangles. The general form is


oiddrawRoundRect(intx, int y, int w, int h, int aw, int ah);|

Where

X,Y-starting point
W width of the rectangle

h height of the rectangle


aw arcwidth
ah arc height
his command draws a round corner rectangle or

Wand height h from the starting point (x, y). The arc

determine the rounding of the


and arc height ah
omer. *
ne tigure given below shows the example.
5.21

(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:

X,y starting point


w width of the rectangle
h height of the rectangle
b either ture or false
ana

This command draws of width W


a 3D ectangle ean valueb
height h from the starting point (x, y). The boolea
not.
indicates whether the rectangle has to be raised o
5.22

Example

Graphics g:
g.drawRect (10, 10, 50, 20, true);
(10, 10) 50

20

Filling rectangle

We can draw and fill the


following types of rectangles
ethods available in graphics class.
ng the met
They are
Drawing and filling sharp corner rectangle
ti) Drawing and
filling round corner rectangle
)Drawing and filling 3D rectangle
Drawing and filling sharp corner
rectangle
fillRect( ) method is used to draw and fill
rectangles. The general form is: sharp

void fillRect(int x, int y,


int w, int h);|
Where

X, y starting point
width of the
h
rectangle
height of the rectangle
Example
la Graphics g
gfilRect(10, 10,
50, 20)
5.23

Write a program to draw and fill


(b) the
red color rectangle vith
/<appktanke="rillizct"'wicdth==00heijght=I

mpot java.awt."
lappkt
importjiava.applet.":
cla fillrect extends Applet
public

Color cl=new Color(255,0,0); //creating colorohiocawith green


olor
publicvoid paint(Graphicsg)

g.setColor(c1);
g.fillRect(10,10,50,20);

(ii) Drawing and filling round corner rectangle

fillRoundRect() method is used to draw andfill round


corner rectangle. The general form is

voidfillRoundRect(int x, int y, int w, int h,intaw,intah;


Where

x, y starting point
W width of the rectangle
h height of the rectangle
aw a r c width
ah arc height
tne
rmine

The arc width aw and arc height deter


rounding of the corner.
5.24

tNample
Graphics.g;

(a)
Rect(1 10, 50, 20, 10, 10);
.fillRound

Drawing a n d
filling 3D rectangle

method is used to draw and fill 3D


Rect()
fill3DR
general form is
The
r e c t a n g l e .

file3DRect(int x, int y, int w, inth, boolean b);


void
voidfile3

Where

starting point
x, Y
width of the rectangle
W

h height of the rectangle


-either true or false
b

rectangle appears raised if the value of b is


true
The
and is lowered if b value is false.

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

keyReleased method name


keyTyped
event class name
keyEvent
e
object to receive the generated
event object

(vii) MouseMotionListener interface


(+
This interface has two methods. These methods are
of events caused
used to listen and process the different types
during mouse motion. The general forms are
Choice
Choice is an object of the
class Choice. This is used
to create pop-up menus. From the menu the user can select
the item. Each time the user selects an item an event is
generated.

Constructors in choice class


Constructor is a member function of the class choice.
his is used to give initial values to member variables. The
constructors are called only during object declaration. Choice
Class has only one Constructor.

Choice() This creates an empty list


5.59

Adding items to the list

Initially choice creates an empty list


add items to the list using the ad The user ta
form is method.The erer
void add(Stringstr);|
Where
add method name
str the name of the item to add

The general form to call the method is

obj.add("Name");|
Where
- choice object
obj
name - name of the item to add

Choice class methods

The following are the methods available in choice class


to do manipulations on the created choice list.

Method Description
String getSelectedltem()| This method is used to read the
selected item from the list

int getSelectedindex() This method is used to get the


index position of the selected
item in the list. By defaultthe
first item in the list is selectea

The starting index is 0.


the/
to ge
int getltemCount() This method is used
the list.
number of items in
5.60

etringgetltem(int i) This method is used to read


the
name of the item
specified by
the given index i.

wd Select(int i) This method is used to


the item specified by the
select
given
index i.
Creating choice object

The general form to create choice object is


Choice c =new Choice( );
This calls the no argument constructor and creates
an empty choice list.
()
Handling events on the choice
component
When the created choice list is
selected or deselected
anitemevent is generated. The steps given helnw shou
5.83

Layout managers
Layout managers are used to arrange o

controls on the window. Normally the GUI


controlsGU
are
arranged automatically. But the user can arranae
their GUI
controls using the layout managers given below.
FlowLayout
i) BorderLayout
ii) GridLayout
These classes are implemented from the interface
LayoutManager. The user can arrange their GUI controls
the method setLayout( ). The general formis
by calling
void setLayout(layoutmanagerobject);

If there is no call to this method, the GUI controls are


arranged automatically.

() 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

components into the five regions namely north, south. east

west and center of the container.

Constructors in border layout

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

general form to add the GUI component in the


The
windowis
void add(componentobject, region)

Where

componentobject
-

created objects of the controls such


as button, textfield etc.

region
-
five regions such BorderlLayout
as

.CENTER, BorderLayout .LEFT etc.

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

GridLayoutfint row, This createsgrid layout with the given


a
int column) number of rows and columns. -/4
Example
applet code =

gridlayoutdemo width=300 height-200>appic


importjava.applet.*;
import java.awt. *;

You might also like