KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java2d > demos > Fonts > Highlighting


1 /*
2  * @(#)Highlighting.java 1.31 06/08/29
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)Highlighting.java 1.31 06/08/29
39  */

40
41 package java2d.demos.Fonts;
42
43
44 import static java.awt.Color JavaDoc.*;
45 import java.awt.*;
46 import java.awt.event.*;
47 import java.awt.font.TextLayout JavaDoc;
48 import java.awt.font.TextHitInfo JavaDoc;
49 import java.awt.font.FontRenderContext JavaDoc;
50 import java.awt.geom.Rectangle2D JavaDoc;
51 import java.awt.geom.AffineTransform JavaDoc;
52 import java2d.AnimatingSurface;
53
54
55 /**
56  * Highlighting of text showing the caret, the highlight & the character
57  * advances.
58  */

59 public class Highlighting extends AnimatingSurface {
60
61     private static String JavaDoc text[] = { "HILIGHTING", "Java2D" };
62     private static Color JavaDoc colors[] = { CYAN, LIGHT_GRAY };
63     private static Font smallF = new Font("Monospaced", Font.PLAIN, 8);
64     private int[] curPos;
65     private TextLayout JavaDoc[] layouts;
66     private Font[] fonts;
67
68
69     public Highlighting() {
70         setBackground(WHITE);
71         fonts = new Font[2];
72         layouts = new TextLayout JavaDoc[fonts.length];
73         curPos = new int[fonts.length];
74     }
75
76
77     public void reset(int w, int h) {
78         fonts[0] = new Font("Monospaced",Font.PLAIN,w/text[0].length()+8);
79         fonts[1] = new Font("Serif", Font.BOLD,w/text[1].length());
80         for (int i = 0; i < layouts.length; i++ ) {
81             curPos[i] = 0;
82         }
83     }
84
85
86     public void step(int w, int h) {
87         setSleepAmount(900);
88         for (int i = 0; i < 2; i++) {
89             if (layouts[i] == null) {
90                 continue;
91             }
92             if (curPos[i]++ == layouts[i].getCharacterCount()) {
93                 curPos[i] = 0;
94             }
95         }
96     }
97
98
99     public void render(int w, int h, Graphics2D g2) {
100         FontRenderContext JavaDoc frc = g2.getFontRenderContext();
101         for (int i = 0; i < 2; i++) {
102             layouts[i] = new TextLayout JavaDoc(text[i], fonts[i], frc);
103             float rw = layouts[i].getAdvance();
104             float rh = layouts[i].getAscent() + layouts[i].getDescent();
105             float rx = (float) ((w - rw) /2);
106             float ry = (float) ((i == 0) ? h/3 : h * 0.75f);
107
108             // draw highlighted shape
109
Shape hilite = layouts[i].getLogicalHighlightShape(0, curPos[i]);
110             AffineTransform JavaDoc at = AffineTransform.getTranslateInstance(rx, ry);
111             hilite = at.createTransformedShape(hilite);
112             float hy = (float) hilite.getBounds2D().getY();
113             float hh = (float) hilite.getBounds2D().getHeight();
114             g2.setColor(colors[i]);
115             g2.fill(hilite);
116
117             // get caret shape
118
Shape[] shapes = layouts[i].getCaretShapes(curPos[i]);
119             Shape caret = at.createTransformedShape(shapes[0]);
120
121             g2.setColor(BLACK);
122             layouts[i].draw(g2, rx, ry);
123             g2.draw(caret);
124             g2.draw(new Rectangle2D.Float JavaDoc(rx,hy,rw,hh));
125
126             // Display character advances.
127
for (int j = 0; j <= layouts[i].getCharacterCount(); j++) {
128                 float[] cInfo = layouts[i].getCaretInfo(TextHitInfo.leading(j));
129                 String JavaDoc str = String.valueOf((int) cInfo[0]);
130                 TextLayout JavaDoc tl = new TextLayout JavaDoc(str,smallF,frc);
131                 tl.draw(g2, (float) rx+cInfo[0]-tl.getAdvance()/2, hy+hh+tl.getAscent()+1.0f);
132             }
133         }
134     }
135
136
137     public static void main(String JavaDoc argv[]) {
138         createDemoFrame(new Highlighting());
139     }
140 }
141
Popular Tags