1 回答
TA贡献1828条经验 获得超13个赞
我不太确定为什么你的代码不起作用,因为这样做似乎是完全合乎逻辑的。我不明白为什么有些属性有效,有些则不起作用。
但根据 Java 2D 教程:使用文本属性来设置文本样式,应在字体上设置属性,而不是在文本本身上设置属性。即。用。SUPERSCRIPTFont.deriveFont(Map<Attribute, ?> attributes)
以下内容适用于我(我稍微修改了您的代码,使其不依赖于您的后台文件):
public class TextAttributesSuperscript {
static int curX = 10;
static int curY = 50;
public static void main(String[] args) throws Exception {
AttributedString attributedString = new AttributedString("this is data. this data should be super script");
attributedString.addAttribute(TextAttribute.FONT, new Font("TimesRoman", Font.PLAIN, 18));
attributedString.addAttribute(TextAttribute.FOREGROUND, Color.BLACK);
Font superScript = new Font("TimesRoman", Font.BOLD, 18)
.deriveFont(Collections.singletonMap(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER));
attributedString.addAttribute(TextAttribute.FONT, superScript, 30, 33);
attributedString.addAttribute(TextAttribute.FOREGROUND, Color.BLUE, 30,33);
BufferedImage image = new BufferedImage(400, 100, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
g.setColor(Color.WHITE);
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.drawString(attributedString.getIterator(), curX, curY);
g.dispose();
ImageIO.write(image, "png", new File("output.png"));
}
}
添加回答
举报