How to draw polygons in java, JAVA Programming

Assignment Help:

How to draw Polygons in java?

In Java rectangles are defined through the position of their upper left hand corner, their height, and their width. Therefore it is implicitly supposed that there is in fact an upper left hand corner. Not all rectangles have an upper left hand corner. For example consider the rectangle below.

Where is its upper left hand corner? What's been supposed so far is that the sides of the rectangle are parallel to the coordinate axes. You can't yet handle a rectangle which been rotated at an arbitrary angle.
There are a few other things you can't handle either, triangles, rhombuses, stars, kites, octagons and more. For take care of this broad class of shapes Java has a Polygon class.

Polygons are defined through their corners. No assumptions are made about them except in which they lie in a 2-D plane. The primary constructor for the Polygon class is

public Polygon(int[] xpoints, int[] ypoints, int npoints)

xpoints is an array which contains the x coordinates of the polygon. ypoints is an array in which contains the y coordinates. Both should have the length npoints. Therefore to construct a 3-4-5 right triangle along with the right angle on the origin you would type

int[] xpoints = {0, 3, 0};
int[] ypoints = {0, 0, 4};
Polygon myTriangle = new Polygon(xpoints, ypoints, 3);
To in fact draw the polygon you use java.awt.Graphics's drawPolygon(Polygon p) method inside your paint() techniques like this:

g.drawPolygon(myTriangle);
You could pass the arrays and number of points directly to the drawPolygon() method if you prefer:
g.drawPolygon(xpoints, ypoints, xpoints.length);
There's also an overloaded fillPolygon() method. The syntax is exactly as you expect:
g.fillPolygon(myTriangle);
g.fillPolygon(xpoints, ypoints, xpoints.length());
nt rectLeft = appletWidth/2 - i*appletWidth/16;
int rectTop = appletHeight/2 - i*appletHeight/16;
g.fillOval(rectLeft, rectTop, rectWidth, rectHeight);
}

}

}
The .class file that draws this image is only 684 bytes. The equivalent GIF image is 1,850 bytes, almost three times larger.

Almost all the work in this applet consists of centering the enclosing rectangles within the applet. The lines in bold do that. The first two lines just set the height and the width of the rectangle to the suitable fraction of the applet's height and width. The further two lines set the position of the upper left hand corner. Once the rectangle is positioned, drawing the oval is simple.


Related Discussions:- How to draw polygons in java

Difference between an interface and an abstract class, What is the differen...

What is the difference between an Interface and an Abstract class? An abstract class can have instance methods that execute a default behavior. An Interface can only declare co

Pebble merchant., there is a pebble merchant. he sells the pebbles,that are...

there is a pebble merchant. he sells the pebbles,that are used for shining the floor.his main duty is to take the length of the room side but he sometines mistakes doing that and m

What do mean by synchronized and synchronized statements, What do mean by ...

What do mean by synchronized methods and synchronized statements ?

Garbage collection guarantee, Is it right that garbage collection guarante...

Is it right that garbage collection guarantee that a program will not run out of memory?

Java pass arguments by value or reference, Does Java pass arguments by valu...

Does Java pass arguments by value or reference? Ans) Pass by value. 1. When passing primitives, it going a copy of the variable to the method. Any change made in the method d

Relate java with JVM, Relate java with JVM Java performance gets better...

Relate java with JVM Java performance gets better each year as JVM gets smarter. This works, since making JVM smarter doesn't require any great change to the java language, sou

How do i include static files within a jsp page, Static resources should al...

Static resources should always be contained using the JSP include directive. This way, the inclusion is performed just once during the translation phase. Do note that you should al

Develop a color picker, Project Description: We need same kind of concep...

Project Description: We need same kind of concept with some changes # Change single colors in a particular design. you need to be able to change the color of only the fox or

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd