【问题标题】:No feature of AttributedString is working other than strikethrough除了删除线之外,没有任何 AttributedString 功能可用
【发布时间】:2019-08-22 00:33:09
【问题描述】:

我必须向BufferedImage 写一个字符串。我正在使用AtrributedStringTextAttribute.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


【解决方案1】:

我不太确定为什么您的代码不起作用,因为这样做似乎完全合乎逻辑。而且我不明白为什么有些属性有效而有些则无效。

但是根据the Java 2D Tutorial: Using Text Attributes to Style TextSUPERSCRIPT 属性应该设置在 font 上,而不是文本本身上。 IE。使用Font.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"));
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-13
    • 2016-09-02
    • 2017-07-09
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2012-12-23
    • 2018-03-01
    相关资源
    最近更新 更多