/* * @(#)Outline.java 1.12 98/06/29 * * 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. */ package demos.Fonts; import java.awt.*; import java.awt.event.*; import java.awt.geom.AffineTransform; import java.awt.font.TextLayout; import java.awt.font.FontRenderContext; import J2DCanvas; /** * Rendering text as an outline shape. */ public class Outline extends J2DCanvas { public Outline() { setBackground(Color.white); } public void drawDemo(Graphics2D g2) { FontRenderContext frc = g2.getFontRenderContext(); Font f = new Font("sansserif",Font.BOLD,w/4); TextLayout tl = new TextLayout("Font", f, frc); float sw = (float) tl.getBounds().getWidth(); float sh = (float) tl.getAscent(); Shape sha = tl.getOutline(null, w/2-sw/2, h/3); g2.setColor(Color.black); g2.draw(sha); f = new Font("serif", Font.BOLD,w/6); tl = new TextLayout("Outline", f, frc); sw = (float) tl.getBounds().getWidth(); sh = (float) tl.getAscent(); g2.setStroke(new BasicStroke(1.5f)); sha = tl.getOutline(null,w/2-sw/2,h/3.2f*2); g2.setColor(Color.black); g2.draw(sha); g2.setColor(Color.red); g2.fill(sha); f = new Font("sansserif",Font.ITALIC,w/6); tl = new TextLayout("Italic", f, frc); sw = (float) tl.getBounds().getWidth(); sh = (float) tl.getAscent(); sha = tl.getOutline(null, 0, 0); g2.translate(w/2-sw/2,h-h/8); g2.setColor(Color.green); g2.draw(sha); g2.setColor(Color.black); g2.fill(sha); g2.setTransform(new AffineTransform()); } public static void main(String s[]) { WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }; Frame f = new Frame("Java2D Demo - Outline"); f.addWindowListener(l); f.add("Center", new Outline()); f.pack(); f.setSize(new Dimension(400,300)); f.show(); } }