Java Reference
In-Depth Information
curve = new GeneralPath();
position = start;
curve.moveTo(origin.x, origin.y);
curve.lineTo(next.x - position.x,
next.y - position.y);
}
We store the start point in position , and create the curve starting at (0, 0). The end point has to be
adjusted so that it is defined relative to (0, 0). Adding a new segment in the modify() method also has
to be changed to take account of the new origin for the curve relative to the start point:
public void modify(Point start, Point next) {
curve.lineTo(next.x - start.x,
next.y - start.y);
}
We just subtract the coordinates of the original start point that we saved in position from the point,
next . The methods for drawing the curve and getting the bounding rectangle have already been
updated, so the last piece is the Element.Text class.
Translating Text
The first step is to remove the declaration for the member, position , from this class, as we will now
be using the member of the same name that is inherited from the base class.
The only changes we need to make to the constructor are as follows:
public Text(Font font, String text, Point position,
Color color, java.awt.Rectangle bounds) {
super(color);
this.font = font;
this.position = position;
this.position.y -= (int)bounds.getHeight();
this.text = text;
this.bounds = new java.awt.Rectangle(origin.x, origin.y,
bounds.width, bounds.height);
}
The bounding rectangle for the text object now has its top left corner at the origin. The point
position that defines where the text is to be drawn is set to correspond to the top-left corner of the
rectangle bounding the text, to be consistent with the way it is defined for the other elements. We will
need to take account of this in our implementation of the draw() method because the
drawString() method expects the position for the text to be the bottom-left corner:
public void draw(Graphics2D g2D) {
g2D.setPaint(highlighted ? Color.MAGENTA : color);
Font oldFont = g2D.getFont(); // Save the old font
g2D.setFont(font); // Set the new font
Search WWH ::




Custom Search