Results 1 to 4 of 4

Thread: caret offset?

  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default caret offset?

    I was going over this basic caret-code (orielly swing book)
    Everything is pretty clear to me.
    Just one thing I wonder about:
    int dist = r.height * 4 / 5 - 3; ( y = r.y + (r.height * 4 / 5 - 3);)

    I understand that one needs an y-offset from the top,
    but why this height * 4 / 5 - 3

    I also played with the font size and noticed that the caret
    stays always perfectly under the letter base (line 59)!
    How come?






    Java Code:
    import javax.swing.*;
    import javax.swing.text.*;
    import java.awt.*;
    
    public class CornerCaret extends DefaultCaret {
    
      public CornerCaret() {
        setBlinkRate(500); // half a second
       // Shape[] caretShapes = layout.getCaretShapes(hit);
      }
    
      protected synchronized void damage(Rectangle r) {
        if (r == null) return;
        // give values to x,y,width,height (inherited from java.awt.Rectangle)
        x = r.x;
        y = r.y  + (r.height * 4 / 5 - 3);
        width = 5;
        height = 5;
        repaint(); // calls getComponent().repaint(x, y, width, height)
      }
    
      public void paint(Graphics g) {
        JTextComponent comp = getComponent();
        if (comp == null) return;
    
        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
        }
      }
    
      public static void main(String args[]) {
        JFrame frame = new JFrame("CornerCaret demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTextArea area = new JTextArea(8, 32);
        area.setCaret(new CornerCaret());
    
        // here I changed the fonts size to see what happens
        // to the caret position
        Font font = area.getFont();
        String family = font.getFamily();
        font = new Font(family, Font.PLAIN, 48);
        area.setFont(font);
    
        area.setText("This is the story\nof the hare who\nlost his spectacles.");
        frame.getContentPane().add(new JScrollPane(area), BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
      }
    }

  2. #2
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: caret offset?

    Anyway what would be the math order again:
    1) r.height * 4
    2) /5
    3) -3
    right?

  3. #3
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: caret offset?

    I am looking for an effective Java course concerning web applications.
    Please would you comment if this might be any good concerning the level I am at:

    :: PUE - Proyecto Universidad Empresa :: FormaciĆ³n :: InformaciĆ³n de curso ::

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,783
    Rep Power
    5

    Default Re: caret offset?

    Quote Originally Posted by willemjav View Post
    I understand that one needs an y-offset from the top,
    but why this height * 4 / 5 - 3
    From the source for DefaultCaret

    Java Code:
    /**
         * 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
         */
    Not certain why those values were chosen since I haven't worked with DefaultCaret. Have you tried changing them to see how it affects the damage area? You could also write some ancillary code to draw a visible rectangle over the area to see how it changes. If I want to figure out how or why something is done, I usually write extra code to "highlight" the affected area.

    Also, the method shown has been overridden and re-written from whats in the JDK.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. tiny offset in calculation
    By pie_man in forum Advanced Java
    Replies: 4
    Last Post: 07-22-2011, 05:50 PM
  2. wunderground.com API GMT offset
    By SteveG in forum New To Java
    Replies: 2
    Last Post: 07-13-2011, 08:03 PM
  3. Creating a caret in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 07:52 PM
  4. Creating a caret (using an image) in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 07:51 PM
  5. How to use TextLayout with Caret
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-25-2008, 10:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •