【发布时间】:2011-02-22 07:27:20
【问题描述】:
我正在创建一个 servlet,它使用给定的文本呈现 jpg/png。我希望文本以渲染图像为中心。我可以得到宽度,但我得到的高度似乎是错误的
Font myfont = new Font(Font.SANS_SERIF, Font.BOLD, 400);
BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setFont(myfont);
g.setColor(Color.BLACK);
FontMetrics fm = g.getFontMetrics();
Integer textwidth = fm.stringWidth(imagetext);
Integer textheight = fm.getHeight();
FontRenderContext fr = g.getFontRenderContext();
LineMetrics lm = myfont.getLineMetrics("5", fr );
float ascent = lm.getAscent();
float descent = lm.getDescent();
float height = lm.getHeight();
g.drawString("5", ((imagewidth - textwidth) / 2) , y?);
g.dispose();
ImageIO.write(image, "png", outputstream);
这些是我得到的值: 文本宽度 = 222 文本高度 = 504 上升 = 402 下降 = 87 高度 = 503
有人知道如何得到“5”的准确高度吗?估计高度应该在 250 左右
【问题讨论】:
标签: java awt graphics2d