Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-25-2008, 11:32 AM
Moderator
 
Join Date: Nov 2007
Posts: 1,637
Java Tip will become famous soon enoughJava Tip will become famous soon enough
How to get mouse hit position in a textlayout using TextHitInfo class
Code:
import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Shape; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.font.FontRenderContext; import java.awt.font.TextAttribute; import java.awt.font.TextHitInfo; import java.awt.font.TextLayout; import java.awt.geom.Point2D; import java.text.AttributedCharacterIterator; import java.text.AttributedString; import java.util.Hashtable; import javax.swing.JFrame; import javax.swing.JPanel; public class HitTestSample extends JPanel { private static final Color STRONG_CARET_COLOR = Color.red; private static final Color WEAK_CARET_COLOR = Color.black; private TextLayout textLayout; private int insertionIndex; private static final FontRenderContext DEFAULT_FRC = new FontRenderContext( null, false, false); private static final Hashtable map = new Hashtable(); static { map.put(TextAttribute.SIZE, new Float(18.0)); } private static AttributedString helloWorld = new AttributedString( "Java Source and Support.", map); public HitTestSample() { AttributedCharacterIterator text = helloWorld.getIterator(); // Create a new TextLayout from the given text. textLayout = new TextLayout(text, DEFAULT_FRC); // Initilize insertionIndex. insertionIndex = 0; addMouseListener(new HitTestMouseListener()); } public Dimension getPreferredSize() { return new Dimension(400, 250); } private Point2D computeLayoutOrigin() { 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 void paintComponent(Graphics g) { super.paintComponent(g); setBackground(Color.white); Graphics2D graphics2D = (Graphics2D) g; Point2D origin = computeLayoutOrigin(); graphics2D.translate(origin.getX(), origin.getY()); // Draw textLayout. textLayout.draw(graphics2D, 0, 0); // Retrieve caret Shapes for insertionIndex. Shape[] carets = textLayout.getCaretShapes(insertionIndex); graphics2D.setColor(STRONG_CARET_COLOR); graphics2D.draw(carets[0]); if (carets[1] != null) { graphics2D.setColor(WEAK_CARET_COLOR); graphics2D.draw(carets[1]); } } private class HitTestMouseListener extends MouseAdapter { public void mouseClicked(MouseEvent e) { Point2D origin = computeLayoutOrigin(); // Compute the mouse click location relative to // textLayout's origin. float clickX = (float) (e.getX() - origin.getX()); float clickY = (float) (e.getY() - origin.getY()); // Get the character position of the mouse click. TextHitInfo currentHit = textLayout.hitTestChar(clickX, clickY); insertionIndex = currentHit.getInsertionIndex(); // Repaint the Component so the new caret(s) will be displayed. repaint(); } } public static void main(String[] args) { JFrame f = new JFrame("HitTestSample"); HitTestSample demo = new HitTestSample(); f.getContentPane().add(demo, "Center"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); f.setSize(new Dimension(400, 250)); f.setVisible(true); } }
__________________
Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums! (closes on July 27, 2008)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use TextLayout class Java Tip java.awt 0 06-25-2008 11:31 AM
BorderFactory to set position? aleplgr AWT / Swing 2 01-30-2008 01:46 PM
How to get the position of character in TextArea loganathan.lakshmanan JavaServer Pages (JSP) and JSTL 1 01-19-2008 01:06 AM
Help with position in java fernando New To Java 1 07-31-2007 08:54 PM
Use the mouse position susan Java Applets 1 07-29-2007 12:10 AM


All times are GMT +3. The time now is 06:57 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org