【发布时间】:2019-08-22 00:33:09
【问题描述】:
我必须向BufferedImage 写一个字符串。我正在使用AtrributedString。 TextAttribute.STRIKETHROUGH 正在工作。上标、下标和其他都不起作用。
public class TextAttributesSuperscript {
static String Background = "input.png";
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);
attributedString.addAttribute(TextAttribute.FONT, new Font("TimesRoman", Font.BOLD, 18), 30,33);
attributedString.addAttribute(TextAttribute.FOREGROUND, Color.BLUE, 29,33);
attributedString.addAttribute(TextAttribute.SUPERSCRIPT,TextAttribute.SUPERSCRIPT_SUPER,30,33);
final BufferedImage image = ImageIO.read(new File(Background));
Graphics g = image.getGraphics();
g.drawString(attributedString.getIterator(), curX, curY);
g.dispose();
ImageIO.write(image, "png", new File("output.png"));
}
}
在执行上述代码时。上标部分不起作用(文本没有像上标那样打印)
【问题讨论】:
-
@AndrewThompson 请看一下代码。我犯了什么错误?
-
上标部分不工作
-
@AndrewThompson 编辑问题描述
标签: java text awt bufferedimage java-2d