Java Reference
In-Depth Information
arrays. Instead, they are represented as arrays of references to array objects.
Those arrays could themselves contain references to other arrays. This layer-
ing continues for as many dimensions as required. Because of this technique
for representing each dimension, the arrays in any one dimension could be of
different lengths. These are sometimes called ragged arrays. For example, the
number of elements in each row of a two-dimensional array may not be the
same. In such situations, care must be taken to make sure the arrays are man-
aged appropriately.
SELF-REVIEW QUESTIONS (see answers in Appendix N)
SR 8.23 How are multidimensional arrays implemented in Java?
SR 8.24 A two-dimensional array named scores holds the test scores for a
class of students for a semester. Write code that prints out a single
value that represents the range of scores held in the array. It prints out
the value of the highest score minus the value of the lowest score. Each
test score is represented as an integer.
8.7 Polygons and Polylines
Arrays are helpful when drawing complex shapes. A polygon, for example, is
a multisided shape that is defined in Java using a series of (
x, y ) points that
indicate the vertices of the polygon. Arrays are often used to store the list of
coordinates.
Polygons are drawn using methods of the Graphics class, similar to how we
draw rectangles and ovals. Like these other shapes, a polygon can be drawn
filled or unfilled. The methods used to draw a polygon are called drawPolygon
and fillPolygon . Both of these methods are overloaded. One version uses
arrays of integers to define the polygon, and the other uses an object of the
Polygon class to define the polygon. We discuss the Polygon class later in this
section.
In the version that uses arrays, the drawPolygon and fillPolygon methods
take three parameters. The first is an array of integers representing the
x coordi-
nates of the points in the polygon, the second is an array of integers representing
the corresponding y coordinates of those points, and the third is an integer that
indicates how many points are used from each of the two
arrays. Taken together, the first two parameters represent the
( x, y ) coordinates of the vertices of the polygons.
A polygon is always closed. A line segment is always drawn
from the last point in the list to the first point in the list.
KEY CONCEPT
A polyline is similar to a polygon
except that a polyline is not a closed
shape.
 
Search WWH ::




Custom Search