Solution for Step 1: Textual Output
/**
* Name (s ):
* ID ( s ):
* Section (s ):
*
* CS100J Program 2 - Fall 2000
*
* Draws a series of polygons
based on a list of coordinates provided by the user. Computes
* the number of
edges for each polygon, as well as the area of a bounding box.
*/
public class GraphicsPolygon
{
/**
* The main method.
* @param args Command
line argument list
*/
public static void main( String args[] )
{
// current number
of Polygons
int numOfPolygons = 0;
// number of edges in latest polygon
int numOfEdges = 0;
// Initialize Text object in to read from standard
input.
TokenReader in = new
TokenReader(System.in);
// Starting coordinates for current polygon
int firstX, firstY;
// Coordinates of xMin, yMin, xMax, and yMax for current
polygon
int xMin, yMin, xMax, yMax;
// Coordinates of latest point
int x, y;
System.out.println(
"Enter the coordinates: " );
xMin = xMax = firstX = x = in.readInt();
yMin = yMax = firstY = y = in.readInt();
System.out.println();
// Continue to draw lines and polygons until the user
enters -1 -1
while ( x >=0
&& y >=0 )
{
// previous point
int xPrev = x;
int yPrev = y;
x = in.readInt();
y = in.readInt();
numOfEdges++;
// Update minimum and maximum variables if
necessary
if ( x < xMin )
xMin =
x;
else if ( x > xMax )
xMax =
x;
if ( y < yMin )
yMin =
y;
else if (y > yMax )
yMax =
y;
// Draw a line to the current point only if it is
not -1 -1
if ( x
>=0 && y >=0 )
System.out.println("Drawing
line from <" + xPrev + ", " +
+ yPrev + "> to <" + x + ",
" + y + ">");
if ( x == firstX && y == firstY )
{
// Print message indicating box has closed.
System.out.println("Polygon
#" + (++numOfPolygons) + " has " +
+ numOfEdges + " edges." );
System.out.println("Drawing
rectangle with top-left corner <" +
+ xMin + ", "+ yMin + ">.");
System.out.println("Bounding
box area is " +
+ ( ( xMax - xMin )*( yMax - yMin ) )
+".\n");
// Reset variables for next polygon
xMin =
xMax = firstX = x = in.readInt();
yMin =
yMax = firstY = y = in.readInt();
numOfEdges
= 0;
}
}// end while
System.out.println("Total number of polygons:
" + numOfPolygons );
}
}
/*
SAMPLE OUTPUT:
Enter the coordinates:
10 10 50 15
5 20 10 10 50 50 60 60 55 75
50 50 -1 -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.
Drawing line from <50,
50> to <60, 60>
Drawing line from <60,
60> to <55, 75>
Drawing line from <55,
75> to <50, 50>
Polygon #2 has 3 edges.
Drawing rectangle with
top-left corner <50, 50>.
Bounding box area is 250.
Total number of polygons: 2
*/
Solution for Step 2: Graphical Output
import java.awt.*;
import java.io.*;
public class GraphicsPolygon
{
/**
* The main method.
* @param args Command
line argument list
*/
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 );
// Starting coordinates for current polygon
int firstX, firstY;
// Coordinates of xMin, yMin, xMax, and yMax for current
polygon
int xMin, yMin, xMax, yMax;
// Coordinates of latest point entered
int x, y;
System.out.println(
"Enter the coordinates: " );
xMin = xMax = firstX = x = in.readInt();
yMin = yMax = firstY = y = in.readInt();
// Continue to draw lines and polygons until the user
enters negative values
while ( x >=0 && y >=0 )
{
int xPrev = x;
int yPrev = y;
x = in.readInt();
y = in.readInt();
// Update minimum and maximum variables if
necessary
if ( x < xMin )
xMin =
x;
else if ( x > xMax )
xMax =
x;
if ( y < yMin )
yMin =
y;
else if (y > yMax )
yMax =
y;
// Draw a line to the current point only if the
current point is not -1 -1
if ( x !=
-1 && y != -1 )
GraphicsUtil.drawLine(easel,
(stretch * xPrev), (stretch * yPrev),
(stretch
* x), (stretch * y), 4, Color.blue);
// Draw bounding rectangle if we have a closed polygon
if ( x == firstX && y == firstY )
{
GraphicsUtil.drawRect(easel,
(stretch * xMin ), (stretch * yMin ),
(
stretch *( xMax-xMin )), (stretch *( yMax-yMin )), 3, Color.green);
xMin =
xMax = firstX = x = in.readInt();
yMin =
yMax = firstY = y = in.readInt();
}
} // end while
}
}
Alternate solution template ( using nested do-while
loops )
System.out.println(
"Enter the coordinates: " );
do {
xMax
= xMin = x = xFirst = in.readInt();
yMax
= yMin = y = yFirst = in.readInt();
do{
//
Update minimum and maximum variables if necessary
if ( x < xMin )
xMin = x;
else if ( x > xMax )
xMax = x;
if ( y < yMin )
yMin = y;
else if (y > yMax )
yMax = y;
int xPrev = x;
int yPrev = y;
x = in.readInt();
y = in.readInt();
//
Draw a line to the current point only if the current point is not -1 -1
if ( x != -1
&& y != -1 )
GraphicsUtil.drawLine(easel, ( stretch * xPrev ), ( stretch * yPrev ),
( stretch * x ), ( stretch * y ), 4, Color.blue);
}
while ( x != xFirst || y != yFirst );
//
Draw bounding box
GraphicsUtil.drawRect(easel,
( stretch * xMin ), ( stretch * yMin ),
(stretch *( xMax - xMin )), (stretch *( yMax - yMin
)), 3,
Color.green);
} while ( x>=0 &&
y>=0 );