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

What is actionmapping, Action mapping having all the deployment information...

Action mapping having all the deployment information for a particular Action bean. This class is to verify where the results of the Action will be sent once its processing is done.

Terminology, what is mearnt by a variable in java

what is mearnt by a variable in java

Java, 1. Which of the following are not valid Java identifiers, and why? (...

1. Which of the following are not valid Java identifiers, and why? (a) wolVes (b) United(there is only one) (c) 87 (d) 5 3 (e) Real ale (f) isFound?by 2. A class Television

Program, a program that groups items according to their number

a program that groups items according to their number

Prepare a computer graded test, Please check out the given instruction that...

Please check out the given instruction that I received to do the assessment. I can provide you that link to go on and submit the answers. To assess your coding skills, we would

Moving test, After refactor the main class to become abstract class, I want...

After refactor the main class to become abstract class, I want to move test method from tset clas of main class to subclass, how to do that?

Prepare a java program to read .gpx files, Prepare a Java program to read ....

Prepare a Java program to read .GPX files Project Description: Prepare a Java program (GPX_Tracks_to_Text.Java) for Windows to: - Read every .GPX file in a provide drive a

What does a simple spring application contain, These applications are like ...

These applications are like any Java application. They are made up of not many classes, each performing a particular purpose within the application. But these classes are configure

When should i use abstract classes and interfaces, When should I use abstra...

When should I use abstract classes and when should I use interfaces?  Use Interface, when: Design changing often or when various implementations only share method sign

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