0% found this document useful (0 votes)
34 views2 pages

Java Applets and Graphics Methods Guide

This document discusses Java applets and graphics programming. It covers: 1) Syntax and examples of drawRect() and drawOval() methods. 2) Designing an applet that displays a red rectangle with blue text. 3) Creating an applet that displays three circles filled with different colors. 4) Explaining attributes of the <applet> tag and states in an applet lifecycle like initialization, running, and display.

Uploaded by

subbareddy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views2 pages

Java Applets and Graphics Methods Guide

This document discusses Java applets and graphics programming. It covers: 1) Syntax and examples of drawRect() and drawOval() methods. 2) Designing an applet that displays a red rectangle with blue text. 3) Creating an applet that displays three circles filled with different colors. 4) Explaining attributes of the <applet> tag and states in an applet lifecycle like initialization, running, and display.

Uploaded by

subbareddy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Chapter 5.

Java Applets & Graphics Programming

4 Marks

1. State syntax and explain it with parameters for :


a) drawRect ( ) ii) drawOval ( )
2. Design an Applet program which displays a rectangle filled with red color and
message as “Hello Third year Students” in blue color.
3. Write a program to design an applet to display three circles filled with three
different colors on screen.
4. Explain all attributes available in < applet > tag.
5. Describe following states of applet life cycle :
a) Initialization state b) Running state. c) Display state.
6. Differentiate applet and application with any four points.
7. Describe applet life cycle with suitable diagram.
8. Describe following attributes of <applet> tag :
1) codebase 2) code 3) width 4) alt

6 Marks

1. Write method to set font of a text and describe its parameters.


2. Explain <applet> tag with its major attributes only. State purpose of
getAvailableFontFamilyName ( ) method of graphics environment class.
3. Differentiate between applet and application and also write a simple applet
which display message ‘Welcome to Java’.
4. Explain <PARAM> Tag of applet with suitable example.
5. Write the syntax and example for each of following graphics methods :
1) drawPolygon ( ) 2) drawRect ( ) 3) drawOval ( ) 4) fillOval ( )
6. Write syntax and example of following Graphics class methods :
1) drawOval ( ) 2) drawPolygon ( ) 3) drawArc()

8 Marks

1. Write a simple applet program which display three concentric circle.


2. Write syntax and example of following Graphics class methods :
a. drawOval( ) b. drawPolygon( ) c. drawArc( ) d. drawRect( )
3. How can parameters be passed to an applet? Write an applet to accept user name
in the form of parameter and print ‘Hello < username >’.
4. Write syntax and example of
a. drawString ( ) b. drawRect ( ) c. drawOval ( ) d. drawArc ( )
5. Design an applet which displays three circles one below the other and fill them
red, green and yellow color respectively.
6. Write a program to design an applet which accepts 2 numbers as parameters using
<PARAM> tag and display addition of squares of those numbers.

Common questions

Powered by AI

To create and fill a rectangle with a specific color in an applet, use the setColor() method of the Graphics class to set the desired color, followed by fillRect() to draw the filled rectangle. Example code: ```public void paint(Graphics g) { g.setColor(Color.RED); g.fillRect(50, 50, 200, 100); }``` This applet method sets the color to red and draws a filled rectangle at position (50, 50) with dimensions 200x100 pixels .

Parameters are passed to an applet through the <PARAM> tag within the HTML, specifying name-value pairs. The applet reads these parameters using the getParameter() method. For instance, a parameter <PARAM NAME="username" VALUE="John"> is retrieved in the applet with getParameter("username") and can be used to display a message like "Hello John". This mechanism allows applets to be dynamic and customizable based on user input or other external data .

The applet lifecycle diagram visually maps the sequence of states applets go through: initialization, running, stopping, and destruction. This visualization aids in understanding the applet's execution flow and interaction points. Each transition corresponds to lifecycle methods like init(), start(), stop(), and destroy(), highlighting when resources are allocated/freed and user interaction occurs. Recognizing these states helps developers manage resources efficiently and optimize performance .

The getAvailableFontFamilyNames() method, typically used within the GraphicsEnvironment class, returns an array of font family names available on the system. This allows applet developers to dynamically adjust the font styles available for rendering text, ensuring compatibility and a rich visual experience regardless of the user's local environment. This adaptability enhances the appearance and usability of text within applets .

The drawPolygon() method is used to draw a closed shape based on a series of x and y coordinates that define vertices, suitable for shapes like triangles and pentagons. On the contrary, drawOval() draws an ellipse fitting within a bounding rectangle defined by its parameters. Use cases for drawPolygon() include creating complex shapes or icons, while drawOval() is ideal for circles and ellipses, facilitating simple and geometrically precise renderings in applets .

The <applet> tag is used to embed Java applets within an HTML page. Major attributes include 'codebase', which specifies the base URL for the applet code; 'code', which defines the name of the class that starts the applet; 'width' and 'height', which set the dimensions of the applet display area; and 'alt', which provides alternate text if the applet cannot be displayed. These attributes define the applet's source, appearance, and fallback content .

The drawRect() method uses the syntax drawRect(int x, int y, int width, int height), where it draws an outline of a rectangle at the specified coordinates and dimensions. Similarly, drawOval() uses drawOval(int x, int y, int width, int height) to draw an outline of an oval within the specified rectangle area. These methods are typically used in the paint() method of an applet to render shapes directly onto the component's visible area, providing visual elements within the applet .

The <PARAM> tag attributes define name-value pairs that pass configuration data to an applet. For instance, the attributes can include NAME and VALUE where NAME represents the parameter's identifier, and VALUE holds the data being passed. Example: `<PARAM NAME="bgcolor" VALUE="blue">` passes a background color parameter. The applet accesses this using getParameter("bgcolor") to customize its appearance, such as setting a blue background .

Applets run within a browser or applet viewer and are subject to security restrictions, whereas standalone applications are executed directly by the Java interpreter and have full access to system resources. Applets do not have a main method and rely on the browser for lifecycle management, while applications begin execution at the main method. These differences matter because applets are more suitable for small web-based tasks and have limited capabilities, while applications are versatile and can perform a wider range of tasks without environment constraints .

The Java applet lifecycle comprises the initialization state, where the applet is loaded and initialized using the init() method; the running state, where the applet is started with the start() method and can be interacted with; and the display state, which represents the active running phase where content is rendered using the paint() method. These stages control the applet's lifecycle, ensuring orderly execution and interaction with the user, maintaining its state across different activities .

You might also like