/* * @(#)Text.java 1.14 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.Clipping; import java.awt.*; import java.awt.event.*; import java.awt.geom.Line2D; import java.awt.font.TextLayout; import java.awt.font.FontRenderContext; import J2DCanvas; /** * Clipping an image, lines and text with text. */ public class Text extends J2DCanvas { private Image img; public Text() { setBackground(Color.white); img = getImage("images/clouds.jpg"); } public void drawDemo(Graphics2D g2) { FontRenderContext frc = g2.getFontRenderContext(); Font f = new Font("Helvetica",Font.BOLD,w/6); String s = new String("Java 2D"); TextLayout tl = new TextLayout(s, f, frc); float sw = (float) tl.getBounds().getWidth(); float sh = (float) tl.getAscent(); Shape shape = tl.getOutline(null,w/2-sw/2,h/4); Rectangle r = shape.getBounds(); g2.setColor(Color.gray); g2.draw(shape); g2.setClip(shape); g2.drawImage(img, r.x, r.y, r.width, r.height, this); f = new Font("Helvetica",Font.BOLD,w/8); s = new String("Text Clipping"); tl = new TextLayout(s, f, frc); sw = (float) tl.getBounds().getWidth(); sh = (float) tl.getAscent(); shape = tl.getOutline(null,w/2-sw/2,h/2); g2.setClip(shape); g2.setColor(Color.black); g2.fill(shape.getBounds()); g2.setColor(Color.yellow); for (int j = shape.getBounds().y; j < shape.getBounds().y + shape.getBounds().height; j=j+3) { Line2D line = new Line2D.Float( 0.0f, (float) j, (float) w, (float) j); g2.draw(line); } f = new Font("Times New Roman Bold",Font.PLAIN,w/4); s = new String("FoNtS"); tl = new TextLayout(s, f, frc); sw = (float) tl.getBounds().getWidth(); shape = tl.getOutline(null,w/2-sw/2,h-h/6); g2.setClip(shape); g2.setColor(Color.black); g2.fill(shape.getBounds()); g2.setColor(Color.cyan); f = new Font("Helvetica",Font.BOLD,10); tl = new TextLayout("java", f, frc); sw = (float) tl.getBounds().getWidth(); r = shape.getBounds(); int x = r.x; int y = r.y; while ( y < (r.y + r.height+(int) tl.getAscent()) ) { tl.draw(g2, x, y); if ((x += (int) sw) > (r.x+r.width)) { x = r.x; y += (int) tl.getAscent(); } } } public static void main(String s[]) { WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }; Frame f = new Frame("Java2D Demo - Text"); f.addWindowListener(l); f.add("Center", new Text()); f.pack(); f.setSize(new Dimension(400,300)); f.show(); } }