0% found this document useful (0 votes)
11 views17 pages

Java Graphics Class Drawing Methods

The document describes the Graphics class in Java, detailing various drawing methods for shapes such as lines, rectangles, ovals, arcs, and polygons. It provides syntax examples for each method and includes sample Java applet programs demonstrating how to use these methods. Additionally, it covers drawing techniques using control loops and creating bar charts within applets.

Uploaded by

suvi6718
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)
11 views17 pages

Java Graphics Class Drawing Methods

The document describes the Graphics class in Java, detailing various drawing methods for shapes such as lines, rectangles, ovals, arcs, and polygons. It provides syntax examples for each method and includes sample Java applet programs demonstrating how to use these methods. Additionally, it covers drawing techniques using control loops and creating bar charts within applets.

Uploaded by

suvi6718
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

The Graphics Class

The graphics class defines a number of drawing functions, Each shape


can be drawn edge-only or filled. To draw shapes on the screen, we may
call one of the methods available in the graphics class. The most
commonly used drawing methods included in the graphics class are
listed below. To draw a shape, we only need to use the appropriate
method with the required arguments.

Drawing Methods of the Graphics Class


[Link] Method Description
1. clearRect() Erase a rectangular area of the canvas.
Copies a rectangular area of the canvas to
2. copyAre()
another area
3. drawArc() Draws a hollow arc
4. drawLine() Draws a straight line
5 drawOval() Draws a hollow oval
6 drawPolygon() Draws a hollow polygon
7 drawRect() Draws a hollow rectangle
8. drawRoundRect() Draws a hollow rectangle with rounded corners
9. drawString() Display a text string
10. FillArc() Draws a filled arc
11. fillOval() Draws a filled Oval
12. fillPolygon() Draws a filled Polygon
13. fillRect() Draws a filled rectangle
14. fillRoundRect() Draws a filled rectangle with rounded corners
15. getColor() Retrieves the current drawing color
16. getFont() Retrieves the currently used font
17. getFontMetrics() Retrieves the information about the current font
18. setColor() Sets the drawing color
19. setFont() Sets the font

Lines and Rectangles


Lines are drawn by means of the drawLine() method.

Syntax
void drawLine(int startX, int startY, int endX, int endY)

drawLine() displays a line in the current drawing color that begins at


(start X, start Y) and ends at (endX, end Y).

//Drawing Lines
import [Link].*;
import [Link].*;
/*
<applet code="Lines" width=300 Height=250>
</applet>
*/
public class Lines extends Applet
{
public void paint(Graphics g)
{
[Link](0,0,100,100);
[Link](0,100,100,0);
[Link](40,25,250,180);
[Link](5,290,80,19);
}
}

After this you can comiple your java applet program as shown below:

"Output of [Link]"
Rectangle

The drawRect() and fillRect() methods display an outlined and filled


rectangle, respectively.

Syntax
void drawRect(int top, int left, int width, int height)
void fillRect(int top, int left, int width, int height)

The upper-left corner of the rectangle is at(top,left). The dimensions of


the rectangle are specified by width and height.

Use drawRoundRect() or fillRoundRect() to draw a rounded rectangle. A


rounded rectangle has rounded corners.

Syntax
void drawRoundRect(int top, int left, int width, int height int Xdiam, int
YDiam)
void fillRoundRect(int top, int left, int width, int height int Xdiam, int
YDiam)

The upper-left corner of the rounded rectangle is at (top,left). The


dimensions of the rectangle are specified by width and height. The
diameter of the ribdubg are along the X axis are specified by x Diam.
The diameter of the rounding are along the Y axis is specified by Y
Diam.

*/
import [Link].*;
import [Link].*;
/*
<applet code="Rectanlge" width=300 Height=300>
</applet>
*/
public class Rectanlge extends Applet
{
public void paint(Graphics g)
{
[Link](10,10,60,50);
[Link](100,100,100,0);
[Link](190,10,60,50,15,15);
[Link](70,90,140,100,30,40);
}
}

After this you can comiple your java applet program as shown below:

c:\jdk1.4\bin\javac [Link]
c:\jdk1.4\bin\appletviewer [Link]
"Output of [Link]"

Circles and Ellipses


The Graphics class does not contain any method for circles or ellipses.
To draw an ellipse, use drawOval(). To fill an ellipse, use fillOval().

Syntax
void drawOval(int top, int left, int width, int height)
void fillOval(int top, int left, int width, int height)
The ellipse is drawn within a bounding rectangle whose upper-left
corner is specified by (top,left) and whose width and height are specified
by width and height. To draw a circle, specify a square as the bounding
rectangle i.e get height = width.

The following program draws several ellipses:


*/
import [Link].*;
import [Link].*;
/*
<applet code="Ellipses" width=300 Height=300>
</applet>
*/
public class Ellipses extends Applet
{
public void paint(Graphics g)
{
[Link](10,10,60,50);
[Link](100,10,75,50);
[Link](190,10,90,30);
[Link](70,90,140,100);
}
}
After this you can compile your java applet program as shown below:

c:\jdk1.4\bin\javac [Link]
c:\jdk1.4\bin\appletviewer [Link]
"Output of [Link]"

Drawing Arcs
An arc is a part of oval. Arcs can be drawn with draw Arc() and fillArc()
methods.

Syntax
void drawArc(int top, int left, int width, int height, int startAngle, int
sweetAngle)
void fillArc(int top, int left, int width, int height, int startAngle, int
sweetAngle)

The arc is bounded by the rectangle whose upper-left corner is specified


by (top,left) and whose width and height are specified by width and
height. The arc is drawn from startAngle through the angular distance
specified by sweepAngle. Angles are specified in degree. '0o' is on the
horzontal, at the 30' clock's position. The arc is drawn conterclockwise if
sweepAngle is positive, and clockwise if sweetAngle is negative.

The following applet draws several arcs:


*/
import [Link].*;
import [Link].*;
/*
<applet code="Arcs" width=300 Height=300>
</applet>
*/
public class Arcs extends Applet
{
public void paint(Graphics g)
{
[Link](10,40,70,70,0,75);
[Link](100,40,70,70,0,75);
[Link](10,100,70,80,0,175);
[Link](100,100,70,90,0,270);
[Link](200,80,80,80,0,180);
}
}
After this you can comiple your java applet program as shown below:

c:\jdk1.4\bin\javac [Link]
c:\jdk1.4\bin\appletviewer [Link]

"Output of [Link]"

Drawing Polygons
Polygons are shapes with many sides. It may be considered a set of lines
connected together. The end of the first line is the beginning of the
second line, the end of the second line is the beginning of the third line,
and so on. Use drawPolygon() and fillPolygon() to draw arbitrarily
shaped figures.
Syntax
void drawPolygon(int x[] , int y[], int numPointer)
void fillPolygon(int x[] , int y[], int numPointer)

The polygon's endpoints are specified by the coordinate pairs contained


within the x and y arrays. The number of points defined by x and y is
specified by numPoints.

It is obvious that x and y arrays should be of the same size and we must
repeat the first point at the end of the array for closing the polygon.

The following applet draws an hour glass shape:


*/
import [Link].*;
import [Link].*;
/*
<applet code="Polygon" width=300 Height=300>
</applet>
*/
public class Polygon extends Applet
{
public void paint(Graphics g)
{
int xpoints[]={30,200,30,200,30};
int ypoints[]={30,30,200,200,30};
int num=5;
[Link](xpoints,ypoints,num);
}
}
After this you can compile your java applet program as shown below:
c:\jdk1.4\bin\javac [Link]
c:\jdk1.4\bin\appletviewer [Link]

"Output of [Link]"

Draw Line Graph in Java Applet


import [Link].*;
import [Link].*;
public class Line_Graph extends Applet
{
int x[]={ 0, 60, 120, 180, 240, 300, 360, 400};
int y[]={ 400, 280, 220, 140, 60, 60, 100, 220};
int z=[Link];
public void paint(Graphics g)
{
[Link](x, y, z);
}
}
<html>
<head>
</head>
<body>
<applet code = "Line_Graph.class" width = "640" height =
"480"></applet>
</body>
</html>
Output:
using control loops in Applets
Here, we have shown four circle one below other using "for" loop with
two different colors. The methods used
are drawOval( ), fillOval( ) and setColor( ).

PROGRAM
import [Link].*;
import [Link].*;

public class ControlLoopApplet extends Applet


{
public void paint(Graphics g)
{
for(int i=1;i<=4;i++)
{
if(i%2==0)
{
[Link](90,i*50+10,50,50);
[Link]([Link]);
}
else
{
[Link](90,i*50+10,50,50);
[Link]([Link]);
}
}
}
}
/* <applet code=ControlLoopApplet width=300 height=300>
</applet> */
OUTPUT

C:\>javac [Link]
C:\>appletviewer [Link]

Program to create applet to draw Bar Chart


Here, we are displaying bar chart on applet window. The values for
labels, columns are passed from applet's <PARAM> tag and stored in
applet's init( ) method with the help of arrays. In <APPLET> tag first
four parameters are for column values, next four parameters are for
column labels and the last parameter is for the number of columns in bar
chart.

PROGRAM
import [Link].*;
import [Link].*;
public class BarChart extends Applet
{
int n=0;
String label[];
int value[];
public void init() {
setBackground([Link]);
try {
int n = [Link](getParameter("Columns"));
label = new String[n];
value = new int[n];
label[0] = getParameter("label1");
label[1] = getParameter("label2");
label[2] = getParameter("label3");
label[3] = getParameter("label4");
value[0] = [Link](getParameter("c1"));
value[1] = [Link](getParameter("c2"));
value[2] = [Link](getParameter("c3"));
value[3] = [Link](getParameter("c4"));
}
catch(NumberFormatException e){}
}
public void paint(Graphics g)
{
for(int i=0;i<4;i++) {
[Link]([Link]);
[Link](label[i],20,i*50+30);
[Link]([Link]);
[Link](50,i*50+10,value[i],40);
}
}
}

/* <applet code=BarChart width=400 height=400>


<param name=c1 value=110>
<param name=c2 value=150>
<param name=c3 value=100>
<param name=c4 value=170>
<param name=label1 value=1991>
<param name=label2 value=1992>
<param name=label3 value=1993>
<param name=label4 value=1994>
<param name=Columns value=4>
</applet>
*/

OUTPUT

C:\>javac [Link]
C:\>appletviewer [Link]

You might also like