static new ThreadLocal renderText_fg; static new ThreadLocal renderText_bg; sbool renderText_withLeading = true; static BufferedImage renderText(S fontName, float fontSize, S text) { ret renderText(loadFont_cached(fontName), fontSize, text); } static BufferedImage renderText(Font font default sansSerif(), float fontSize, S text) { ret renderText(font.deriveFont(fontSize), text); } static BufferedImage renderText(Font font default sansSerif(), O _text) { S text = str(_text); Color background = optPar(renderText_bg, Color.white); Color foreground = optPar(renderText_fg, Color.black); // for font metrics FontMetrics fm = fontMetrics(font); int width = fm.stringWidth(text); if (width <= 0) null; int height = fm.getHeight(); int y = renderText_withLeading ? fm.getLeading() + fm.getMaxAscent() : fm.getAscent(); BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = imageGraphics(img); g.setColor(background); g.fillRect(0, 0, width, height); g.setColor(foreground); g.setFont(font); g.drawString(text, 0, y); g.dispose(); return img; }