Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

SUMMER– 18

(b) With proper syntax and example explain following graphics


methods:
1) SetColor( )
2) SetForeGround( )
3) getFont( )
4) setSize( )
Ans:

1) setColor() :
 Syntax :
setColor(Color c) // where „c‟ is the Color class object.

//Sets this graphics context's current color to the specified color.

 Example :
setColor(Color.RED);

2) setForeGround() :

 Syntax:

public void setForeground(Color c) // „c‟ is Color class object Sets the foreground
color of the component.
 Example :

setForeground(Color.BLUE);

3) getFont() :
 Syntax :

public static Font getFont(String nm)


 nm - the property name.

Returns a font from the system properties list.

public static Font getFont(String nm, Font font)

 nm - the property name.


 font – a default font to return if property 'nm' is not defined.

Returns the specified font from the system properties list.

 Example:

Font f=g.getFont();

(c) Define applet. Write a program to create an applet to display


message “Welcome to java applet”. 8M
Ans:
1. Java applet is a small dynamic Java program that can be transferred via the Internet and run
by a Java-compatible Web browser.
2. The main difference between Java-based applications and applets is that applets are typically
executed in an appletviewer or Javacompatible Web browser.
3. All applets import the java.awt package.

/* <applet code= WelcomeJava width= 300 height=300></applet>*/


import java.applet.*;

import java.awt.*;

public class WelcomeJava extends Applet

public void paint(Graphics g)

g.drawString(“Welcome to java”,25,50);

(a) Explain any four applet tag.


Ans:
 APPLET Tag:
1. The APPLET tag is used to start an applet from both an HTML document and from
an appletviewer will execute each APPLET tag that it finds in a separate window,
while web browser will allow many applets on a single page the syntax for the
standard APPLET tag is:
 CODEBASE:

It is an optional attribute that specifies the base URL of the applet code or the directory that will be
searched for the applets executable class file.

 CODE:

It is a required attribute that give the name of the file containing your applets compiled class file
which will be run by web browser or applet viewer.

 ALT: (Alternate Text)

The ALT tag is an optional attribute used to specify a short text message that should be displayed if
the browser cannot run java applets.

 NAME:

It is an optional attribute used to specify a name for the applet instance.

 WIDTH AND HEIGHT:

They are required attributes that give the size(in pixels) of the applet display area.

 ALIGN:

ALIGN is an optional attribute that specifies the alignment of the applet.

 The possible value is:

LEFT, RIGHT, TOP, BOTTOM, MIDDLE, BASELINE, TEXTTOP, ABSMIDDLE, and


ABSBOTTOM.

 VSPACE AND HSPACE:

attributes are optional, VSPACE specifies the space, in pixels, about and below the applet.
HSPACE VSPACE specifies the space, in pixels, on each side of the applet

 PARAM NAME AND VALUE:

The PARAM tag allows you to specifies appletspecific arguments in an HTML page applets access
there attributes with the getParameter() method.

(b) Explain applet life cycle with suitable diagram. 4M


Ans:
Applets are small applications that are accessed on an Internet server, transported over the Internet,
automatically installed, and run as part of a web document.

The applet states include:


 Born or initialization state

 Running state

 Idle state

 Dead or destroyed state

 Initialization state:
Applet enters the initialization state when it is first loaded. This is done by calling the init() method
of Applet class.

At this stage the following can be done:

 Create objects needed by the applet

 Set up initial values

 Load images or fonts


 Set up colors

Initialization happens only once in the life time of an applet.

public void init()

//implementation

 Running state:

1. applet enters the running state when the system calls the start() method of Applet class.
2. This occurs automatically after the applet is initialized. start() can also be called if the applet
is already in idle state.
3. start() may be called more than once.
4. start() method may be overridden to create a thread to control the applet.

public void start()

//implementation

 Idle or stopped state:


1. An applet becomes idle when it is stopped from running.
2. Stopping occurs automatically when the user leaves the page containing the currently
running applet.
3. stop() method may be overridden to terminate the thread used to run the applet.

public void stop()

//implementation

 Dead state:

1. an applet is dead when it is removed from memory. This occurs automatically by invoking
the destroy method when we quit the browser.
2. Destroying stage occurs only once in the lifetime of an applet.
3. destroy() method may be overridden to clean up resources like threads.

public void destroy()

//implementation

 Display state:

1. applet is in the display state when it has to perform some output operations on the screen.
2. This happens after the applet enters the running state. paint() method is called for this.
3. If anything is to be displayed the paint() method is to be overridden.

public void paint(Graphics g)

//implementation

(c) Explain following methods for applet with an example:


(1) Passing Parameter to applet
(2) Embedding
Ans:
1) Passing parameter to applet :
Parameter can be passed to applet through Param attributes of applet tag.

[<PARAM NAME=attributeName1 VALUE=attributeValue> ]

 getParameter() Method:

The getParameter() method of the Applet class can be used to retrieve the parameters passed
from the HTML page.

 Syntax of getParameter() method :


String getParameter(String param-name)

2) APPLET Tag:
Embedding applet tag in java code by two ways.

 Adding applet tag to html file

 Adding applet tag in java source file 1.

 Example for embedding <applet> tag in html file.


import java.awt.*;

import java.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);

<HTML>

<Applet code = hellouser.class width = 400 height = 400>

<PARAM NAME = "username" VALUE = abc> </Applet>


</HTML>
2. Example for embedding <applet> tag in Java source file.
import java.awt.*;

import java.applet.*;

/*<Applet code = hellouser.class width = 400 height = 400>

<PARAM NAME = "username" VALUE = abc>

</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);

(c)Give the syntax of following methods of graphics class.


Explain their use with suitable program:
(i) drawRoundReel( )
(ii) drawPolygon( )
(iii) drawOval( )
(iv) drawstring( )
Ans.:
(Note: Solution is given for drawRoundRect() method)

(i) void drawRoundRect( ):-


void drawRoundRect(int x, int y, int width, int height, int arc_width, int arc_height)

- draws an outlined round cornered rectangle.int x and y represents the top left corner of the
rectangle. Width and height represents the length and breadth of the rectangle.

- The arcwidth and archeight represents the horizontal and vertical diameter of the arc at the four
corners.

(ii) void drawPolygon( ):


void drawPolygon(int x[], int y[], int n)

- draws a polygon with the arrays of x coordinates and y coordinates and the number of points
specified by n

OR
void drawPolygon(Polygon p):-

draws a polygon defined by the specified polygon object.

(iii) void drawOval( ):-


void drawOval(int x, int y, int width, int height)

- draws an outline of an oval.

(iv) void drawstring( ):-


void drawstring(String str, int x, int y)

- draws the string specified using the coordinates specified by x and y.

 Example:-
import java.awt.*;

importjava.applet.*;

/*<applet code = MyApplet.class height = 300 width = 300></applet> */

public class MyApplet extends Applet

public void paint(Graphics g)

g.drawRoundRect(40, 40, 40, 30, 10,10);

int x[] = {40,80,120};

int y[] = {90,100,95};

g.drawPolygon(x,y,3);

g.drawOval(40, 110,40,30);

g.drawString("My Applet",40, 160);

(c) How to pass parameter to an applet? Write an applet to


accept Account No and balance in form of parameter and print
message “low balance” if the balance is less than 500.
Ans.:
 Passing parameters to an applet :
For passing parameters in an applet class <param> tag can be used within <applet> tag. <param>
has two attributes as name and value.

 For example :
<applet code=applet1 width=200 height=200>

<param name=”uname” value=”abc”>


</applet>

Attribute name specifies name of the parameter as “uname” in example and value specifies the
value inside uname as “abc”.
The values of the parameter can be fetched in applet with the help of getParameter() method as

String username=getParameter(“uname”);

 Program :
importjava.awt.*;

importjava.applet.*;

public class applet1 extends Applet

String accno="";

int balance=0;

public void init()

accno=getParameter("acno");

balance=Integer.parseInt(getParameter("bal"));

public void paint(Graphics g)

if(balance<500)

g.drawString(accno+": Low balance...",100,100);

else

g.drawString(accno+":sufficient balance...",100,100);

/*<applet code=applet1 width=200 height=200>


<param name="acno" value="1001">

<param name="bal" value="200">

</applet>*/

Summer 2017

(c) Explain applet life cycle with suitable diagram.


Ans.:

Applets are small applications that are accessed on an Internet server, transported over the Internet,
automatically installed, and run as part of a web document.

 The applet states include:


 Initialization state:
applet enters the initialization state when it is first loaded. This is done by calling the init() method
of Applet class. At this stage the following can be done:

Initialization happens only once in the life time of an applet.

public void init()

//implementation

 Running state:
applet enters the running state when the system calls the start() method of Applet class. This occurs
automatically after the applet is initialized. start() can also be called if the applet is already in idle
state. start() may be called more than once. start() method may be overridden to create a thread to
control the applet.

public void start()

//implementation

 Idle or stopped state:


an applet becomes idle when it is stopped from running. Stopping occurs automatically when the
user leaves the page containing the currently running applet. stop() method may be overridden to
terminate the thread used to run the applet.
public void stop()

//implementation

 Dead state:
an applet is dead when it is removed from memory. This occurs automatically by invoking the
destroy method when we quit the browser. Destroying stage occurs only once in the lifetime of an
applet. destroy() method may be overridden to clean up resources like threads.

public void destroy()

//implementation

 Display state:
applet is in the display state when it has to perform some output operations on the screen. This
happens after the applet enters the running state. paint() method is called for this. If anything is to
be displayed the paint() method is to be overridden.

public void paint(Graphics g)

//implementation

(d) Describe the following attributes of applet.


(i) Codebase
(ii) Alt
(iii) Width
(iv) Code
Ans.
(i) Codebase:
Code base is an optional attribute that specifies the base URL of the applet code or the directory
that will be searched for the applet‟s executable class file.

(ii) Alt:
Alternate Text. The ALT tag is an optional attribute used to specify a short text message that
should be displayed if the browser cannot run java applets.

(iii) Width:
Width is required attributes that give the width (in pixels) of the applet display area.

(iv) Code:
Code is a required attribute that give the name of the file containing your applet‟s compiled class
file which will be run by web browser or appletviewer.

(d) Differentiate between Applet and Application (any 4 points).


Ans.:
(c) Explain the following methods of applet class:
(i) drawRect()
(ii) drawPolygon()
(iii) drawArc()
(iv) drawRoundRect()
Ans.:
(i) drawRect():
The drawRect() method displays an outlined rectangle.

 Syntax:
void drawRect(inttop, intleft, intwidth, int height)

The upper-left corner of the Rectangle is at top and left. The dimension of the Rectangle is
specified by width and height.

 Example:
g.drawRect(10,10,60,50);

(ii) drawPolygon():
drawPolygon() method is used to draw arbitrarily shaped figures.

 Syntax:
void drawPolygon(int x[], int y[], intnumPoints)

The polygon‟s end points are specified by the co-ordinates pairs contained within the x and y
arrays. The number of points define by x and y is specified by numPoints.

 Example:
intxpoints[]={30,200,30,200,30};

intypoints[]={30,30,200,200,30};

intnum=5;

g.drawPolygon(xpoints,ypoints,num);

(iii) drawArc( ):
It is used to draw arc .
 Syntax:
void drawArc(int x, int y, int w, int h, intstart_angle,intsweep_angle);

where x, y starting point, w & h are width and height of arc, and start_angle is starting angle of arc
sweep_angle is degree around the arc

 Example:
g.drawArc(10, 10, 30, 40, 40, 90);

(iv)drawRoundRect():
It is used to draw rectangle with rounded corners.

 Syntax :
drawRoundRect(int x,int y,int width,int height,int arcWidth,int arcHeight)

Where x and y are the starting coordinates, with width and height as the width and height of
rectangle. arcWidth and arcHeight defines by what angle the corners of rectangle are rounded.

 Example:
g.drawRoundRect(25, 50, 100, 100, 25, 50);

You might also like