/* * @(#)Highlighting.java 1.14 98/06/29 * * Copyright 1998 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. */ package demos.Fonts; import java.awt.*; import java.awt.event.*; import java.awt.font.TextLayout; import java.awt.font.TextHitInfo; import java.awt.font.FontRenderContext; import java.awt.geom.Rectangle2D; import java.awt.geom.AffineTransform; import J2DCanvas; /** * Highlighting of text showing the caret, the highlight & the character * advances. */ public class Highlighting extends J2DCanvas { private int[] curPos; private TextLayout[] layouts; private Rectangle2D[] rects; private Color colors[] = { Color.cyan, Color.lightGray }; private Font[] fonts; private Font smallF = new Font("Courier", Font.PLAIN, 8); private FontRenderContext frc; public Highlighting() { setBackground(Color.white); fonts = new Font[2]; layouts = new TextLayout[fonts.length]; rects = new Rectangle2D[fonts.length]; curPos = new int[fonts.length]; sleepAmount = 900; runnable = true; } public void drawTextField(Graphics2D g2, TextLayout tl, Rectangle2D r, Color c, int i) { float rx = (float) r.getX(); float ry = (float) r.getY(); float rw = (float) r.getWidth(); float rh = (float) r.getHeight(); AffineTransform at = new AffineTransform(); at.setToTranslation(rx, ry); // draw highlighted shape Shape hilite = tl.getLogicalHighlightShape(0, curPos[i]); hilite = at.createTransformedShape(hilite); float hx = (float) hilite.getBounds().getX(); float hy = (float) hilite.getBounds().getY(); float hh = (float) hilite.getBounds().getHeight(); g2.setColor(c); g2.fill(hilite); // get caret shape Shape[] shapes = tl.getCaretShapes(curPos[i]); Shape caret = at.createTransformedShape(shapes[0]); g2.setColor(Color.black); tl.draw(g2, rx, ry); g2.draw(caret); g2.draw(new Rectangle2D.Float(hx,hy,rw,hh)); // Display character advances. for (TextHitInfo hit = TextHitInfo.trailing(-1); hit != null; hit = tl.getNextRightHit(hit)) { float[] cInfo = tl.getCaretInfo(hit); String str = String.valueOf((int) cInfo[0]); TextLayout t = new TextLayout(str,smallF,frc); t.draw(g2, (float) rx+cInfo[0], hy+hh+t.getAscent()+1.0f); } } public void drawDemo(Graphics2D g2) { if (newBufferedImage) { String text[] = { "HILIGHTING", "Java2D" }; fonts[0] = new Font("Courier",Font.PLAIN,w/text[0].length()+8); fonts[1] = new Font("serif", Font.BOLD+Font.ITALIC,w/text[1].length()); frc = g2.getFontRenderContext(); for (int i = 0; i < layouts.length; i++ ) { layouts[i] = new TextLayout(text[i], fonts[i], frc); float rx = (float) (w/2-layouts[i].getBounds().getWidth()/2); float ry = (float) ((i == 0) ? h/3 : h * 0.75f); float rw = (float) (layouts[i].getBounds().getWidth()); float rh = (float) (layouts[i].getBounds().getHeight()); rects[i] = new Rectangle2D.Float(rx, ry, rw, rh); curPos[i] = 0; } } for (int i = 0; i < 2; i++) { if (layouts[i] == null || rects[i] == null) continue; drawTextField(g2, layouts[i], rects[i], colors[i], i); if (curPos[i]++ == layouts[i].getCharacterCount()) curPos[i] = 0; } } public static void main(String argv[]) { final Highlighting demo = new Highlighting(); WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} public void windowDeiconified(WindowEvent e) { demo.start(); } public void windowIconified(WindowEvent e) { demo.stop(); } }; Frame f = new Frame("Java2D Demo - Highlighting"); f.addWindowListener(l); f.add("Center", demo); f.pack(); f.setSize(new Dimension(400,300)); f.show(); demo.start(); } }