XORCaretpublic class XORCaret extends DefaultCaret
Fields Summary |
---|
protected int | lastPaintedWidth | protected Font | currentFont | protected int | currentAscent | protected FontMetrics | currentFontMetrics |
Constructors Summary |
---|
public XORCaret()
setBlinkRate(500); // blinking cursor
|
Methods Summary |
---|
protected int | currentWidth()
Font f = getComponent().getFont();
if (f != currentFont) {
currentFontMetrics = Toolkit.getDefaultToolkit().getFontMetrics(f);
currentAscent = currentFontMetrics.getAscent();
}
// Determine the character at the current caret position
String current = null;
try {
current = getComponent().getText(getDot(), 1);
} catch (BadLocationException ex) { /* ignore */ }
char currentChar;
if (current != null && current.length() > 0)
currentChar = current.charAt(0);
else
currentChar = ' ";
// Return the character's size, or the size of a space when we're adding
// new text
if (Character.isWhitespace(currentChar))
return currentFontMetrics.charWidth(' ");
else
return currentFontMetrics.charWidth(current.charAt(0));
| public void | damage(java.awt.Rectangle r)
if (r != null) {
// Repaint the area where the cursor was. We don't use the current
// width here, instead we use the width of the last caret we painted.
getComponent().repaint(r.x, r.y, lastPaintedWidth, r.height);
}
| public void | paint(java.awt.Graphics g)
if (isVisible()) {
// Determine where to draw the cursor
JTextComponent c = getComponent();
TextUI ui = c.getUI();
Rectangle r = null;
int dot = getDot();
try {
r = ui.modelToView(c, dot);
}
catch (BadLocationException ex) { return; }
g.setColor(c.getCaretColor());
// Draw the rectangle, using the currentWidth() method for the width
lastPaintedWidth = currentWidth();
g.fillRect(r.x, r.y, lastPaintedWidth, r.height);
// Draw the character with the background color so it shows up!
g.setColor(c.getBackground());
try {
if (dot < c.getDocument().getLength()) {
String s = getComponent().getText(dot, 1);
// Keep \t and \n from looking funny. . .
if (Character.isISOControl(s.charAt(0)) == false)
g.drawString(s, r.x, r.y+currentAscent);
}
}
catch (BadLocationException ex) { }
}
|
|