}
@Override
public void paint(Graphics2D graphics) {
if (glyphVector != null) {
TextArea textArea = (TextArea)getComponent();
if (FONT_RENDER_CONTEXT.isAntiAliased()) {
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
Platform.getTextAntialiasingHint());
}
if (FONT_RENDER_CONTEXT.usesFractionalMetrics()) {
graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
}
LineMetrics lm = font.getLineMetrics("", FONT_RENDER_CONTEXT);
float ascent = lm.getAscent();
graphics.setFont(font);
int selectionStart = textArea.getSelectionStart();
int selectionLength = textArea.getSelectionLength();
Span selectionRange = new Span(selectionStart, selectionStart + selectionLength - 1);
int documentOffset = getDocumentOffset();
Span characterRange = new Span(documentOffset, documentOffset + getCharacterCount() - 1);
if (selectionLength > 0
&& characterRange.intersects(selectionRange)) {
// Determine the selection bounds
int width = getWidth();
int height = getHeight();
int x0;
if (selectionRange.start > characterRange.start) {
Bounds leadingSelectionBounds = getCharacterBounds(selectionRange.start - documentOffset);
x0 = leadingSelectionBounds.x;
} else {
x0 = 0;
}
int x1;
if (selectionRange.end < characterRange.end) {
Bounds trailingSelectionBounds = getCharacterBounds(selectionRange.end - documentOffset);
x1 = trailingSelectionBounds.x + trailingSelectionBounds.width;
} else {
x1 = width;
}
Rectangle selection = new Rectangle(x0, 0, x1 - x0, height);
// Paint the unselected text
Area unselectedArea = new Area();
unselectedArea.add(new Area(new Rectangle(0, 0, width, height)));
unselectedArea.subtract(new Area(selection));
Graphics2D textGraphics = (Graphics2D)graphics.create();
textGraphics.setColor(color);
textGraphics.clip(unselectedArea);
textGraphics.drawGlyphVector(glyphVector, 0, ascent);
textGraphics.dispose();
// Paint the selection
Color selectionColor;
if (textArea.isFocused()) {
selectionColor = TextAreaSkin.this.selectionColor;
} else {
selectionColor = inactiveSelectionColor;
}
Graphics2D selectedTextGraphics = (Graphics2D)graphics.create();
selectedTextGraphics.setColor(textArea.isFocused() &&
textArea.isEditable() ? selectionColor : inactiveSelectionColor);
selectedTextGraphics.clip(selection.getBounds());
selectedTextGraphics.drawGlyphVector(glyphVector, 0, ascent);
selectedTextGraphics.dispose();
} else {
// Draw the text