使用反射获取外部字体即可
public static void main(String[] args) throws IOException { //画板 BufferedImage i = new BufferedImage(400, 400, 2); //画笔 var m = i.createGraphics(); //平滑度 m.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP); m.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); m.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT); m.setColor(Color.YELLOW); m.fillRect(0, 0, 400, 400); String s = "Hello world 版权所有,陈卸甲!"; Font font = new Font("Aa狂派手书", Font.BOLD, 20); //反射机制获取外部字体 var is = Imges5.class.getClassLoader().getResourceAsStream("font/f1.ttf"); try { font = createFont(Font.TRUETYPE_FONT, is); } catch (FontFormatException e) { e.printStackTrace(); } //获取外部字体方式2 // var is2 = Thread.currentThread().getContextClassLoader().getResourceAsStream("font/f1.ttf"); // try { // font=Font.createFont(0,is2); // } catch (FontFormatException e) { // e.printStackTrace(); // } System.out.println(font.getName()); var fm = m.getFontMetrics(font); m.setColor(Color.red); font = font.deriveFont(50f); m.setFont(font); m.drawString(s, 10, 50); m.dispose(); ImageIO.write(i, "png", new File("ziti.png")); Runtime.getRuntime().exec("cmd /k start ziti.png"); }