1 36 37 40 41 package java2d.demos.Fonts; 42 43 import java.awt.*; 44 import java.awt.font.*; 45 import java.awt.geom.*; 46 import java.net.URL ; 47 import java.io.File ; 48 import java.io.FileInputStream ; 49 import java.io.InputStream ; 50 import java.awt.image.BufferedImage ; 51 import java.text.AttributedString ; 52 import java.text.AttributedCharacterIterator ; 53 import java2d.Surface; 54 55 import static java.awt.Font .*; 56 import static java.awt.font.TextAttribute .*; 57 58 59 63 public class AttributedStr extends Surface { 64 65 static Color black = new Color( 20, 20, 20); 66 static Color blue = new Color( 94, 105, 176); 67 static Color yellow = new Color(255, 255, 140); 68 static Color red = new Color(149, 43, 42); 69 static Color white = new Color(240, 240, 255); 70 static String text = " A quick brown fox jumped over the lazy duke "; 71 static AttributedString as = new AttributedString (text); 72 static AttributedCharacterIterator aci; 73 static { 74 Shape shape = new Ellipse2D.Double(0,25,12,12); 75 ShapeGraphicAttribute sga = new 76 ShapeGraphicAttribute(shape, GraphicAttribute.TOP_ALIGNMENT, false); 77 as.addAttribute(CHAR_REPLACEMENT, sga, 0, 1); 78 79 80 Font font = new Font ("sanserif", BOLD|ITALIC, 20); 81 int index = text.indexOf("quick"); 82 as.addAttribute(FONT, font, index, index+5); 83 84 index = text.indexOf("brown"); 85 font = new Font ("serif", BOLD, 20); 86 as.addAttribute(FONT, font, index, index+5); 87 as.addAttribute(FOREGROUND, red, index, index+5); 88 89 index = text.indexOf("fox"); 90 AffineTransform fontAT = new AffineTransform(); 91 fontAT.rotate(Math.toRadians(10)); 92 Font fx = new Font ("serif", BOLD, 30).deriveFont(fontAT); 93 as.addAttribute(FONT, fx, index, index+1); 94 as.addAttribute(FONT, fx, index+1, index+2); 95 as.addAttribute(FONT, fx, index+2, index+3); 96 97 fontAT.setToRotation(Math.toRadians(-4)); 98 fx = font.deriveFont(fontAT); 99 index = text.indexOf("jumped"); 100 as.addAttribute(FONT, fx, index, index+6); 101 102 font = new Font ("serif", BOLD|ITALIC, 30); 103 index = text.indexOf("over"); 104 as.addAttribute(UNDERLINE, UNDERLINE_ON, index, index+4); 105 as.addAttribute(FOREGROUND, white, index, index+4); 106 as.addAttribute(FONT, font, index, text.length()); 107 108 font = new Font ("dialog", PLAIN, 20); 109 int i = text.indexOf("duke"); 110 as.addAttribute(FONT, font, index, i-1); 111 112 BufferedImage bi = new BufferedImage (4,4,BufferedImage.TYPE_INT_ARGB); 113 bi.setRGB(0, 0, 0xffffffff); 114 TexturePaint tp = new TexturePaint(bi,new Rectangle(0,0,4,4)); 115 as.addAttribute(BACKGROUND, tp, i, i+4); 116 font = new Font ("serif", BOLD, 40); 117 as.addAttribute(FONT, font, i, i+4); 118 } 119 120 121 public AttributedStr() { 122 setBackground(Color.white); 123 124 Font font = getFont("A.ttf"); 125 if (font != null) { 126 font = font.deriveFont(PLAIN, 70); 127 } else { 128 font = new Font ("serif", PLAIN, 50); 129 } 130 int index = text.indexOf("A")+1; 131 as.addAttribute(FONT, font, 0, index); 132 as.addAttribute(FOREGROUND, white, 0, index); 133 134 font = new Font ("dialog", PLAIN, 40); 135 int size = getFontMetrics(font).getHeight(); 136 BufferedImage bi = 137 new BufferedImage (size, size, BufferedImage.TYPE_INT_ARGB); 138 Graphics2D big = bi.createGraphics(); 139 big.drawImage(getImage("snooze.gif"), 0, 0, size, size, null); 140 ImageGraphicAttribute iga = 141 new ImageGraphicAttribute(bi, GraphicAttribute.TOP_ALIGNMENT); 142 as.addAttribute(CHAR_REPLACEMENT, iga, text.length()-1, text.length()); 143 144 aci = as.getIterator(); 145 } 146 147 148 public void render(int w, int h, Graphics2D g2) { 149 150 float x = 5, y = 0; 151 FontRenderContext frc = g2.getFontRenderContext(); 152 LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc); 153 154 g2.setPaint(new GradientPaint(0,h,blue,w,0,black)); 155 g2.fillRect(0, 0, w, h); 156 157 g2.setColor(white); 158 String s = "AttributedString LineBreakMeasurer"; 159 Font font = new Font ("serif", PLAIN, 12); 160 TextLayout tl = new TextLayout(s, font, frc); 161 162 tl.draw(g2, 5, y += (float) tl.getBounds().getHeight()); 163 164 g2.setColor(yellow); 165 166 while (y < h-tl.getAscent()) { 167 lbm.setPosition(0); 168 while (lbm.getPosition() < text.length()) { 169 tl = lbm.nextLayout(w-x); 170 if (!tl.isLeftToRight()) { 171 x = w - tl.getAdvance(); 172 } 173 tl.draw(g2, x, y += tl.getAscent()); 174 y += tl.getDescent() + tl.getLeading(); 175 } 176 } 177 } 178 179 180 public static void main(String s[]) { 181 createDemoFrame(new AttributedStr()); 182 } 183 } 184 | Popular Tags |