Java Reference
In-Depth Information
Another thing we have to consider is the rendering of the graphical objects.
As the programmer can add new kinds of shapes, one cannot set up a drawing
method in advance that works for all of them. Only the programmer knows what
the graphical object has to look like. Therefore, we require that a graphical shape
knows how to draw itself. This concept is implemented by an abstract method
draw in class GraphicalObject . Every (concrete) class derived from it has to
implement this method.
There is, however, a problem here. To actually render the shape, the graphical
object has to know the pixel coordinates in the panel where the drawing is dis-
played. These depend on the panel's current size and the size of the drawing which,
in turn, depend on other graphical objects in the drawing. The necessary infor-
mation is provided by instances of classes DimensionObject and ScaleObject .
These classes are discussed later.
As an example, the package has three graphical objects that are implemented,
Circle , Line and OpenPolygon . The listing of OpenPolygon is shown below. The
constructor receives the corner points of the polygon as an array. It is checked
whether the array contains at least two points. If this is the case, the array is
traversed to find the extremal coordinates. A DimensionObject with these coor-
dinates is created. The reference point for the object - the anchor -isdefined to
be the first point.
The draw method receives a reference to a Graphics object and a ScaleOb-
ject . The latter contains the information on the current size of the display. This
information is used to transfer the abstract real coordinates of the polygon into
screen (pixel) coordinates. Conversion routines are provided by methods of the
utility class Conversions . The pixel coordinates are then used to draw the polygon
as a sequence of line segments. Class OpenPolygon provides a method textString
which returns a textual description of the polygon.
File: its/GenericDraw/OpenPolygon.java
1.
package its.GenericDraw;
2.
3.
import java.awt.Graphics;
4.
5.
public class OpenPolygon extends GraphicalObject {
6.
7.
private RealPoint[] points;
8.
9.
public OpenPolygon(RealPoint[] pts) {
points = pts;
10.
if (points.length < 2){
11.
System.out.println("ERROR in OpenPolygon: less than two points.");
12.
}
13.
else
14.
{
15.
double xmin = points[0].getX();
16.
double xmax = points[0].getX();
17.
Search WWH ::




Custom Search