java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4089
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to implement your own line highlighter E-mail
User Rating: / 6
PoorBest 

This Java tip illustrates a method of implementing a line highlighter. This code implements HighlightPainter that underlines text with a thick line. Developers may use this code in their wordprocessing applications.


Image

import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;

public class LineHighlightPainter implements Highlighter.HighlightPainter {

  // paint a thick line under one line of text, from r extending rightward to x2
  private void paintLine(Graphics g, Rectangle r, int x2) {
    int ytop = r.y + r.height - 3;
    g.fillRect(r.x, ytop, x2 - r.x, 3);
  }

  // paint thick lines under a block of text
  public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c) {

    Rectangle r0 = null, r1 = null, rbounds = bounds.getBounds();
    int xmax = rbounds.x + rbounds.width; // x coordinate of right edge
    try {  // convert positions to pixel coordinates
      r0 = c.modelToView(p0);
      r1 = c.modelToView(p1);
    catch (BadLocationException ex) { return}
    if ((r0 == null|| (r1 == null)) return;

    g.setColorc.getSelectionColor() );

    // special case if p0 and p1 are on the same line
    if (r0.y == r1.y) {
      paintLine(g, r0, r1.x);
      return;
    }

    // first line, from p1 to end-of-line
    paintLine(g, r0, xmax);

    // all the full lines in between, if any (assumes that all lines have
    // the same height--not a good assumption with JEditorPane/JTextPane)
    r0.y += r0.height; // move r0 to next line 
    r0.x = rbounds.x; // move r0 to left edge
    while (r0.y < r1.y) {
      paintLine(g, r0, xmax);
      r0.y += r0.height; // move r0 to next line
    }

    // last line, from beginning-of-line to p1
    paintLine(g, r0, r1.x);
  }

  public static void main(String args[]) {

    // extend DefaultCaret as an anonymous inner class
    Caret lineHighlightPainterCaret = new DefaultCaret() {
      private Highlighter.HighlightPainter lhp = new LineHighlightPainter();
      // override getSelectionPainter to return the LineHighlightPainter
      protected Highlighter.HighlightPainter getSelectionPainter() {
        return lhp;
      }
    };

    JFrame frame = new JFrame("LineHighlightPainter demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextArea area = new JTextArea(945);
    area.setCaret(lineHighlightPainterCaret);
    area.setLineWrap(true);
    area.setWrapStyleWord(true);
    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);
  }
}

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.