Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/demos/Fonts/Highlighting.java
41175 views
/*1*2* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/31package java2d.demos.Fonts;323334import static java.awt.Color.BLACK;35import static java.awt.Color.CYAN;36import static java.awt.Color.LIGHT_GRAY;37import static java.awt.Color.WHITE;38import java.awt.Color;39import java.awt.Font;40import java.awt.Graphics2D;41import java.awt.Shape;42import java.awt.font.FontRenderContext;43import java.awt.font.TextHitInfo;44import java.awt.font.TextLayout;45import java.awt.geom.AffineTransform;46import java.awt.geom.Rectangle2D;47import java2d.AnimatingSurface;484950/**51* Highlighting of text showing the caret, the highlight & the character52* advances.53*/54@SuppressWarnings("serial")55public class Highlighting extends AnimatingSurface {5657private static String[] text = { "HIGHLIGHTING", "OpenJDK" };58private static Color[] colors = { CYAN, LIGHT_GRAY };59private static Font smallF = new Font("Monospaced", Font.PLAIN, 8);60private int[] curPos;61private TextLayout[] layouts;62private Font[] fonts;6364public Highlighting() {65setBackground(WHITE);66fonts = new Font[2];67layouts = new TextLayout[fonts.length];68curPos = new int[fonts.length];69}7071@Override72public void reset(int w, int h) {73fonts[0] = new Font("Monospaced", Font.PLAIN, w / text[0].length() + 8);74fonts[1] = new Font("Serif", Font.BOLD, w / text[1].length());75for (int i = 0; i < layouts.length; i++) {76curPos[i] = 0;77}78}7980@Override81public void step(int w, int h) {82setSleepAmount(900);83for (int i = 0; i < 2; i++) {84if (layouts[i] == null) {85continue;86}87if (curPos[i]++ == layouts[i].getCharacterCount()) {88curPos[i] = 0;89}90}91}9293@Override94public void render(int w, int h, Graphics2D g2) {95FontRenderContext frc = g2.getFontRenderContext();96for (int i = 0; i < 2; i++) {97layouts[i] = new TextLayout(text[i], fonts[i], frc);98float rw = layouts[i].getAdvance();99float rx = ((w - rw) / 2);100float ry = ((i == 0) ? h / 3 : h * 0.75f);101102// draw highlighted shape103Shape hilite = layouts[i].getLogicalHighlightShape(0, curPos[i]);104AffineTransform at = AffineTransform.getTranslateInstance(rx, ry);105hilite = at.createTransformedShape(hilite);106float hy = (float) hilite.getBounds2D().getY();107float hh = (float) hilite.getBounds2D().getHeight();108g2.setColor(colors[i]);109g2.fill(hilite);110111// get caret shape112Shape[] shapes = layouts[i].getCaretShapes(curPos[i]);113Shape caret = at.createTransformedShape(shapes[0]);114115g2.setColor(BLACK);116layouts[i].draw(g2, rx, ry);117g2.draw(caret);118g2.draw(new Rectangle2D.Float(rx, hy, rw, hh));119120// Display character advances.121for (int j = 0; j <= layouts[i].getCharacterCount(); j++) {122float[] cInfo = layouts[i].getCaretInfo(TextHitInfo.leading(j));123String str = String.valueOf((int) cInfo[0]);124TextLayout tl = new TextLayout(str, smallF, frc);125tl.draw(g2, rx + cInfo[0] - tl.getAdvance() / 2, hy + hh + tl.126getAscent() + 1.0f);127}128}129}130131public static void main(String[] argv) {132createDemoFrame(new Highlighting());133}134}135136137