Java Aap Let Methods

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Below are explanations and examples for each of the mentioned methods used in Java applets:

1. getParameter() Method :
- Description : The `getParameter()` method is used to retrieve the value of a parameter
passed to the applet in the HTML code. Parameters can be used to customize the behavior of the
applet.
- Syntax :
Public String getParameter(String paramName)

 paramName: The name of the parameter whose value is to be retrieved.

Example :
Import java.applet.Applet;
Public class MyApplet extends Applet {
Public void init() {
String message = getParameter(“message”);
// Use the value of the “message” parameter
}
}

2. drawString() Method :
- Description : The `drawString()` method is used to draw a text string at the specified
coordinates.
- Syntax :
Public abstract void drawString(String str, int x, int y)

 Str: The text string to be drawn.


 x: The x-coordinate of the starting point of the string
 y: The y-coordinate of the starting point of the string.
Example :
Import java.applet.Applet;
Import java.awt.Graphics;
Public class MyApplet extends Applet {
Public void paint(Graphics g) {
g.drawString(“Hello, World!”, 50, 50);
}
}

3. drawLine() Method :
- Description : The `drawLine()` method is used to draw a straight line between two points.
- Syntax :
Public abstract void drawLine(int x1, int y1, int x2, int y2)

 X1: The x-coordinate of the starting point of the line.


 y1: The y-coordinate of the starting point of the line.
 x2: The x-coordinate of the ending point of the line.
 y2: The y-coordinate of the ending point of the line.

Example :
Import java.applet.Applet;
Import java.awt.Graphics;
Public class MyApplet extends Applet {
Public void paint(Graphics g) {
g.drawLine(20, 30, 100, 150);
}
}
4. drawRect() Method :
- Description : The `drawRect()` method is used to draw the outline of a rectangle.
- Syntax :
Public abstract void drawRect(int x, int y, int width, int height)

 X: The x-coordinate of the upper-left corner of the rectangle.


 y: The y-coordinate of the upper-left corner of the rectangle.
 width: The width of the rectangle.
 height: The height of the rectangle.

Example :
Import java.applet.Applet;
Import java.awt.Graphics;
Public class MyApplet extends Applet {
Public void paint(Graphics g) {
g.drawRect(50, 50, 100, 80);
}
}

5. fillRect() Method :
- Description : The `fillRect()` method is used to draw a filled rectangle.
- Syntax :
Public abstract void fillRect(int x, int y, int width, int height)

Example :
Import java.applet.Applet;
Import java.awt.Graphics;
Public class MyApplet extends Applet {
Public void paint(Graphics g) {
g.fillRect(50, 50, 100, 80);
}
}

6. drawRoundRect() Method :
- Description : The `drawRoundRect()` method is used to draw the outline of a rectangle with
rounded corners.
- Syntax :
Public abstract void drawRoundRect(int x, int y, int width, int height, int arcWidth, int
arcHeight)

 x: The x-coordinate of the upper-left corner of the rectangle.


 y: The y-coordinate of the upper-left corner of the rectangle.
 width: The width of the rectangle.
 height: The height of the rectangle.
 arcWidth: The width of the arc at the four corners.
 arcHeight: The height of the arc at the four corners.

Example :
Import java.applet.Applet;
Import java.awt.Graphics;
Public class MyApplet extends Applet {
Public void paint(Graphics g) {
g.drawRoundRect(50, 50, 100, 80, 20, 20);
}
}

7. fillRoundRect() Method :
- Description : The `fillRoundRect()` method is used to draw a filled rectangle with rounded
corners.
- Syntax :
Public abstract void fillRoundRect(int x, int y, int width, int height, int arcWidth, int
arcHeight)

Example :
Import java.applet.Applet;
Import java.awt.Graphics;
Public class MyApplet extends Applet {
Public void paint(Graphics g) {
g.fillRoundRect(50, 50, 100, 80, 20, 20);
}
}

6. DRAW ROUND RECT


The methods `public void draw3drect` and `public void fill3drect` are typically found in
graphics-related libraries or frameworks like Java’s AWT (Abstract Window Toolkit) or Swing.
Here’s an explanation of each method and its parameters:

1. **public void draw3drect**:


- This method is used to draw a 3D rectangle outline on a graphical component, such as a GUI
window or panel.
- Parameters:
- `int x`: The x-coordinate of the upper-left corner of the rectangle.
- `int y`: The y-coordinate of the upper-left corner of the rectangle.
- `int width`: The width of the rectangle.
- `int height`: The height of the rectangle.
- `boolean raised`: A boolean value that determines whether the rectangle should be drawn
with a raised or lowered appearance. If `raised` is `true`, the rectangle is drawn with a raised 3D
effect; if `false`, it is drawn with a lowered effect.

Example usage:
g.draw3DRect(10, 10, 100, 50, true);

2. **public void fill3drect**:


- This method is used to fill a 3D rectangle with a specified color on a graphical component.
- Parameters:
- `int x`: The x-coordinate of the upper-left corner of the rectangle.
- `int y`: The y-coordinate of the upper-left corner of the rectangle.
- `int width`: The width of the rectangle.
- `int height`: The height of the rectangle.
- `boolean raised`: A boolean value that determines whether the rectangle should be filled
with a raised or lowered appearance, similar to `draw3drect`.

Example usage:
g.fill3DRect(10, 10, 100, 50, true

7. DRAW OVAL
The methods `public void drawOval` and `public void fillOval` are used in Java’s graphics
libraries (such as AWT or Swing) to draw or fill an oval shape on a graphical component. Here’s
an explanation of each method and its parameters:

1. **public void drawOval**:


- This method is used to draw an outline of an oval shape on a graphical component.
- Parameters:
- `int x`: The x-coordinate of the upper-left corner of the bounding rectangle of the oval.
- `int y`: The y-coordinate of the upper-left corner of the bounding rectangle of the oval.
- `int width`: The width of the bounding rectangle of the oval.
- `int height`: The height of the bounding rectangle of the oval.

Example usage:
g.drawOval(50, 50, 100, 80);

2. **public void fillOval**:


- This method is used to fill an oval shape with a specified color on a graphical component.
- Parameters:
- `int x`: The x-coordinate of the upper-left corner of the bounding rectangle of the oval.
- `int y`: The y-coordinate of the upper-left corner of the bounding rectangle of the oval.
- `int width`: The width of the bounding rectangle of the oval.
- `int height`: The height of the bounding rectangle of the oval.

Example usage:
g.fillOval(50, 50, 100, 80);

8. DRAW POLYGON
The `public void drawPolygon` method is used in Java’s graphics libraries, such as AWT or
Swing, to draw the outline of a polygon on a graphical component. Here’s an explanation of the
method and its parameters:
- **public void drawPolygon**:
- This method is used to draw the outline of a polygon, which is defined by a series of points.
- Parameters:
- `int[] xPoints`: An array of x-coordinates of the points that define the polygon.
- `int[] yPoints`: An array of y-coordinates of the points that define the polygon.
- `int nPoints`: The number of points in the polygon.

Example usage:
Int[] xPoints = {100, 200, 300}; // X-coordinates of the polygon points
Int[] yPoints = {150, 250, 200}; // Y-coordinates of the polygon points
Int nPoints = 3; // Number of points in the polygon
g.drawPolygon(xPoints, yPoints, nPoints); // Draws the outline of the polygon
9. DRAW POLYLINE

2. **public void drawPolyline**:


- This method is used to draw a series of connected line segments (polyline) on a graphical
component.
- Parameters:
- `int[] xPoints`: An array of x-coordinates of the endpoints of the line segments.
- `int[] yPoints`: An array of y-coordinates of the endpoints of the line segments.
- `int nPoints`: The number of points (endpoints) in the polyline.

Example usage:
Int[] xPoints = {100, 200, 150}; // x-coordinates of endpoints
Int[] yPoints = {100, 100, 200}; // y-coordinates of endpoints
Int nPoints = 3; // number of endpoints
g.drawPolyline(xPoints, yPoints, nPoints); // Draws a polyline connecting specified points

In this example, `xPoints` and `yPoints` arrays contain the coordinates of the endpoints of the
line segments, and `nPoints` specifies the number of endpoints.

Both `drawPolygon` and `drawPolyline` methods are useful for creating custom shapes or paths
in graphical user interfaces. The coordinates in the arrays define the vertices or endpoints of the
shapes or lines to be drawn.

10. DRAW ROUND ARC


Certainly! Let’s start with `public void drawArc` and then cover `public void fillArc`.

1. **public void drawArc**:


- This method is used to draw an arc (a portion of an ellipse) on a graphical component.
- Parameters:
- `int x`: The x-coordinate of the upper-left corner of the bounding rectangle of the ellipse.
- `int y`: The y-coordinate of the upper-left corner of the bounding rectangle of the ellipse.
- `int width`: The width of the bounding rectangle of the ellipse.
- `int height`: The height of the bounding rectangle of the ellipse.
- `int startAngle`: The starting angle of the arc in degrees (0 degrees is at the 3 o’clock
position).
- `int arcAngle`: The extent of the arc in degrees (positive for counterclockwise rotation).

Example usage:
Int x = 50, y = 50, width = 100, height = 80;
Int startAngle = 45, arcAngle = 90; // Start at 45 degrees, arc extent of 90 degrees
g.drawArc(x, y, width, height, startAngle, arcAngle); // Draws an arc within the specified
bounding rectangle
```
In this example, the arc is drawn within the bounding rectangle defined by `x`, `y`, `width`, and
`height`. The `startAngle` specifies where the arc begins, and `arcAngle` specifies the extent of
the arc.

2. **public void fillArc**:


- This method is used to fill an arc (a portion of an ellipse) with a specified color on a graphical
component.
- Parameters:
- `int x`: The x-coordinate of the upper-left corner of the bounding rectangle of the ellipse.
- `int y`: The y-coordinate of the upper-left corner of the bounding rectangle of the ellipse.
- `int width`: The width of the bounding rectangle of the ellipse.
- `int height`: The height of the bounding rectangle of the ellipse.
- `int startAngle`: The starting angle of the arc in degrees (0 degrees is at the 3 o’clock
position).
- `int arcAngle`: The extent of the arc in degrees (positive for counterclockwise rotation).

Example usage:
Int x = 50, y = 50, width = 100, height = 80;
Int startAngle = 45, arcAngle = 90; // Start at 45 degrees, arc extent of 90 degrees
g.fillArc(x, y, width, height, startAngle, arcAngle); // Fills an arc within the specified bounding
rectangle

Similar to `drawArc`, `fillArc` fills the arc within the bounding rectangle with a specified
color.

These methods are commonly used to create arcs or segments of ellipses in graphical user
interfaces, such as for creating pie charts or circular progress indicators.
11. SET COLOR
In Java’s graphics libraries like AWT or Swing, `public void setColor(Color c)` is a method used
to set the drawing color for subsequent drawing operations. Here’s an explanation of its
parameters:

- `Color c`: This parameter specifies the color to be used for drawing. It can be an instance
of the `java.awt.Color` class, which represents a color in terms of its red, green, and blue
(RGB) components.

Example usage:
Color redColor = Color.RED; // Create a Color object for red color
g.setColor(redColor); // Set the drawing color to red
g.fillRect(50, 50, 100, 80); // Fill a rectangle with the current drawing color (red in this case)
```

In this example, `setColor(Color c)` is used to set the drawing color to red (`Color.RED`), and
then a rectangle is filled with this red color using `fillRect(int x, int y, int width, int height)`.

The `setColor(Color c)` method allows you to customize the appearance of shapes, text, and
other graphical elements by specifying the color to be used in subsequent drawing operations.

You might also like