// TTextLayout2.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.font.*; import java.awt.geom.*; public class TTextLayout2 extends JFrame { public TTextLayout2(String title) { super(title); setBackground(Color.white); } public static void main(String arg[]) { TTextLayout2 frame = new TTextLayout2("TTextLayout2"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); frame.getContentPane().add("Center", new DisplayPanel()); frame.pack(); frame.setSize(new Dimension(400,300)); frame.show(); } } class DisplayPanel 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 DisplayPanel() { setBackground(Color.white); setForeground(Color.black); setSize(400, 200); addMouseListener(new MouseHandler()); addMouseMotionListener(new MouseMotionHandler()); w = getWidth(); h = getHeight(); String text = "This is Java2D text!"; font = new Font("Arial", Font.PLAIN, 36); frc = new FontRenderContext(null, false, false); layout = new TextLayout(text, font, frc); rx = (float) (w/2-layout.getBounds().getWidth()/2); ry = (float) 3*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(0, 0, getWidth(), getHeight()); paintComponent(g); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; // Create an instance of translated affine transform AffineTransform at = AffineTransform.getTranslateInstance(rx, ry); // First draw the highlighted shape Shape hilight = layout.getLogicalHighlightShape(hit1, hit2); hilight = at.createTransformedShape(hilight); g2.setColor(Color.lightGray); g2.fill(hilight); // Draw text, centering it horizontally in the frame 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(); } } }