1 36 37 40 41 package java2d.demos.Fonts; 42 43 import static java.awt.Color .*; 44 import java.awt.*; 45 import java.awt.event.*; 46 import java.awt.geom.AffineTransform ; 47 import java.awt.font.TextAttribute ; 48 import java.awt.font.TextLayout ; 49 import java.awt.font.FontRenderContext ; 50 import java.text.AttributedString ; 51 import java.text.AttributedCharacterIterator ; 52 import java2d.Surface; 53 54 55 58 public class Outline extends Surface { 59 60 public Outline() { 61 setBackground(WHITE); 62 } 63 64 65 public void render(int w, int h, Graphics2D g2) { 66 67 FontRenderContext frc = g2.getFontRenderContext(); 68 Font f = new Font("sansserif",Font.PLAIN,w/8); 69 Font f1 = new Font("sansserif",Font.ITALIC,w/8); 70 String s = "AttributedString"; 71 AttributedString as = new AttributedString (s); 72 as.addAttribute(TextAttribute.FONT, f, 0, 10 ); 73 as.addAttribute(TextAttribute.FONT, f1, 10, s.length() ); 74 AttributedCharacterIterator aci = as.getIterator(); 75 TextLayout tl = new TextLayout (aci, frc); 76 float sw = (float) tl.getBounds().getWidth(); 77 float sh = (float) tl.getBounds().getHeight(); 78 Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(w/2-sw/2, h*0.2+sh/2)); 79 g2.setColor(BLUE); 80 g2.setStroke(new BasicStroke(1.5f)); 81 g2.draw(sha); 82 g2.setColor(MAGENTA); 83 g2.fill(sha); 84 85 f = new Font("serif", Font.BOLD,w/6); 86 tl = new TextLayout ("Outline", f, frc); 87 sw = (float) tl.getBounds().getWidth(); 88 sh = (float) tl.getBounds().getHeight(); 89 sha = tl.getOutline(AffineTransform.getTranslateInstance(w/2-sw/2,h*0.5+sh/2)); 90 g2.setColor(BLACK); 91 g2.draw(sha); 92 g2.setColor(RED); 93 g2.fill(sha); 94 95 f = new Font("sansserif",Font.ITALIC,w/8); 96 AffineTransform fontAT = new AffineTransform (); 97 fontAT.shear(-0.2, 0.0); 98 Font derivedFont = f.deriveFont(fontAT); 99 tl = new TextLayout ("Italic-Shear", derivedFont, frc); 100 sw = (float) tl.getBounds().getWidth(); 101 sh = (float) tl.getBounds().getHeight(); 102 sha = tl.getOutline(AffineTransform.getTranslateInstance(w/2-sw/2,h*0.80f+sh/2)); 103 g2.setColor(GREEN); 104 g2.draw(sha); 105 g2.setColor(BLACK); 106 g2.fill(sha); 107 } 108 109 110 public static void main(String s[]) { 111 createDemoFrame(new Outline()); 112 } 113 } 114 | Popular Tags |