AttributedStrpublic class AttributedStr extends Surface Demonstrates how to build an AttributedString and then render the
string broken over lines. |
Fields Summary |
---|
static Color | black | static Color | blue | static Color | yellow | static Color | red | static Color | white | static String | text | static AttributedString | as | static AttributedCharacterIterator | aci |
Constructors Summary |
---|
public AttributedStr()
Shape shape = new Ellipse2D.Double(0,25,12,12);
ShapeGraphicAttribute sga = new ShapeGraphicAttribute(shape, GraphicAttribute.TOP_ALIGNMENT, false);
as.addAttribute(TextAttribute.CHAR_REPLACEMENT, sga, 0, 1);
Font font = new Font("sanserif", Font.BOLD | Font.ITALIC, 20);
int index = text.indexOf("quick");
as.addAttribute(TextAttribute.FONT, font, index, index+5);
index = text.indexOf("brown");
font = new Font("serif", Font.BOLD, 20);
as.addAttribute(TextAttribute.FONT, font, index, index+5);
as.addAttribute(TextAttribute.FOREGROUND, red, index, index+5);
index = text.indexOf("fox");
AffineTransform fontAT = new AffineTransform();
fontAT.rotate(Math.toRadians(10));
Font fx = new Font("serif", Font.BOLD, 30).deriveFont(fontAT);
as.addAttribute(TextAttribute.FONT, fx, index, index+1);
as.addAttribute(TextAttribute.FONT, fx, index+1, index+2);
as.addAttribute(TextAttribute.FONT, fx, index+2, index+3);
fontAT.setToRotation(Math.toRadians(-4));
fx = font.deriveFont(fontAT);
index = text.indexOf("jumped");
as.addAttribute(TextAttribute.FONT, fx, index, index+6);
font = new Font("serif", Font.BOLD | Font.ITALIC, 30);
index = text.indexOf("over");
as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, index, index+4);
as.addAttribute(TextAttribute.FOREGROUND, white, index, index+4);
as.addAttribute(TextAttribute.FONT, font, index, text.length());
font = new Font("dialog", Font.PLAIN, 20);
int i = text.indexOf("duke");
as.addAttribute(TextAttribute.FONT, font, index, i-1);
BufferedImage bi = new BufferedImage(4,4,BufferedImage.TYPE_INT_ARGB);
bi.setRGB(0, 0, 0xffffffff);
TexturePaint tp = new TexturePaint(bi,new Rectangle(0,0,4,4));
as.addAttribute(TextAttribute.BACKGROUND, tp, i, i+4);
font = new Font("serif", Font.BOLD, 40);
as.addAttribute(TextAttribute.FONT, font, i, i+4);
setBackground(Color.white);
Font font = getFont("A.ttf");
if (font != null) {
font = font.deriveFont(Font.PLAIN, 70);
} else {
font = new Font("serif", Font.PLAIN, 50);
}
int index = text.indexOf("A")+1;
as.addAttribute(TextAttribute.FONT, font, 0, index);
as.addAttribute(TextAttribute.FOREGROUND, white, 0, index);
font = new Font("dialog", Font.PLAIN, 40);
int size = getFontMetrics(font).getHeight();
BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
Graphics2D big = bi.createGraphics();
big.drawImage(getImage("snooze.gif"), 0, 0, size, size, null);
ImageGraphicAttribute iga = new ImageGraphicAttribute(bi, GraphicAttribute.TOP_ALIGNMENT);
as.addAttribute(TextAttribute.CHAR_REPLACEMENT, iga, text.length()-1, text.length());
aci = as.getIterator();
|
Methods Summary |
---|
public static void | main(java.lang.String[] s)
createDemoFrame(new AttributedStr());
| public void | render(int w, int h, java.awt.Graphics2D g2)
float x = 5, y = 0;
FontRenderContext frc = g2.getFontRenderContext();
LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
g2.setPaint(new GradientPaint(0,h,blue,w,0,black));
g2.fillRect(0, 0, w, h);
g2.setColor(white);
String s = "AttributedString LineBreakMeasurer";
Font font = new Font("serif", Font.PLAIN, 12);
TextLayout tl = new TextLayout(s, font, frc);
tl.draw(g2, 5, y += (float) tl.getBounds().getHeight());
g2.setColor(yellow);
while (y < h-tl.getAscent()) {
lbm.setPosition(0);
while (lbm.getPosition() < text.length()) {
tl = lbm.nextLayout(w-x);
if (!tl.isLeftToRight()) {
x = w - tl.getAdvance();
}
tl.draw(g2, x, y += tl.getAscent());
y += tl.getDescent() + tl.getLeading();
}
}
|
|