java drawstring 字体_java - 为什么字体大小为101的Graphics2D.drawString字母“o”坏了? - 堆栈内存溢出...

我想在png中绘制字符串。 我需要高达600像素的字母。

因此我尝试使用Graphics2D.drawString:

String text = "o";

graphics.setFont(font.deriveFont(Font.PLAIN, fontsize));

graphics.drawString(text, 0, 200);

字体大小最大为100的文本可以正常工作。 问题:在字体大小为101的情况下,某些圆形字符(如“o”)会变得有点倾斜。

这可以通过字体“Abril_Fatface”,“Amatic_SC”和“Cinzel_Decorative”(来自谷歌字体,例如https://fonts.google.com/specimen/Abril+Fatface或https://fonts.google.com/specimen/ )重现。 Amatic + SC ; ttf文件可通过“选择此字体”下载,然后在“1个系列选中”对话框中“下载此部分”。 使用其他字体,字符串被绘制好。

LibreOffice显示圆形(如预期)。

这是完整的代码:

import java.awt.Color;

import java.awt.Font;

import java.awt.FontFormatException;

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

public class CustomFontWriting {

public static void main(String[] args) throws FontFormatException, IOException {

Font font = Font.createFont(Font.TRUETYPE_FONT, new File("Amatic_SC.ttf"));

// Create empty image

BufferedImage image = new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB);

Graphics2D graphics = image.createGraphics();

graphics.setColor(Color.BLACK);

// Draw letter "o" with fontsize 101 in an image

graphics.setFont(font.deriveFont(Font.PLAIN, 101));

graphics.drawString("o", 0, 200);

// Write image to disk

File output = new File("output.png");

ImageIO.write(image, "png", output);

}

}

预期结果:圆形字母“o”。

aHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS96WVNrcS5wbmc=

实际结果:有角度的字母“o”。

aHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS96UE1XdS5wbmc=

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值