Java Graphics Draw Polygons Applets

Java Graphics Draw Polygons Applets


3rd style: Using drawLine() method.

Each and every line of the polygon is drawn separately (with drawLine()) while attaching end-points of one line to the starting points of another. If this is not done properly, you get a open polygon figure. This is a tedious approach compared to other 3 styles.

Java Graphics Draw Polygons Applets

Observe the line coordinates carefully. One line x1 and y1 coordinates are another line's x2, y2. Again starting line x1, y1 are last line's x2, y2; else, an open figure will be obtained.

4th style: Using addPoint() method.

Here, we use addPoint() method of Polygon class. addPoint() method takes a pair of coordinates that becomes automatically the vertex of the polygon. drawPolygon() method takes care of joining the first vertex with the last vertex.

Supporting method of Polygon class

  • void addPoint(int x, int y): drawPolygon() mehod joints all the points of x and y specified in each addPoint() method.

Java Graphics Draw Polygons Applets

Now, you need not worry about the coordination of each x and y values. Give any values for each addPoint() method. The points are joined by the Polygon class implicitly.

One more program on graphics with applets is available in "Drawing Circles (Graphics with Applets)".

Note: Knowledge of "applets" is required to do with this program.

Note: In applet, window closing does not require extra code. It is implicit.

2 thoughts on “Java Graphics Draw Polygons Applets

Leave a Reply

Your email address will not be published. Required fields are marked *