0% found this document useful (0 votes)
26 views6 pages

Applet Graphics Methods Overview

lab practice

Uploaded by

osmytech57
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)
26 views6 pages

Applet Graphics Methods Overview

lab practice

Uploaded by

osmytech57
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

Applet program lab practice

Commonly used methods of Graphics class for applet program:


❖ public void drawString(String str, int x, int y): is used to draw the specified string.
❖ public void drawRect(int x, int y, int width, int height): draws a rectangle with the specified width
and height.
❖ public void fillRect(int x, int y, int width, int height): is used to fill rectangle with the default
color and specified width and height.
❖ public void drawOval(int x, int y, int width, int height): is used to draw oval with the specified
width and height.
❖ public void fillOval(int x, int y, int width, int height): is used to fill oval with the default color and
specified width and height.
❖ public void drawLine(int x1, int y1, int x2, int y2): is used to draw line between the points(x1, y1)
and (x2, y2).
❖ public boolean drawImage(Image img, int x, int y, ImageObserver observer): is used draw the
specified image.
❖ public abstract void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle): is
used draw a circular or elliptical arc.
❖ public abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle): is
used to fill a circular or elliptical arc.
❖ public void setColor(Color c): is used to set the graphics current color to the specified color.
❖ public void setFont(Font font): is used to set the graphics current font to the specified font.

1. Simple applet program to display text on screen


import [Link];
import [Link];
public class First extends Applet{
public void paint(Graphics g){
[Link]([Link]);
[Link]("welcome to applet",150,150);
} }
2. Applet program to draw the rectangle and line
import [Link].*;
import [Link].*;
public class LineRect extends Applet {
public void paint(Graphics g) {
[Link](“Draw line and rectangle”,30,30);
[Link](40,40,60,50);
[Link](40,60,40,50);
} }
OUTPUT:

3. Applet program to drawing fill rectangle and draw Round Rectangle


import [Link].*;
import [Link].*;
public class LineRect extends Applet {
public void paint(Graphics g) {
[Link](60,10,30,80);
[Link](10,100,80,50,10,10);
[Link](20,110,60,30,5,5);
}
}
OUTPUT:
4. Applet program to draw oval and fill oval by default or set color and also to draw arc and fill arc
by default or set color.
import [Link].*;
import [Link].*;
public class lab1 extends Applet
{
public void paint(Graphics g){
[Link](30,100,30,30);
[Link]([Link]);
[Link](170,200,30,30);
[Link](90,150,30,30,30,270);
[Link](270,150,30,30,0,180);
}
}}

OUTPUT:
5. Applet program to draw happy face
import [Link].*;
import [Link].*;
public class NewClass extends Applet
{
public void paint(Graphics g)
{
[Link](40,40,120,150); //Head
[Link](57,75,30,20); //Left eye
[Link](110,75,30,20); //Right eye
[Link](68,81,10,10); //Pupil (left)
[Link](121,81,10,10); //Pupil (right)
[Link](85,100,30,30); //Nose
[Link](60,125,80,40,180,180); //Mouth
[Link](25,92,15,30); //Left ear
[Link](160,92,15,30); //Right ear
}}
Output of question number 4.
6. Applet program to draw poygon
Polygons are shapes with many sides. A polygons may be defined as a set of connected lines. The end of
first line is the beginning of second line, and so on,
Syntax:
drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
Draws a closed polygon defined by arrays of x and y coordinates.
import [Link].*;
import [Link].*;
public class NewClass extends Applet
{
int x1[]= { 20,120,220,20};
int y1[]= { 20,120,20,20};
int n1= 4;
int x2[]= { 120,220,220,120};
int y2[]= { 120,20,220,120};
int n2= 4;
public void paint(Graphics g)
{
[Link](x1,y1,n1);
[Link](x2,y2,n2);
}}
OUTPUT:

T
import [Link].*;
import [Link].*;
public class LineRect extends Applet
{
public void paint(Graphics g)
{
[Link](10,10,50,50);
[Link](10,60,40,30);
[Link](60,10,30,80);
[Link](10,100,80,50,10,10);
[Link](20,110,60,30,5,5);
[Link](100,10,230,140);
[Link](100,140,230,10);
}
}

Common questions

Powered by AI

Potential issues when using `drawLine` and `drawRect` include incorrect coordinate specification leading to lines or rectangles not appearing as expected. For example, `drawLine(40,40,60,50)` wrongly used the method `drawstring` instead of `drawString` in one applet, which would generate a compile-time error . Additionally, if the coordinates provided do not align with the intended design, such as specifying `drawRect(40,60,40,50)` incorrectly, the rectangle may not be drawn where anticipated, suggesting errors in coordinate plotting or confusion between width and point coordinates. Proper debugging and understanding of coordinate systems are crucial .

When creating facial features using graphical methods in an applet, it is essential to consider the positioning and proportions of each feature. For a face drawn with ovals and arcs, as illustrated in an applet in the document, features like eyes, nose, and mouth must be appropriately sized and placed relative to each other to maintain facial symmetry. The document describes drawing a happy face with `drawOval` and `fillArc` methods to create eyes, pupils, a nose, and a mouth in a coherent and aesthetically pleasing manner. Aligning the features considering these methods can produce a more realistic and expressive face .

Using color settings like `setColor` in graphical applets is significant for enhancing visual appeal and improving information conveyance. In the document, `setColor(Color.red)` is used before `fillOval` and `fillArc`, ensuring the shapes are filled with red. This technique highlights or differentiates elements, aiding in visual organization and user understanding. Selecting appropriate colors based on context can improve the usability and aesthetic value of the applet, crucial in designing engaging interfaces .

When integrating multiple graphical elements in an applet, consider the logical organization of method calls, proper setting of colors and fonts, and coordinate positioning. The sequence impacts the layering of elements (e.g., using `fillRect` before `drawString` ensures the text is visible). The document showcases varied use of `drawRect`, `fillRect`, and other methods within a single class to demonstrate functionality cohesively, requiring accurate coordinate and size parameters for elements to align and appear correctly on the screen. Attention to these details ensures the composite image is coherent and functional .

The `drawArc` and `fillArc` methods allow developers to draw and fill arc-shaped segments, expanding beyond simple geometric shapes like rectangles and ovals. By specifying the start angle and the arc angle, these methods can create intricate designs like sections of circles or ellipses, potentially serving purposes like drawing pie charts or adding artistic flourishes to designs. Their use is demonstrated in drawing smiling or sad mouth expressions on faces by altering the arc angles, as shown in the document's happy face example . This flexibility enhances the potential for visual creativity in applet designs.

Drawing polygons within an applet involves defining arrays of x and y coordinates representing the vertices of the polygon. The `drawPolygon` method takes these arrays and the number of points to outline the shape, while `fillPolygon` does the same but fills the shape with the current color. For example, the document uses arrays such as `x1[]= {20,120,220,20}` and `y1[]= {20,120,20,20}` with `n1=4` to draw a polygon, and `fillPolygon` with similar coordinates to fill another shape . This process allows creating complex shapes beyond basic geometric figures, adding functionality and flexibility to graphical applets .

The `setColor` method in an applet sets the color state for the Graphics object so that all subsequent drawing operations use this color. For example, if `setColor(Color.red)` is used, any shape or string drawn afterward will appear in red. Similarly, `setFont` changes the font state for text rendering within the Graphics context, affecting how strings are drawn. If a specific font is set, it alters the appearance of text on the applet screen. By combining these methods, developers can customize their applet's appearance significantly .

The `drawImage` method displays an image within a Java applet by taking an `Image` object, x and y coordinates for placement, and an `ImageObserver` for asynchronous updates. Critically, ensure the `Image` is appropriately loaded, often necessitating an `ImageObserver` to handle image loading updates since images may not be immediately available after initiating `loadImage`. Also, placement coordinates must align with the overall layout to integrate seamlessly with text and shape elements. Proper synchronization and handling of image fetch completion are necessary to prevent incomplete rendering .

Both `drawRoundRect` and `fillRoundRect` are used to draw rectangles with rounded corners. The former method outlines a rounded rectangle, taking parameters for x and y positions, width, height, and the arc width and height that define the curvature of the corners. `fillRoundRect` fills such a rounded rectangle with the current color. These methods are critical in designing user-friendly and aesthetically appealing graphics, providing visual softness compared to harsh rectangular edges. The document highlights how these are implemented with examples like `drawRoundRect(10,100,80,50,10,10)` to achieve desired graphical effects .

The Graphics class in Java applets provides several methods for drawing shapes and text. Examples include `drawString` for displaying text, `drawRect` and `fillRect` for drawing and filling rectangles, `drawOval` and `fillOval` for drawing and filling ovals, `drawLine` for creating lines, and `drawImage` for rendering images. Additionally, `drawArc` and `fillArc` are used for arcs, and `drawPolygon` and `fillPolygon` for polygons. These methods allow the creation of various shapes and text with specified colors and fonts .

You might also like