由于不想在服务器去安装字体 想通过项目里放置字体文件来防止项目上线字体问题导致画出来的图中文乱码,所以决定使用Font.createFont(int fontFormat, InputStream fontStream)来导入本地字体。

首先我是这样写的

//模板图片
ClassPathResource resource = new ClassPathResource("template/template1.png");
//字体文件
ClassPathResource fontResource = new ClassPathResource("font/simsun.ttf");
// 获取模板图片
BufferedImage buffImg = ImageIO.read(resource.getInputStream());
// 开启画图
Graphics2D graphics = buffImg.createGraphics();
Font qrNameFont = Font.createFont(Font.TRUETYPE_FONT, fontResource.getInputStream());
graphics.setFont(qrNameFont);
//接下来就是操作graphics画图 但是字总写不出来

一番抓耳挠腮之后。度娘了半天的我决定看官方文档

public static Font createFont(int fontFormat,InputStream fontStream)throws FontFormatException,IOException

Returns a new Font using the specified font type and input data. The new Font is created with a point size of 1 and style PLAIN. This base font can then be used with the deriveFont methods in this class to derive new Font objects with varying sizes, styles, transforms and font features. This method does not close the InputStream.

To make the Font available to Font constructors the returned Font must be registered in the GraphicsEnviroment by calling registerFont(Font).

为了让字体可用必须注册。。。。。。。原因找到了,接下来看GraphicsEnviroment 的registerFont方法

可以看到整个GraphicsEnviroment 中有创建 GraphicsEnviroment 对象的getLocalGraphicsEnvironment()方法

还有一个创建Graphics2D对象的createGraphics(java.awt.image.BufferedImage)方法。

好了 流程大概就清楚了

首先创建GraphicsEnviroment 对象   再把创建的字体registerFont进GraphicsEnviroment 对象   最终通过GraphicsEnviroment创建Graphics2D对象     用这个Graphics2D对象才有创建的字体

代码改写如下

//模板
ClassPathResource resource = new ClassPathResource("template/template1.png");
//字体
ClassPathResource fontResource = new ClassPathResource("font/simsun.ttf");
// 获取模板图片
BufferedImage buffImg = ImageIO.read(resource.getInputStream());
// 设置文字
Font font = Font.createFont(Font.TRUETYPE_FONT, fontResource.getInputStream());
设置字体
font = font.deriveFont(Font.BOLD, 64F);
Font multiFont = font.deriveFont(Font.BOLD, 60f);
Font finalfont = font.deriveFont(Font.PLAIN, 50f);
GraphicsEnvironment localGraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
localGraphicsEnvironment.registerFont(font);
localGraphicsEnvironment.registerFont(multiFont);
localGraphicsEnvironment.registerFont(finalfont);
// 开启画图
Graphics2D graphics = localGraphicsEnvironment.createGraphics(buffImg);
//设置字体
graphics.setFont(font);
//这里就是大小为64F的字体
graphics.setFont(multiFont);
//这里就是大小为60f的字体
graphics.setFont(finalfont);
//这里就是大小为50f的字体

好了 问题解决  还是要多看官方文档啊

java.awt.Font.createFont 画图的时候字体不显示问题相关推荐

  1. java.awt.font 宋体,SpringBoot项目集成字体工具类

    场景:采用JasperReport生成报表时,若将模版中的字体配置成宋体时.部署在不同的系统上时,可能会出现中文乱码的情况,也可以适用于其他任何需要单独配置字体的第三方库. 首先,你可以给部署的服务器 ...

  2. JavaAwtSwing笔记之 字体 java.awt.Font

    java.awt.Font 直接继承自Object 前言 类声明 构造函数 public Font(String name, int style, int size) { public Font(Ma ...

  3. MATLAB画图中英文不同字体混合显示标题、坐标轴标签(完美解决方案)

    MATLAB是吾等理工狗常用的求生工具.在画图时对中英文标题均支持较好,但学术出版物常常对字体有所要求.对于英文出版物一切好说,设置成相应字体即可.而对于中文出版物.中文毕业论文.报告,则常常需要混合 ...

  4. java出现无法读取_Java无法读取字体

    好的,所以使用自定义字体时出现问题.基本上,我会从网上下载一个自定义字体,然后在程序中使用它.当我在Eclipse(我使用的编辑器)中运行程序时,一切正常,并且没有问题.但是,每当我将其从eclips ...

  5. java.awt.FontFormatException: Unsupported sfnt C:\Windows\Fonts\mstmc.ttf

    具体报错 java.awt.FontFormatException: Unsupported sfnt C:\Windows\Fonts\mstmc.ttfat sun.font.TrueTypeFo ...

  6. java.awt包_java.awt包介绍

    组件, Component类是所有AWT组件的根. 用户与组件交互操作时,一些组件会激发事件, AWTEvent类及其子类用于表达AWT组件能够激发的事件. 容器是一个可以含有组件和其他容器的组件, ...

  7. java.awt包_java.awt包 简介

    java.awt有创建用户接口.绘图和图像的所有类.用户接口对象,例如按钮或滚动条, 在AWT(Abstrat Window Toolkit)中被称为组件, Component类是所有AWT组件的根. ...

  8. java 消除png 锯齿_java Graphics2d消除锯齿,使字体平滑显示

    标签: Java 2D API 提供的文本处理功能进行美化.Java 2D API 的文本功能包括: 使用抗锯齿处理和微调(hinting)以达到更好的输出质量 可以使用系统安装的所有字体 可以将对图 ...

  9. java使用paint方法画图

    java使用paint方法画图 需要继承JFrame类来画窗口 => public class Game extends JFrame {} setTitle(String s); //设置窗口 ...

最新文章

  1. 获取app传入的json值处理
  2. 2-SAT 问题 详解
  3. 《高阶Perl》——导读
  4. vue 优化CDN加速
  5. 云信小课堂|如何实现音视频通话
  6. Python之路----------内置函数
  7. [css] 移动端的布局用过媒体查询吗?写出例子看看
  8. 两个分布的特征映射_跨语言分布表示学习方法概述
  9. python 金字塔的程序_python生成金字塔
  10. async spring 默认线程池_springboot中@Async默认线程池导致OOM问题
  11. 一款功能强大的 IP 查询工具!
  12. 省会城市房价地图,这9座新星城市正在悄然崛起
  13. mysql xtrabackup-v2_MySQL物理备份基础知识(基于xtrabackup2.4版本)
  14. 谷歌中国算法比赛解题报告 APAC2017C
  15. PTP简介-IEEE1588协议
  16. STM32F7xx基于HAL库的USB_CDC接收数据的函数调用
  17. Acer 4750 安装黑苹果_黑苹果安装 你要知道的那些坑
  18. pr如何处理音效_pr怎么把视频声音去掉 去掉视频中的所有声音,视频消音操作...
  19. 2021阿里云ECS镜像导入本地VMware虚拟机
  20. 9个offer,12家公司,35场面试,计算机应届毕业生求职之路

热门文章

  1. FreeCAD编译错误解决方法
  2. word编辑过程中突然发现后面好几页消失不见
  3. linux关闭显示器接口类型,Linux Mint 笔记本外接显示器,关闭笔记本显示器的方法...
  4. android 5星,[android]市场满分5星游戏推荐:battleheart(勇者之心) | 古意人
  5. 20230313用友善之臂的Android11跑起来ROC-RK3399-PC(U-boot)
  6. python seaborn的常用方法及小例子,免费开源!
  7. 湖南本地购物网站——路在何方
  8. Java多线程(入门系列)
  9. UltraISO(软碟通)制作各类U启动
  10. 运动耳机也有降噪?飞利浦降噪真无线耳机A7507开箱