import java.awt.geom.Rectangle2D; //导入方法依赖的package包/类
/**
* Damages the area surrounding the caret to cause
* it to be repainted in a new location. If paint()
* is reimplemented, this method should also be
* reimplemented. This method should update the
* caret bounds (x, y, width, and height).
*
* @param r the current location of the caret
* @see #paint
*/
@Override
protected synchronized void damage(final Rectangle r) {
if (r == null || fPainting) return;
x = r.x - 4;
y = r.y;
width = 10;
height = r.height;
// Don't damage the border area. We can't paint a partial border, so get the
// intersection of the caret rectangle and the component less the border, if any.
final Rectangle caretRect = new Rectangle(x, y, width, height);
final Border border = getComponent().getBorder();
if (border != null) {
final Rectangle alloc = getComponent().getBounds();
alloc.x = alloc.y = 0;
final Insets borderInsets = border.getBorderInsets(getComponent());
alloc.x += borderInsets.left;
alloc.y += borderInsets.top;
alloc.width -= borderInsets.left + borderInsets.right;
alloc.height -= borderInsets.top + borderInsets.bottom;
Rectangle2D.intersect(caretRect, alloc, caretRect);
}
x = caretRect.x;
y = caretRect.y;
width = Math.max(caretRect.width, 1);
height = Math.max(caretRect.height, 1);
repaint();
}