R4R
Right Place For Right Person TM
 

R4R JAVAAWT ExamplesDraw Polygons in java

previous

Home

Next

Draw Polygons in java

In this example we are going to draw  polygons.
In this we are using Graphics class and Applet class.
void draw Polygon( int x[], int y[], int num Point): This method is used to draw Polygons.
void fill Polygon ( int x[], int y[], int num Point): This method is used to draw  and fill Polygons.
paint (Graphics g); This method is used to paint the applet.
Here draw Polygon  and fillies use to create Polygons. Polygons end point are specified by the coordinate pair contained within the x and y array. The number of points of polygons are specified by num Point.
Here first we are importing two pacakages.Then after we have write applet tag into comments. In this tag we have pass three arguments code ,height  and width. The code attribute is used to call the applet class. And Height , width is used for Sizing the window.
Then we have create a class and extends this class with Applet Class to accurse the property of applet. We had override paint method of applet class. Then we passes the points in array x and y . Then we set the number (num Point) for drawing the polygon of appropriate number.   



//Draw polygons
import java.awt.*;
import java.applet.*;
/*
<applet code="Polygons" width=230 height=210>
</applet>
*/
public class Polygons extends Applet{
public void paint(Graphics g){
int xpoints[]={70,100,130,70,130};
int ypoints[]={70,30,70,100,100};
int num=6;
g.drawPolygon(xpoints , ypoints , num);
}
}

Download Source Code

Output of Example

previous

Home

Next

New Updates

R4R
R4R
R4R
R4R
R4R
R4R
R4R
R4R