Mouse hit and textlayout : Text Layout « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.2D Graphics GUI1
3.3D
4.Advanced Graphics
5.Ant
6.Apache Common
7.Chart
8.Class
9.Collections Data Structure
10.Data Type
11.Database SQL JDBC
12.Design Pattern
13.Development Class
14.EJB3
15.Email
16.Event
17.File Input Output
18.Game
19.Generics
20.GWT
21.Hibernate
22.I18N
23.J2EE
24.J2ME
25.JDK 6
26.JNDI LDAP
27.JPA
28.JSP
29.JSTL
30.Language Basics
31.Network Protocol
32.PDF RTF
33.Reflection
34.Regular Expressions
35.Scripting
36.Security
37.Servlets
38.Spring
39.Swing Components
40.Swing JFC
41.SWT JFace Eclipse
42.Threads
43.Tiny Application
44.Velocity
45.Web Services SOA
46.XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
SCJP
Java » 2D Graphics GUI » Text LayoutScreenshots 
Mouse hit and textlayout
Mouse hit and textlayout
     

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(400250);
  }

  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 = (Graphics2Dg;

    Point2D origin = computeLayoutOrigin();

    graphics2D.translate(origin.getX(), origin.getY());

    // Draw textLayout.
    textLayout.draw(graphics2D, 00);

    // 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(400250));
    f.setVisible(true);
  }
}


           
         
    
    
    
    
  
Related examples in the same category
1.JFreeChart: Draw String DemoJFreeChart: Draw String Demo
2.Unicode: test layoutUnicode: test layout
3.Unicode displayUnicode display
4.Line break for textlayoutLine break for textlayout
5.TextLayout demoTextLayout demo
6.Draw text along a curveDraw text along a curve
7.TextHitInfo Demo: tell you which is the letter you are clickingTextHitInfo Demo: tell you which is the letter you are clicking
8.TextAttribute: Underline and strike throughTextAttribute: Underline and strike through
9.TextAttribute: color and fontTextAttribute: color and font
10.Hightlight text by drag and selectionHightlight text by drag and selection
11.LineMetrics: the metrics to layout characters along a lineLineMetrics: the metrics to layout characters along a line
12.Paragraph LayoutParagraph Layout
13.Caret actionCaret action
14.Caret and TextLayoutCaret and TextLayout
15.Another Line Break Demo
16.A display of text, formatted by us instead of by AWT/SwingA display of text, formatted by us instead of by AWT/Swing
17.Draw text with TextLayout
18.Drawing a Paragraph of Text
19.Getting the Shape from the Outline of Text
20.Drawing Text with Mixed Styles
21.Drawing multi-line text with AttributedString and LineBreakMeasurer
22.Draws the string at the specified location underlining the specified character
23.Renders a paragraph of text (line breaks ignored) to an image (created and returned).
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.