亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

除了刪除線之外,屬性化字符串的任何功能都不起作用

除了刪除線之外,屬性化字符串的任何功能都不起作用

Cats萌萌 2022-09-14 10:33:15
我必須寫一個字符串到.我正在使用 . 正在工作。上標,下標等不起作用。BufferedImageAtrributedStringTextAttribute.STRIKETHROUGHpublic 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"));    }}在執行上述代碼時。上標部分不起作用(文本未像上標一樣打?。?
查看完整描述

1 回答

?
慕田峪7331174

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"));

    }

}


查看完整回答
反對 回復 2022-09-14
  • 1 回答
  • 0 關注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號