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"));
}
}
添加回答
舉報