Program 2 Solution




Write a Java program which uses the LWJGL library to draw a window of 640×480 (with a black background). The coordinate system should be centered in this window. Your program will read a file titled coordinates.txt and draw the corresponding filled polygon in this window using the scanline polygon fill algorithm. Each specified polygon should be filled in the color specified in the text file and then undergo the transformations specified in the input file before being drawn on the screen. Hence you will need to calculate the composition of the transformation matrices and then calculate the position of the polygon vertices and then apply the scan line fill algorithm. Use the glVertex2f() command to plot the pixel and glColor3f() to specify the color. Finally, your program should also use the input.Keyboard class to have the escape key quit your application.

If you are motivated enough, you may also add extra functional keys that would allow the user to change the colors of the polygon fills. These added functions should be clearly stated in your comments.

The given coordinates.txt file will be in the following format:

P 0.5 0.6 0.3

  1. 300

  1. 150

  1. 400

  1. 150

  1. 300


r 45 0 0

s 0.5 1.5 0 0 t 200 –150 r –90 0 0

P 0.2 0.4 0.7 -350 –350 -350 350

  1. 350

  1. 350


r 90 0 0

t 100 100

s 0.5 0.5 0 0

The file coordinates.txt will contain an ordered set of vertices for the polygon. The polygon can be drawn by joining the first vertex to the second, the second to the third and so on, until the final vertex is joined to the first to close the polygon. It denotes the polygon vertices by using the symbol P and the transformations for that polygon by using the symbol T. The symbol P is followed by three-float numbers that signify the R, G and B values specifying the fill color of the polygon. The symbols r, s and t stand for rotation, scaling and translation respectively. The symbol r is followed by the rotation angle, and the coordinate for the pivot point. Similarly the symbol s is followed by the scaling factors in the x and y direction and the pivot point coordinates. The symbol t

is followed by the translation coordinates in the x and y direction. The sample file shows two polygons and the transformations for each polygon.

When your program is evaluated, a different coordinates.txt file will be used by me– so be sure your algorithm tests all cases, such as polygons with interior and exterior regions as well as self intersections. I will have more than two polygons in my test file, so read till end of the file and not just two polygons.

Note: Please do not turn in anything that does not use Java with our LWJGL library. Your programs should be hand-coded. Hence, don’t use pre-existing code from other texts or from the internet.

What to turn in:

– Soft copy of the program (using Blackboard)