0
使用Graphics2D,我想在圖像中顯示文本,但我希望它顯示在一行中。這意味着如果文字比圖片長,尺寸會縮小(改編)Java圖形字體 - 如何確保文本保存在一行中?
我該怎麼辦?
到目前爲止,這是我所做的,但我確定它確實很糟糕。
int fontSize = 66;
FontRenderContext frc = new FontRenderContext(null, true, true);
element = null;
do { // we turn until the size goes into the image
fontSize -= 2;
layout = new TextLayout(myText, new Font(font, Font.BOLD, fontSize), frc);
element = layout.getPixelBounds(null, 0, 0);
if (fontSize <= 12) {
throw new Exception ("Title too long.");
}
} while(element.width > image.getWidth());
我該怎麼辦?
感謝您的幫助。