afternoon folk,
i have an rtf document rendered in a JTextPane.  within the document are a 
couple of keywords that i want to have a visual highlight applied to (a line 
drawn just above them across the text area width.  i am trying to come up with 
the best solution for this, as the document is an editable document, and hence 
the content either before or after the keywords  will change, and the highlight 
must also change with it.  i have about gotten it by tracking changes to the 
document, and tracking the line (relative to the start of the document) that 
the keyword is located in.  my problem is two-fold.  one is i can't seem to get 
the proper location offsets for using the draw Graphics2D.draw() method 
invocation.   a code snippet of how i'm locating the offset in the text buffer, 
and then converting it to what i had hoped would be the proper coordinate space 
is as follows:
    int yMarker0 = 0;
    int yMarker1 = 0;
...

    DefaultStyledDocument doc = (DefaultStyledDocument) editor.getDocument();
    Style defaultStyle = doc.getStyle("default");
    Font f = new Font(StyleConstants.getFontFamily(defaultStyle),
        Font.PLAIN, StyleConstants.getFontSize(defaultStyle));
    FontMetrics fm = editor.getFontMetrics(f);       
    try {
      String[] nlCount = editor.getDocument().getText(0, 
editor.getDocument().getLength()).split("\n");
      for(int i = 0; i < nlCount.length; i += 1) {
        if(nlCount[i].contains("keyword0")) {
          /*
           * located the first keyword
           */
          yMarker0 = i * fm.getHeight();
          System.out.println("keyword0 on line " + i + " line height = " + 
fm.getHeight() + " data: " + nlCount[i]);
        }
        if(nlCount[i].contains("keyword1")) {
          /*
           * located the second keyword
           */
          yMarker1 = i * fm.getHeight();
          System.out.println("keyword1 on line " + i + " line height = " + 
fm.getHeight() + " data: " + nlCount[i]);
        } else {
          System.out.println("Skipping " + i + ":" + nlCount[i]);
        }
      }
       System.out.println("yMarker0 = " + yMarker0 + "   yMarker1 = " + 
yMarker1);
    } catch (BadLocationException e) {
      System.out.println(e.getMessage());
    }

then

    JEditorPane editor = new JEditorPane(){
      @Override
      protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setPaint(Color.red);
        g2.draw(new Line2D.Float(0, yMarker0, super.getWidth(), yMarker0));
        g2.setPaint(Color.green);
        g2.draw(new Line2D.Float(0, yMarker1, super.getWidth(), yMarker1));
      }
    };
 
when i hit the paintComponent(), the lines don't show up at the line the 
keywords are on, but rather much higher up (toward the top) in the document.  
the line numbers that are being displayed in the println calls are the correct 
line numbers though, and the font metrics seems to be correct.  this feels like 
i'm fixing to have one of those duh moments once i hear what i'm doing wrong 
(or i happen to stumble across it).

the bigger problem is that i've spent about 12 hours searching and hunting for 
the proper way to accomplish this (the time spent isn't the problem), but i'm 
not sure i am really approaching it properly.  i am might weak in swing 
programming, and  i have wondered if i shouldn't be pursuing the swing 
highlighters route as a better approach, but even there, i'm not sure.

any help/suggestions would be appreciated as always.

thanks,
tom
[Message sent by forum member 'tjansto' (tjansto)]

http://forums.java.net/jive/thread.jspa?messageID=247691

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to