Saturday, October 9, 2010

DRAWING SHAPES USING JAVA 2D





Java, as a programming language, has a standard class library which provides many classes that let us present and manipulate graphical information. Our priority is the Graphics class and the Color class which are defined in the java.awt package. They contain various methods that allow us to draw shapes, including lines, rectangles, and ovals. As you can see above, several shapes have been drawn using the Java language, particularly using the Graphics class and the Color class. These shapes were made in such an exciting way of which I am going to outline below the methods i used in coming up with the shapes drawn above.
As I have already mentioned, the package used is the java.awt package.
Shape 1
The first shape, a star, is an example of a polygon. It can be drawn filled or unfilled. The methods used to draw a polygon are drawPolygon and fillPolygon. Hence, the first shape was drawn using the fillPolygon method that takes parameters. The first is an array of integers representing the x coordinates of the points in the polygon, the second is an array of integers representing the y coordinates of the points in the polygon and the third is an integer that indicates how many points are used from each of the two arrays. The colors were filled using the method setColor().
Shape 2
This is a circle with a certain portion that has been cut from it. The method used to create this shape is the fillArc() method that takes parameters. Here are the parameters you need to specify:
·         The starting angle for the arc in degrees-0 is due east, or 3 o'clock as they say in the movies.
·         The extent, which is an angle that represents how much of the ellipse the arc spans. This too is specified in degrees. The important thing to know is that the arc travels counterclockwise from the starting point. So if you specify 0 as the starting point and 90 as the extent, the arc travels from 3 o'clock to 12 o'clock high.
 The method setColor(Color.GRAY) was specified for the color of the shape.
Shape 3
These are a set of semi-circles joined together in a line. The method used here is the same as the one above. So since there are 4 semi-circles, therefore the method will have to be used four times.

Shape 4
The fourth shape is that of the form that looks like a doughnut. The method used was that of fillOval () which draws circles. There are two circles drawn here. The first circle is the outer black-filled one and the second is the small inner white-filled circle which together with the black one produces the shape show-cased above.
Shape 5
This shape (consisting of flower petals) uses the same method as the one used to draw the fourth shape but with different rather smaller parameters
Shape 6.
This is the last shape and as you can see, it is some kind of a vehicle. The method used to draw this shape is the fillPolygon(). Just like with the first shape, this takes (x, y) as an array of parameters and n as an integer that indicates how many points are used from each of the two arrays. For the wheels, I used the method i used to draw shape 4, only with smaller parameters. It's really that simple!


I can not say i am an expect in Java programming but what i can tell is i have learnt a lot during my study of this program and it's more fun than i anticipated. I am really looking forward to my next project involving Java programming.

No comments:

Post a Comment