home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Java2D / src / java2d / demos / Fonts / Highlighting.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  5.0 KB  |  139 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)Highlighting.java    1.26 02/06/13
  38.  */
  39.  
  40. package java2d.demos.Fonts;
  41.  
  42.  
  43. import java.awt.*;
  44. import java.awt.event.*;
  45. import java.awt.font.TextLayout;
  46. import java.awt.font.TextHitInfo;
  47. import java.awt.font.FontRenderContext;
  48. import java.awt.geom.Rectangle2D;
  49. import java.awt.geom.AffineTransform;
  50. import java2d.AnimatingSurface;
  51.  
  52.  
  53. /**
  54.  * Highlighting of text showing the caret, the highlight & the character
  55.  * advances.
  56.  */
  57. public class Highlighting extends AnimatingSurface {
  58.  
  59.     private static String text[] = { "HILIGHTING", "Java2D" };
  60.     private static Color colors[] = { Color.cyan, Color.lightGray };
  61.     private static Font smallF = new Font("Monospaced", Font.PLAIN, 8);
  62.     private int[] curPos;
  63.     private TextLayout[] layouts;
  64.     private Font[] fonts;
  65.  
  66.  
  67.     public Highlighting() {
  68.         setBackground(Color.white);
  69.         fonts = new Font[2];
  70.         layouts = new TextLayout[fonts.length];
  71.         curPos = new int[fonts.length];
  72.     }
  73.  
  74.  
  75.     public void reset(int w, int h) {
  76.         fonts[0] = new Font("Monospaced",Font.PLAIN,w/text[0].length()+8);
  77.         fonts[1] = new Font("Serif", Font.BOLD,w/text[1].length());
  78.         for (int i = 0; i < layouts.length; i++ ) {
  79.             curPos[i] = 0;
  80.         }
  81.     }
  82.  
  83.  
  84.     public void step(int w, int h) {
  85.         setSleepAmount(900);
  86.         for (int i = 0; i < 2; i++) {
  87.             if (layouts[i] == null) {
  88.                 continue;
  89.             }
  90.             if (curPos[i]++ == layouts[i].getCharacterCount()) {
  91.                 curPos[i] = 0;
  92.             }
  93.         }
  94.     }
  95.  
  96.  
  97.     public void render(int w, int h, Graphics2D g2) {
  98.         FontRenderContext frc = g2.getFontRenderContext();
  99.         for (int i = 0; i < 2; i++) {
  100.             layouts[i]  = new TextLayout(text[i], fonts[i], frc);
  101.             float rw = layouts[i].getAdvance();
  102.             float rh = layouts[i].getAscent() + layouts[i].getDescent();
  103.             float rx = (float) ((w - rw) /2);
  104.             float ry = (float) ((i == 0) ? h/3 : h * 0.75f);
  105.  
  106.             // draw highlighted shape
  107.             Shape hilite = layouts[i].getLogicalHighlightShape(0, curPos[i]);
  108.             AffineTransform at = AffineTransform.getTranslateInstance(rx, ry);
  109.             hilite = at.createTransformedShape(hilite);
  110.             float hy = (float) hilite.getBounds2D().getY();
  111.             float hh = (float) hilite.getBounds2D().getHeight();
  112.             g2.setColor(colors[i]);
  113.             g2.fill(hilite);
  114.  
  115.             // get caret shape
  116.             Shape[] shapes = layouts[i].getCaretShapes(curPos[i]);
  117.             Shape caret = at.createTransformedShape(shapes[0]);
  118.  
  119.             g2.setColor(Color.black);
  120.             layouts[i].draw(g2, rx, ry);
  121.             g2.draw(caret);
  122.             g2.draw(new Rectangle2D.Float(rx,hy,rw,hh));
  123.  
  124.             // Display character advances.
  125.             for (int j = 0; j <= layouts[i].getCharacterCount(); j++) {
  126.                 float[] cInfo = layouts[i].getCaretInfo(TextHitInfo.leading(j));
  127.                 String str = String.valueOf((int) cInfo[0]);
  128.                 TextLayout tl = new TextLayout(str,smallF,frc);
  129.                 tl.draw(g2, (float) rx+cInfo[0]-tl.getAdvance()/2, hy+hh+tl.getAscent()+1.0f);
  130.             }
  131.         }
  132.     }
  133.  
  134.  
  135.     public static void main(String argv[]) {
  136.         createDemoFrame(new Highlighting());
  137.     }
  138. }
  139.