Caret 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 
Caret and TextLayout
Caret and TextLayout
     

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
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.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.font.FontRenderContext;
import java.awt.font.TextHitInfo;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class TextLayoutPanel extends JPanel {
  TextLayout layout;

  FontRenderContext frc;

  Font font;

  Rectangle2D rect;

  float rx, ry, rw, rh;

  TextHitInfo hitInfo;

  Color caretColor;

  int hit1, hit2;

  int w, h;

  public TextLayoutPanel() {
    setBackground(Color.white);
    setForeground(Color.black);
    setSize(400200);
    addMouseListener(new MouseHandler());
    addMouseMotionListener(new MouseMotionHandler());
    w = getWidth();
    h = getHeight();

    String text = "Java Source and Support";
    font = new Font("Arial", Font.PLAIN, 36);
    frc = new FontRenderContext(null, false, false);
    layout = new TextLayout(text, font, frc);

    rx = (float) (w / - layout.getBounds().getWidth() 2);
    ry = (float* h / 4;
    rw = (float) (layout.getBounds().getWidth());
    rh = (float) (layout.getBounds().getHeight());
    rect = new Rectangle2D.Float(rx, ry, rw, rh);

    caretColor = getForeground();
  }

  public void update(Graphics g) {
    g.clearRect(00, getWidth(), getHeight());
    paintComponent(g);
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2Dg;
    AffineTransform at = AffineTransform.getTranslateInstance(rx, ry);

    Shape hilight = layout.getLogicalHighlightShape(hit1, hit2);
    hilight = at.createTransformedShape(hilight);
    g2.setColor(Color.lightGray);
    g2.fill(hilight);

    g2.setColor(Color.black);
    layout.draw(g2, rx, ry);

    // Draw caret
    Shape[] caretShapes = layout.getCaretShapes(hit1);
    Shape caret = at.createTransformedShape(caretShapes[0]);
    g2.setColor(caretColor);
    g2.draw(caret);
  }

  public int getHitLocation(int mouseX, int mouseY) {
    hitInfo = layout.hitTestChar(mouseX, mouseY, rect);
    return hitInfo.getInsertionIndex();
  }

  class MouseHandler extends MouseAdapter {
    public void mouseClicked(MouseEvent e) {
      caretColor = getForeground();
      hit1 = getHitLocation(e.getX(), e.getY());
      hit2 = hit1;
      repaint();
    }

    public void mousePressed(MouseEvent e) {
      caretColor = getForeground();
      hit1 = getHitLocation(e.getX(), e.getY());
      hit2 = hit1;
      repaint();
    }

    public void mouseReleased(MouseEvent e) {
      hit2 = getHitLocation(e.getX(), e.getY());
      repaint();
    }
  }

  class MouseMotionHandler extends MouseMotionAdapter {
    public void mouseDragged(MouseEvent e) {
      caretColor = getBackground();
      hit2 = getHitLocation(e.getX(), e.getY());
      repaint();
    }
  }

  public static void main(String arg[]) {
    JFrame frame = new JFrame();
    frame.setBackground(Color.white);
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    frame.getContentPane().add("Center"new TextLayoutPanel());
    frame.pack();
    frame.setSize(new Dimension(400300));
    frame.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.Mouse hit and textlayoutMouse hit and textlayout
6.TextLayout demoTextLayout demo
7.Draw text along a curveDraw text along a curve
8.TextHitInfo Demo: tell you which is the letter you are clickingTextHitInfo Demo: tell you which is the letter you are clicking
9.TextAttribute: Underline and strike throughTextAttribute: Underline and strike through
10.TextAttribute: color and fontTextAttribute: color and font
11.Hightlight text by drag and selectionHightlight text by drag and selection
12.LineMetrics: the metrics to layout characters along a lineLineMetrics: the metrics to layout characters along a line
13.Paragraph LayoutParagraph Layout
14.Caret actionCaret action
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.