java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow javax.swing arrow How to implement a fancy caret for your application
 
 
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: 4090
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 a fancy caret for your application E-mail
User Rating: / 7
PoorBest 

This Java Swing tip illustrates a method of implementing a fancy caret for your application. Developers may use this in gaming applications to provide animated caret to users.


Image

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

public class FancyCaret extends DefaultCaret {

  protected synchronized void damage(Rectangle r) {
    if (r == nullreturn;

    // give values to x,y,width,height (inherited from java.awt.Rectangle)
    x = r.x; 
    y = r.y;
    height = r.height;
    // A value for width was probably set by paint(), which we leave alone.
    // But the first call to damage() precedes the first call to paint(), so
    // in this case we must be prepared to set a valid width, or else paint()
    // will receive a bogus clip area and caret will not get drawn properly.
    if (width <= 0width = getComponent().getWidth();

    repaint()// calls getComponent().repaint(x, y, width, height)
  }

  public void paint(Graphics g) {
    JTextComponent comp = getComponent();
    if (comp == nullreturn;

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

    if ( (x != r.x|| (y != r.y) ) {
      // 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;   // Update dimensions (width gets set later in this method)
      y = r.y;
      height = r.height;
    }

    g.setColor(comp.getCaretColor());
    g.setXORMode(comp.getBackground())// do this to draw in XOR mode

    if (dotChar == '\n') {
      int diam = r.height;
      if (isVisible())
        g.fillArc(r.x-diam/2, r.y, diam, diam, 270180)// half circle
      width = diam / 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, 1212);
        return;
      }
      else dotChar = ' ';
    catch (BadLocationException e) { dotChar = ' '}

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

  public static void main(String args[]) {
    JFrame frame = new JFrame("FancyCaret demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextArea area = new JTextArea(832);
    area.setCaret(new FancyCaret());
    area.setText("VI\tVirgin Islands \nVA      Virginia\nVT\tVermont");
    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.