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:34 AM
Moderator
 
Join Date: Nov 2007
Posts: 1,657
Java Tip will become famous soon enoughJava Tip will become famous soon enough
How to use TextHitInfo class to find the letter you are clicking
Code:
import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.font.FontRenderContext; import java.awt.font.TextHitInfo; import java.awt.font.TextLayout; import javax.swing.JFrame; import javax.swing.JPanel; public class TextHitInfoDemo extends JPanel { private TextLayout mTextLayout; private int mX = 40, mY = 80; public TextHitInfoDemo() { addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent me) { TextHitInfo hit = mTextLayout.hitTestChar(me.getX() - mX, me .getY() - mY); System.out.println(hit); } }); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); String s = "Java Source and Support"; Font font = new Font("Serif", Font.PLAIN, 32); if (mTextLayout == null) { FontRenderContext frc = g2.getFontRenderContext(); mTextLayout = new TextLayout(s, font, frc); } mTextLayout.draw(g2, mX, mY); } public static void main(String[] args) { JFrame f = new JFrame(); f.getContentPane().add(new TextHitInfoDemo()); f.setSize(200, 200); f.show(); } }
__________________
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
Could not find the main class, program will exit. aryubi New To Java 13 09-30-2008 12:11 AM
How to get mouse hit position in a textlayout using TextHitInfo class Java Tip java.awt 0 06-25-2008 11:32 AM
JVM Could not find main class banduskank Advanced Java 2 06-24-2008 09:05 AM
Able to find class file in WEB-INF/classes but not after add sub folders in class dir vitalstrike82 Web Frameworks 0 05-13-2008 07:16 AM
cannot find symbol class error po0oker New To Java 5 10-31-2007 03:52 PM


All times are GMT +3. The time now is 12:32 PM.


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