Java Reference
In-Depth Information
18.
double ymin = points[0].getY();
19.
double ymax = points[0].getY();
20.
for ( int i=1;i<points.length; i++) {
21.
xmin = Math.min(xmin,points[i].getX());
22.
xmax = Math.max(xmax,points[i].getX());
23.
ymin = Math.min(ymin,points[i].getY());
24.
ymax = Math.max(ymax,points[i].getY());
25.
}
26.
dimObj = new DimensionObject(xmin,xmax,ymin,ymax);
27.
anchor = points[0];
28.
}
29.
}
30.
31.
public void draw(Graphics g, ScaleObject scale) {
32.
PixelPoint startPix = Conversions.realToPixelPoint(points[0],scale);
33.
g.setColor(color);
34.
for ( int i=0;i<points.length; i++) {
35.
PixelPoint endPix
= Conversions.realToPixelPoint(points[i],scale);
36.
g.drawLine(startPix.getX(),startPix.getY(),
endPix.getX(),endPix.getY());
37.
startPix = endPix;
38.
}
39. }
40.
41. public String textString(){
42.
String result = "Open Polygon: "+points[0].toString();
43.
for ( int i=1;i<points.length; i++) {
44.
result += ";"+points[i].toString();
45.
}
46.
return (result);
47. }
48. }
21.2.3
Class GenericDrawPanel
Class GenericDrawPanel is derived from JPanel and displays the drawing. A pro-
grammer who wants to use the GenericDrawing package has to know about this
class. GenericDrawPanel keeps the abstract representation of the drawing in an
instance of class Drawing . GenericDrawPanel also provides methods to add and
delete graphical objects. These methods only call the corresponding methods of
class Drawing . The paintComponent method of GenericDrawPanel calls the draw
method of class Drawing .
A mouse adapter is implemented as internal class GenericDrawMouseAdapter .
It supports two operations: if the right mouse button is clicked inside the panel,
then the graphical object whose anchor is closest to the mouse position is removed
from the drawing. If the left mouse button is clicked once, the mouse position is
Search WWH ::




Custom Search