/* * @(#)Highlighting.java 1.4 98/12/03 * * 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. */ 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 java.awt.image.BufferedImage; import javax.swing.*; /** * The Highlighting class demonstrates highlighting of text showing the * caret, the highlight and the character advances. */ public class Highlighting extends JApplet implements Runnable { private static String text[] = { "HILIGHTING", "Java2D" }; private static Color colors[] = { Color.cyan, Color.lightGray }; private static Font smallF = new Font("Courier", Font.PLAIN, 8); private int[] curPos; private TextLayout[] layouts; private Font[] fonts; private long sleepAmount = 900; private Thread thread; private BufferedImage bimg; public void init() { setBackground(Color.white); fonts = new Font[2]; layouts = new TextLayout[fonts.length]; curPos = new int[fonts.length]; } /* * resets the caret position to 0 and resets the Font objects * based on the length of the text and the width of the surface */ public void reset(int w, int h) { fonts[0] = new Font("Courier",Font.PLAIN,w/text[0].length()+8); fonts[1] = new Font("serif", Font.BOLD,w/text[1].length()); for (int i = 0; i < layouts.length; i++ ) { curPos[i] = 0; } } // advances the current position of the caret public void step(int w, int h) { for (int i = 0; i < 2; i++) { if (layouts[i] == null) { continue; } if (curPos[i]++ == layouts[i].getCharacterCount()) { curPos[i] = 0; } } } public void drawDemo(int w, int h, Graphics2D g2) { FontRenderContext frc = g2.getFontRenderContext(); for (int i = 0; i < 2; 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()); // fills highlighted shape Shape hilite = layouts[i].getLogicalHighlightShape(0, curPos[i]); AffineTransform at = AffineTransform.getTranslateInstance(rx, ry); hilite = at.createTransformedShape(hilite); float hy = (float) hilite.getBounds().getY(); float hh = (float) hilite.getBounds().getHeight(); g2.setColor(colors[i]); g2.fill(hilite); // gets caret shape Shape[] shapes = layouts[i].getCaretShapes(curPos[i]); Shape caret = at.createTransformedShape(shapes[0]); // renders the strings and the outline rectangles g2.setColor(Color.black); layouts[i].draw(g2, rx, ry); g2.draw(caret); g2.draw(new Rectangle2D.Float(rx,hy,rw,hh)); // displays character advances. for (int j = 0; j <= layouts[i].getCharacterCount(); j++) { float[] cInfo = layouts[i].getCaretInfo(TextHitInfo.leading(j)); String str = String.valueOf((int) cInfo[0]); TextLayout tl = new TextLayout(str,smallF,frc); tl.draw(g2, (float) rx+cInfo[0], hy+hh+tl.getAscent()+1.0f); } } } public Graphics2D createGraphics2D(int w, int h) { Graphics2D g2 = null; if (bimg == null || bimg.getWidth() != w || bimg.getHeight() != h) { bimg = (BufferedImage) createImage(w, h); reset(w, h); } g2 = bimg.createGraphics(); g2.setBackground(getBackground()); g2.clearRect(0, 0, w, h); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); return g2; } public void paint(Graphics g) { Dimension d = getSize(); step(d.width, d.height); Graphics2D g2 = createGraphics2D(d.width, d.height); drawDemo(d.width, d.height, g2); g2.dispose(); g.drawImage(bimg, 0, 0, this); } public void start() { thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.start(); } public synchronized void stop() { thread = null; } public void run() { try { thread.sleep(3333); } catch (InterruptedException e) { return; } Thread me = Thread.currentThread(); while (thread == me) { repaint(); try { thread.sleep(sleepAmount); } catch (InterruptedException e) { break; } } thread = null; } public static void main(String argv[]) { final Highlighting demo = new Highlighting(); demo.init(); JFrame f = new JFrame("Java 2D(TM) Demo - Highlighting"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} public void windowDeiconified(WindowEvent e) { demo.start(); } public void windowIconified(WindowEvent e) { demo.stop(); } }); f.getContentPane().add("Center", demo); f.pack(); f.setSize(new Dimension(400,300)); f.show(); demo.start(); } }