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

Java Preparation Unit - 5

Uploaded by

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

Java Preparation Unit - 5

Uploaded by

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

Java Preparation

Unit-5
Java Applets and Graphics Programming
2 Marks
1. State the classes that can an applet extend.
Ans) • Graphics
• Font
• Color

2. Write syntax of elipse.


Ans) Syntax for drawing an ellipse in Java using the Graphics class:

1) void fillOval(int top, int left, int width, int height)


• This method draws a filled ellipse within a bounding rectangle.
• Parameters:
• top: The y-coordinate of the upper-left corner of the bounding
rectangle.
• left: The x-coordinate of the upper-left corner of the bounding
rectangle.
• width: The width of the bounding rectangle.
• height: The height of the bounding rectangle.
OR
2) void drawOval(int top, int left, int width, int height)
• This method draws an empty (outlined) ellipse within a bounding
rectangle.
• Parameters:
• top: The y-coordinate of the upper-left corner of the bounding
rectangle.
• left: The x-coordinate of the upper-left corner of the bounding
rectangle.
• width: The width of the bounding rectangle.
• height: The height of the bounding rectangle.

3. Give a syntax of tag to pass parameters to an applet.


OR
Give a syntax of (param) tag to pass parameters to an applet.
Ans) The syntax of the <param> tag to pass parameters to an applet is as follows:
<param name="parameterName" value="parameterValue">

For Example:
<param name="name" value="Ramesh">
<param name="age" value="25">

To retrieve these parameters in the applet, you can use the getParameter() method of
the Applet class, which has the following syntax:
String getParameter(String parameter_name);
4. Distinguish between Java applications and Java Applet (any two points).
Ans)

4 Marks
1. Explain applet life cycle in detail.
Ans)

Below is the description of each applet life cycle method:

init(): The init() method is the first method to execute when the applet is executed.
Variable declaration and initialization operations are performed in this method.

start(): The start() method contains the actual code of the applet that
should run. The start() method executes immediately after
the init() method. It also executes whenever the applet is restored,
maximized or moving from one tab to another tab in the browser.

stop(): The stop() method stops the execution of the applet. The
stop() method executes when the applet is minimized or when
moving from one tab to another in the browser.

destroy(): The destroy() method executes when the applet window is


closed or when the tab containing the webpage is
closed. stop() method executes just before when destroy() method is
invoked. The destroy() method removes the applet object from
memory.

paint(): The paint() method is used to redraw the output on the applet
display area. The paint() method executes after the execution
of start() method and whenever the applet or browser is resized.
The method execution sequence when an applet is executed is:
✓ init()
✓ start()
✓ paint()
The method execution sequence when an applet is closed is:
✓ stop()
✓ destroy()

2. Explain any four font methods with example.


Ans) Font Methods:
1. boolean isBold( ): Returns true if the font includes the BOLD style value.
Otherwise, false is returned.
2. boolean isItalic( ): Returns true if the font includes the ITALIC style value.
Otherwise, false is returned.
3. boolean isPlain( ): Returns true if the font includes the PLAIN style value.
Otherwise, false is returned.
4. String getFamily( ): Returns the name of the font family to which the invoking
font belongs.
5. String getFontName( ): Returns the face name of the invoking font.
6. String getName( ): Returns the logical name of the invoking font.
7. String getSize( ): Returns the size, in points, of the invoking font.
8. int getStyle( ): Returns the style values of the invoking font.
9. int hashCode( ): Returns the hash code associated with the invoking object.
10. static Font getFont(String property): Returns the font associated with the
system property specified by property. Null is returned if the property does not
exist.

3. Explain
i) drawLine
ii) drawOval
iii) drawRect
iv) drawArc

OR
Write a syntax and example of
i) drawRect( )
ii) drawoval( )

OR

Give the usage of following methods.


i) drawPolygon()
ii) drawOval()
iii) drawLine()
v) drawArc()

OR

Describe the use of Following methods:


i)Drawoval()
ii) getFont()
iii) drawRect()
iv) getFamily()

Ans) 1. drawLine(int x1, int y1, int x2, int y2)


• This method draws a line between two points (x1, y1) and (x2, y2) in the
current graphics context.
• Syntax: void drawLine(int x1, int y1, int x2, int y2)
• Example:
import java.awt.Graphics;
import java.applet.Applet;

public class DrawLineExample extends Applet {


public void paint(Graphics g) {
g.drawLine(50, 50, 150, 150);
}
}

2. drawOval(int x, int y, int width, int height)


• This method draws an oval or ellipse within the specified bounding
rectangle.
• Syntax: void drawOval(int x, int y, int width, int height)
• Example:
import java.awt.Graphics;
import java.applet.Applet;

public class DrawOvalExample extends Applet {


public void paint(Graphics g) {
g.drawOval(50, 50, 100, 50);
}
}

3. drawRect(int x, int y, int width, int height)


• This method draws the outline of a rectangle with the specified width and
height.
• Syntax: void drawRect(int x, int y, int width, int height)
• Example:
import java.awt.Graphics;
import java.applet.Applet;
public class DrawRectExample extends Applet {
public void paint(Graphics g) {
g.drawRect(50, 50, 100, 75);
}
}

4. drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
• This method draws a circular or elliptical arc covering the specified
rectangle.
• Syntax: void drawArc(int x, int y, int width, int height, int startAngle,
int arcAngle)
• Example:
import java.awt.Graphics;
import java.applet.Applet;

public class DrawArcExample extends Applet {


public void paint(Graphics g) {
g.drawArc(50, 50, 100, 100, 45, 90);
}
}

5. drawPolygon(int[] xPoints, int[] yPoints, int nPoints)


• This method draws a polygon defined by arrays of x and y coordinates.
• Syntax: void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
• Example:
import java.awt.Graphics;
import java.applet.Applet;

public class DrawPolygonExample extends Applet {


public void paint(Graphics g) {
int[] xPoints = {50, 100, 150};
int[] yPoints = {100, 50, 100};
int nPoints = 3;
g.drawPolygon(xPoints, yPoints, nPoints);
}
}

6. getFont()
• This method returns the current font used by the graphics context.
• Syntax: Font getFont()
• Example:
import java.awt.Font;
import java.awt.Graphics;
import java.applet.Applet;

public class GetFontExample extends Applet {


public void paint(Graphics g) {
Font font = g.getFont();
System.out.println("Current font: " + font);
}
}
7. getFamily()
• This method returns the font family name of the current font used by the
graphics context.
• Syntax: String getFamily()
• Example:
import java.awt.Font;
import java.awt.Graphics;
import java.applet.Applet;

public class GetFamilyExample extends Applet {


public void paint(Graphics g) {
String fontFamily = g.getFont().getFamily();
System.out.println("Font family: " + fontFamily);
}
}

4. Mention the steps to add applet to HTML file. Give sample code.
Ans) To add an applet to an HTML file, you can follow these steps:
1. Insert the <applet> Tag:
Place an <applet> tag at an appropriate location within the HTML document,
typically in the body section.

2. Specify the .class File:


Set the code attribute of the <applet> tag to specify the name of the applet's
.class file.

3. Use the codebase Attribute (Optional):


If the .class file is not in the current directory, you can use the codebase attribute
to specify the path to the file. This can be a relative path on the local system or a
URL if the file is on a remote server.

4. Define Width and Height:


Set the width and height attributes of the <applet> tag to specify the dimensions
of the applet's display area in pixels.

5. Add Parameters:
Optionally, you can use <param> tags within the <applet> tag to pass additional
parameters to the applet.

6. Provide Alternate Text:


You can include alternate HTML text within the <applet> tags. This text will be
displayed if the user's browser does not support Java or if Java is disabled.
7. Close the <applet> Tag: End the <applet> tag with </applet>.

Here's a sample HTML code demonstrating these steps:


<!DOCTYPE html>
<html>
<head>
<title>Sample Applet HTML</title>
</head>
<body>

<h1>Sample Applet Embedding</h1>

<applet code="HelloJava.class" width="300" height="200">


<!-- Optionally, you can add parameters here -->
</applet>

<p>This is the content of the HTML file.</p>

</body>
</html>

5. Differentiate between Java applications and Java Applet(any Four points.)


Ans)

.
6 Marks
1. Explain how to pass parameter to an applet? Write an applet to accept username in
the form of parameter and print “Hello <username>”.
Ans) • To pass parameters to an applet, you can use the <param> tag in the HTML code
where the applet is embedded.
• Each <param> tag specifies a parameter name and its corresponding value.
• In the applet code, you can retrieve these parameters using the getParameter()
method.

Here's how you can create an applet to accept a username as a parameter and print
"Hello <username>":

1. HTML Code (example.html):


<html>
<head>
<title>Applet Parameter Example</title>
</head>
<body>
<applet code="UsernameApplet.class" width="300" height="200">
<param name="username" value="John">
</applet>
</body>
</html>

2. Java Applet Code (UsernameApplet.java):


import java.applet.Applet;
import java.awt.Graphics;

public class UsernameApplet extends Applet {


private String username;

public void init() {


// Retrieve the value of the "username" parameter
username = getParameter("username");
}

public void paint(Graphics g) {


// Display a personalized greeting
g.drawString("Hello " + username, 20, 20);
}
}

You might also like