猿问

除了删除线之外,属性化字符串的任何功能都不起作用

我必须写一个字符串到.我正在使用 . 正在工作。上标,下标等不起作用。BufferedImageAtrributedStringTextAttribute.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"));

    }

}

在执行上述代码时。上标部分不起作用(文本未像上标一样打印)


Cats萌萌
浏览 86回答 1
1回答

慕田峪7331174

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

相关分类

Java
我要回答