Example usage for javax.swing.text JTextComponent getCaretColor

List of usage examples for javax.swing.text JTextComponent getCaretColor

Introduction

In this page you can find the example usage for javax.swing.text JTextComponent getCaretColor.

Prototype

public Color getCaretColor() 

Source Link

Document

Fetches the current color used to render the caret.

Usage

From source file:FancyCaret.java

public void paint(Graphics g) {
    JTextComponent comp = getComponent();
    if (comp == null)
        return;/*from  ww  w .ja v  a  2  s . co m*/

    int dot = getDot();
    Rectangle r = null;
    char dotChar;
    try {
        r = comp.modelToView(dot);
        if (r == null)
            return;
        dotChar = comp.getText(dot, 1).charAt(0);
    } catch (BadLocationException e) {
        return;
    }

    if ((x != r.x) || (y != r.y)) {
        repaint();
        x = r.x;
        y = r.y;
        height = r.height;
    }

    g.setColor(comp.getCaretColor());
    g.setXORMode(comp.getBackground());

    if (dotChar == '\n') {
        int diam = r.height;
        if (isVisible())
            g.fillArc(r.x - diam / 2, r.y, diam, diam, 270, 180); // half circle
        width = diam / 2 + 2;
        return;
    }

    if (dotChar == '\t')
        try {
            Rectangle nextr = comp.modelToView(dot + 1);
            if ((r.y == nextr.y) && (r.x < nextr.x)) {
                width = nextr.x - r.x;
                if (isVisible())
                    g.fillRoundRect(r.x, r.y, width, r.height, 12, 12);
                return;
            } else
                dotChar = ' ';
        } catch (BadLocationException e) {
            dotChar = ' ';
        }

    width = g.getFontMetrics().charWidth(dotChar);
    if (isVisible())
        g.fillRect(r.x, r.y, width, r.height);
}

From source file:CornerCaret.java

public void paint(Graphics g) {
    JTextComponent comp = getComponent();
    if (comp == null)
        return;//w w w. j  a  va 2 s . co  m

    int dot = getDot();
    Rectangle r = null;
    try {
        r = comp.modelToView(dot);
    } catch (BadLocationException e) {
        return;
    }
    if (r == null)
        return;

    int dist = r.height * 4 / 5 - 3; // will be distance from r.y to top

    if ((x != r.x) || (y != r.y + dist)) {
        // paint() has been called directly, without a previous call to
        // damage(), so do some cleanup. (This happens, for example, when
        // the
        // text component is resized.)
        repaint(); // erase previous location of caret
        x = r.x; // set new values for x,y,width,height
        y = r.y + dist;
        width = 5;
        height = 5;
    }

    if (isVisible()) {
        g.setColor(comp.getCaretColor());
        g.drawLine(r.x, r.y + dist, r.x, r.y + dist + 4); // 5 vertical
        // pixels
        g.drawLine(r.x, r.y + dist + 4, r.x + 4, r.y + dist + 4); // 5 horiz
        // px
    }
}