Results 1 to 1 of 1
-
How to use TextHitInfo class to find the letter you are clicking
Java 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(); } }"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Similar Threads
-
Could not find the main class, program will exit.
By aryubi in forum New To JavaReplies: 39Last Post: 02-19-2010, 10:02 AM -
How to get mouse hit position in a textlayout using TextHitInfo class
By Java Tip in forum java.awtReplies: 0Last Post: 06-25-2008, 10:32 AM -
JVM Could not find main class
By banduskank in forum Advanced JavaReplies: 2Last Post: 06-24-2008, 08:05 AM -
Able to find class file in WEB-INF/classes but not after add sub folders in class dir
By vitalstrike82 in forum Web FrameworksReplies: 0Last Post: 05-13-2008, 06:16 AM -
cannot find symbol class error
By po0oker in forum New To JavaReplies: 5Last Post: 10-31-2007, 02:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks