Program 2  CS 100J Fall 2000

Due.  In lecture on Thursday, September 14, 2000. You may turn it in to a consultant before that date in the consulting room in Carpenter. Do not turn it in at Carpenter on the due date. Programs will not be accepted late.

Note.  Always write your name, Cornell ID#, and the day/time/instructor for your section in the first comment of any program you hand in for credit; otherwise it will not be graded. This cannot be written in by hand. If you wrote the program with a partner, turn in only one printout with your partner's name and ID# in the comment, as well as your own. The comment must also include the section day/time/instructor for partner; the program will be returned to the first person listed. Sign your name(s) by the comment. Please staple the pages of your assignment together.

Grading. This assignment will be given two grades: the first based on correctness, the second on program organization and style. Each grade will be a 0, 4, or 5. Not only should your program work, but it should contain adequate comments to guide the reader who is interested in understanding it. The declaration of every significant variable should include a comment describing that variable. There should be appropriate comments in the code so the reader can see the structure of the program, but not so many that the program text is hard to read.

Goals. To give you experience using the basic constructs of the Java language, including input and output statements, assignments, loops, and conditionals. To demonstrate some of the basic graphics functions available in Java.  To illustrate how to develop a program in steps.

Problem Statement

Write a program that draws a series of polygons based on a list of coordinates provided by the user. A polygon is an ordered sequence of 3 or more points in the plane connected by straight-line segments. Your program must also compute and draw a bounding box for each polygon. A bounding box is a rectangle, with vertical and horizontal sides, that fits tightly around the polygon.

Program input

The input of your program consists of zero or more points that specify zero or more polygons. Each point is given as pairs of integer <x,y> coordinates in the range 0 through 100, where x is column, and y is row. A pair of -1’s terminate the input. 

The first point of the first polygon (if there is at least one polygon) is given by the first pair of integers in the input; the remaining points of the first polygon are given by subsequent input pairs up until the first point is repeated. The first point of the second polygon (if there is a second polygon) is given by the next input pair, and so on.

Here is some sample input:

10 10  50 15  5  20  10 10                                                                                     50 50  60 60  55 75  50 50                                                                                  -1 -1    

Based on this input, your program should draw two polygons (two triangles in this case), one starting at  <10, 10> and one starting at  <50, 50>.  For each polygon, your program has to compute and draw a bounding box, which is determined by the maximum and minimum x and y coordinates of the polygon.

Note that it is OK if the lines of a polygon cross over one another.  You can assume that the input is well formed. In particular, the following forms of degenerate “polygon” will NOT occur:

·         0 sides (e.g., 10 10  10 10)

·         2 sides (e.g., 10 10  50 15  10 10)

·         open (e.g., 10 10  50 15  –1 –1)

Program output

You will develop a program called GraphicsPolygon in three steps.

In Step 1, you write a program that (despite its name) has only textual output. For this step, you should process the input and print out the following textual information for each polygon:

Polygon # number

Drawing line from point to point.

(for each edge of the polygon)

Polygon # number has number edges.

Drawing rectangle with top-left corner point.

Bounding box area is number.

After processing all polygons, output:

Total number of polygons: number.

Given the above sample input, your program should output:

Polygon #1

Drawing line from <10, 10> to <50, 15>.

Drawing line from <50, 15> to <5, 20>.

Drawing line from <5, 20> to <10, 10>.

Polygon #1 has 3 edges.

Drawing rectangle with top-left corner <5, 10>.

Bounding box area is 450.

Polygon #2

etc.

Total number of polygons: 2.

In Steps 2 and 3, you modify this program to have graphical output. Graphical output is discussed further below.

Step 1: Textual Output

In the first step, you write a plain text-based Java application with no graphical output. Your program should read the input given in the format above and produce the textual output in the format described. The main goal of this part is to make sure you read the input, interpret it as a series of polygons, and compute the bounding boxes.  This will give you practice in using input and output statements, assignment, loop, and conditional statements.  You may find it convenient to break Step 1 into even finer steps, e.g., your initial goal for Step 1 might be to produce just the output

Polygon #1

Polygon #1 has 3 edges.

Polygon #2

Polygon #2 has 3 edges.

Total number of polygons: 2.

As with the your first programming assignment, you should use the CUCS Java Application Stationery.

Step 2: Textual and Graphical Output

Things get interesting, of course, when you can actually see the polygons and bounding boxes being drawn on your screen! In Step 2, you adapt your code from Step 1 to make it execute graphics commands.  You should leave your textual output statements in the program to help in developing Step 2.

Java is an excellent language for writing graphics-based programs, in particular, "applets", which are Java programs that run on the World Wide Web. Applets are a special kind of Java program that are executed by web browsers. Applets will be discussed later in the course. For now, we will use a standard Java application that will have both standard text-based input and output window (same as for Assignment 1) and a graphics window for drawing the polygons and bounding boxes.

Even though graphics programming in Java is quite straightforward, we will give you the basic framework for your graphics program. This is needed because some of the concepts required, such as class methods, will only be discussed a bit later in the course.

Below is a code skeleton for your application:

import java.io.*;

import java.awt.*;

// Class GraphicsPolygon

// Give high-level description of your program.

 

public class GraphicsPolygon {

        public static void main( String args[] )

        {

// Initialize graphics context

                PolygonFrame gui = new PolygonFrame();

                Graphics easel = gui.getGraphics();

                final int stretch = 5;

 

// Initialize Text object in to read from standard input.

TokenReader in = new TokenReader(System.in);

 

                // your code here

        }

}

This should look quite familiar to you except for the boxed lines, which you should add to your Step-1 code.  The line

import java.awt;

gives your application access to the Java “Window Toolkit” known as awt.

The lines introduced with the comment

// Initialize graphics context

declare and initialize three variables.  You will use the first two variables (gui and easel) without understanding exactly what they are; you will use variable stretch understanding that it is just a synonym for the constant 5.

Your application will also need two other classes, PolygonFrame and GraphicsUtil, that we have written for you.  You should download these classes from the course web pages, save them as files, and add them to your project.

Then modify your Step-1 code to do the actual drawing of the polygons.

class PolygonFrame

This class defines the method PolygonFrame that your application uses to draw the basic graphics window:

import java.awt.*;

//      Class PolygonFrame creates a graphic context for the

//      GraphicsPolygon class

class PolygonFrame extends Frame

{

        PolygonFrame ( )

        {

                super("Polygons");

                setSize(500, 500);

                show();

         }

}

 

Downloaded this class from the course web page at http://www.cs.cornell.edu/Courses/CS100J/2000fa, look under Programming Assignments, save it in a file called PolygonFrame.java, and add it to your GraphicsPolygon project.

class GraphicsUtil

This class defines two methods that you will use for drawing straight lines and rectangles. The code for the class is not listed here.

Downloaded the class from the course web pages at http://www.cs.cornell.edu/Courses/CS100J/2000fa,  look under Programming Assignments, save it in a file called GraphicsUtil.java, and add it to your GraphicsPolygon project.

Method GraphicsUtil.drawLine

To draw a straight line between two points in the graphics window created by PolygonFrame, use the GraphicsUtil.drawLine method. For example, to draw a line between <10,10> and <50,15>, you would use the statement

GraphicsUtil.drawLine(easel,  

      (stretch * 10),  (stretch * 10), (stretch * 50), (stretch * 15),

      4, Color.blue);

The first argument easel refers to the graphics area where the program should draw the line. The next four arguments give the x and y coordinates of the starting and ending points of the line segment. We use the “stretch” constant to scale up the figure by a constant factor. The next argument gives the line thickness in number of pixels (4 is a reasonable choice), and the final argument gives the color of the line.

Method GraphicsUtil.drawRect

To draw a rectangle, use the

GraphicsUtil.drawRect method. For example, to draw a rectangle with the top-left corner at <10,10> and a width of 40 and a height of 10, use the statement:

GraphicsUtil.drawRect(easel,

      (stretch * 10), (stretch * 10), (stretch * 40), (stretch * 10),

      3,  Color.green);

The line thickness used is 3 in this case, and the color is green.

Step 3: Final Program

Now delete the textual output statements so that your final program just reads textual input and produces graphical output.  Suggestion: do not do Step 3 until you are sure that Step 2 is correct and complete.

Testing

A correct program does what the problem statement requires for any input data permitted, not just the input data given as an example in the handout. So you should experiment with different data sets to debug your program.

If your program does not produce the expected output when you first run it, you may find it helpful to temporarily insert extra output statements in key locations of the program to help you understand what is happening.

Note that when running your program, you should make sure that you text input window does not overlap with your graphics window.

What to Hand In

Run programs on the following input data set:

10 10  50 15   5 20  10 10

25 30  39 55  88 75  70 35  35 25  25 30

12 35  17 60  18 90  21 30  12 35

50 70  60 75  70 80  75 75  60 60  50 70

40 9   45 25  60 15  65 35  70 15  55 9

       45  7  40  9

-1 -1

Hand printouts of

·         Step 1: Java code and textual output.

·         Step 3: Java code and a screen snapshot of the graphics output.

Instructions for taking screen snapshots are given in section 1.4.5 of the Guide to CodeWarrior Java for CS100 and CS211.

Please remember to staple all material together.