dimanche 3 mai 2015

Cant see Overwrinting Cursor when i create new Line

I'm trying to create an overwriting Cursor. I've got it except when I click an earlier line the caret disappears, then when I hit 'enter' for a new line it appears again.

what should I change in my Code to solve this issue?

here is my Caret Class:

public class Mycaret extends DefaultCaret {

    protected static final int MIN_WIDTH = 8;

    protected DefaultCaret dc = null;

    JTextComponent com = null;

    public Mycaret(int rate, DefaultCaret dc) {

        this.dc = dc;
        super.setBlinkRate(rate);
    }

    protected synchronized void damage(Rectangle r) {

        if (r != null) {

            try {

                JTextComponent comp = getComponent();
                TextUI mapper = comp.getUI();
                char dotChar = 0;
                if(comp.getText().length()>0){
                 dotChar = comp.getText().charAt(comp.getText().length()-1);
                }
                this.com = comp;

                Rectangle r2 = mapper.modelToView(comp, getDot() + 1);

                int width = r2.x - r.x;

                if (width == 0 ) {

                    width = MIN_WIDTH;


                }

                comp.repaint(r.x, r.y, width, r.height);

                this.x = r.x;
                this.y = r.y;
                this.width = width;
                this.height = r.height;

            }

            catch (BadLocationException e) {

            }
        }

    }

    public void paint(Graphics g) {

        char dotChar;

        if (isVisible()) {

            try {

                JTextComponent comp = getComponent();
                TextUI mapper = comp.getUI();

                Rectangle r1 = mapper.modelToView(comp, getDot());
                Rectangle r2 = mapper.modelToView(comp, getDot() + 1);

                g = g.create();
                g.setColor(comp.getForeground());
                g.setXORMode(comp.getBackground());

                int width = r2.x - r1.x;

                dotChar = comp.getText(getDot(), 1).charAt(0);

                if (width == 0  ) {
                    width = MIN_WIDTH;

                }



                g.fillRect(r1.x, r1.y, width, r1.height);
                g.dispose();

            } catch (BadLocationException e) {

            }
        }

    }
}

this is a Sample:

public class MyFrameSample extends JFrame {

    DefaultCaret caret=null;

    public MyFrameSample() {

        JTextArea text = new JTextArea(10,20);
        caret = new DefaultCaret();

        text.setCaret(new Mycaret(500, caret));
        add(text);

        pack();
        setVisible(true);
    }

    public static void main(String[] args) {

        new MyFrameSample();
    }
}

Aucun commentaire:

Enregistrer un commentaire