FileDocCategorySizeDatePackage
HitTestSample.javaAPI DocExample5787Tue Dec 12 19:00:00 GMT 2000None

HitTestSample

public class HitTestSample extends JApplet
This class demonstrates how to hit-test a TextLayout. Hit-testing is the mapping of a graphical location to a character position within text. This class constructs a TextLayout from an AttributedCharacterIterator and displays the TextLayout. When the mouse is clicked inside this Component, the mouse position is mapped to a character position, and the carets for this character position are displayed.

Fields Summary
private static final Color
STRONG_CARET_COLOR
private static final Color
WEAK_CARET_COLOR
private TextLayout
textLayout
private int
insertionIndex
private static final FontRenderContext
DEFAULT_FRC
private static final Hashtable
map
private static AttributedString
helloWorld
Dimension
preferredSize
HitPane
hitPane
Constructors Summary
Methods Summary
public voidbuildUI(java.awt.Container container, java.text.AttributedCharacterIterator text)



        // Create a new TextLayout from the given text.
        textLayout = new TextLayout(text, DEFAULT_FRC);

        // Initilize insertionIndex.
        insertionIndex = 0;

	hitPane = new HitPane();
        container.add(hitPane, BorderLayout.CENTER);
        hitPane.addMouseListener(new HitTestMouseListener());
    
private java.awt.geom.Point2DcomputeLayoutOrigin()
Compute a location within this Component for textLayout's origin, such that textLayout is centered horizontally and vertically. Note that this location is unknown to textLayout; it is used only by this Component for positioning.


        Dimension size = getPreferredSize();
        Point2D.Float origin = new Point2D.Float();

        origin.x = (float) (size.width - textLayout.getAdvance()) / 2;
        origin.y = (float) (size.height - textLayout.getDescent() + textLayout.getAscent()) / 2;
        return origin;
    
public java.awt.DimensiongetPreferredSize()

        return preferredSize;
    
public voidinit()


       
	
	AttributedCharacterIterator text = helloWorld.getIterator();
	buildUI(getContentPane(), text);
    
public static voidmain(java.lang.String[] args)


        JFrame sampleFrame = new JFrame("HitTestSample");
	AttributedCharacterIterator text = helloWorld.getIterator();
	final HitTestSample controller = new HitTestSample();
        controller.buildUI(sampleFrame.getContentPane(), text);

        sampleFrame.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });

        sampleFrame.setSize(new Dimension(400, 250));                                            
        sampleFrame.setVisible(true);