Java Preparation Unit - 5
Java Preparation Unit - 5
Unit-5
Java Applets and Graphics Programming
2 Marks
1. State the classes that can an applet extend.
Ans) • Graphics
• Font
• Color
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)
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.
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()
3. Explain
i) drawLine
ii) drawOval
iii) drawRect
iv) drawArc
OR
Write a syntax and example of
i) drawRect( )
ii) drawoval( )
OR
OR
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;
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;
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.
5. Add Parameters:
Optionally, you can use <param> tags within the <applet> tag to pass additional
parameters to the applet.
</body>
</html>
.
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>":