Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/demos/Fonts/Outline.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.BLUE;36import static java.awt.Color.GREEN;37import static java.awt.Color.MAGENTA;38import static java.awt.Color.RED;39import static java.awt.Color.WHITE;40import java.awt.BasicStroke;41import java.awt.Font;42import java.awt.Graphics2D;43import java.awt.Shape;44import java.awt.font.FontRenderContext;45import java.awt.font.TextAttribute;46import java.awt.font.TextLayout;47import java.awt.geom.AffineTransform;48import java.text.AttributedCharacterIterator;49import java.text.AttributedString;50import java2d.Surface;515253/**54* Rendering text as an outline shape.55*/56@SuppressWarnings("serial")57public class Outline extends Surface {5859public Outline() {60setBackground(WHITE);61}6263@Override64public void render(int w, int h, Graphics2D g2) {6566FontRenderContext frc = g2.getFontRenderContext();67Font f = new Font(Font.SANS_SERIF, Font.PLAIN, w / 8);68Font f1 = new Font(Font.SANS_SERIF, Font.ITALIC, w / 8);69String s = "AttributedString";70AttributedString as = new AttributedString(s);71as.addAttribute(TextAttribute.FONT, f, 0, 10);72as.addAttribute(TextAttribute.FONT, f1, 10, s.length());73AttributedCharacterIterator aci = as.getIterator();74TextLayout tl = new TextLayout(aci, frc);75float sw = (float) tl.getBounds().getWidth();76float sh = (float) tl.getBounds().getHeight();77Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(w / 2 - sw78/ 2, h * 0.2 + sh / 2));79g2.setColor(BLUE);80g2.setStroke(new BasicStroke(1.5f));81g2.draw(sha);82g2.setColor(MAGENTA);83g2.fill(sha);8485f = new Font(Font.SERIF, Font.BOLD, w / 6);86tl = new TextLayout("Outline", f, frc);87sw = (float) tl.getBounds().getWidth();88sh = (float) tl.getBounds().getHeight();89sha = tl.getOutline(AffineTransform.getTranslateInstance(w / 2 - sw / 2, h90* 0.5 + sh / 2));91g2.setColor(BLACK);92g2.draw(sha);93g2.setColor(RED);94g2.fill(sha);9596f = new Font(Font.SANS_SERIF, Font.ITALIC, w / 8);97AffineTransform fontAT = new AffineTransform();98fontAT.shear(-0.2, 0.0);99Font derivedFont = f.deriveFont(fontAT);100tl = new TextLayout("Italic-Shear", derivedFont, frc);101sw = (float) tl.getBounds().getWidth();102sh = (float) tl.getBounds().getHeight();103sha = tl.getOutline(AffineTransform.getTranslateInstance(w / 2 - sw / 2, h104* 0.80f + sh / 2));105g2.setColor(GREEN);106g2.draw(sha);107g2.setColor(BLACK);108g2.fill(sha);109}110111public static void main(String[] s) {112createDemoFrame(new Outline());113}114}115116117