Java using the third-party font library method, using the third-party TTF/TTC font library method
Sometimes we use Java fonts in our programs, but not in all font systems. We may use external custom fonts, so we will work less in program migration and deployment. Recently, we used custom font files in a project. If we straighten them out, remember them.
class Loadfont
{
public static Font loadFont(String fontFileName, float fontSize) //The first parameter is the external font name, the second is the font size
{
try
{
File file = new File(fontFileName);
FileInputStream aixing = new FileInputStream(file);
Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, aixing);
Font dynamicFontPt = dynamicFont.deriveFont(fontSize);
aixing.close();
return dynamicFontPt;
}
catch(Exception e)
{
e.printStackTrace();
return new java.awt.Font("宋体", Font.PLAIN, 12);
}
}
public static java.awt.Font Font(){
String root=System.getProperty("user.dir");//Project root path
System.out.println(root);
Font font = Loadfont.loadFont(root+"/simsun.ttc", 12f);//call
return font;//return the font
}
public static java.awt.Font Font2(){
String root=System.getProperty("user.dir");//Project root path
System.out.println(root);
Font font = Loadfont.loadFont(root+"/simsun.ttc", 12f);
return font;//return the font
}
}
usage method:
String root= System.getProperty (” user.dir “);
String fontname = “Droid Serif”;
Font font = Loadfont.loadFont (root+”/ xx.ttf “, 16F); — & gt; to the required xx.ttf Font library file.
If not, you can try to double-click the TTF file, install it (the previous question is Windows system), the above internal class, test can, do not need to install the TTF file.
Or directly put the TTF file into the JRE/lib/fonts directory. If there is no directory, you can MKDIR
In new font (“DDD”, XX, XX); – & gt; DDD is double-click aa.ttf The file font name, not the file name.
Read More:
- [Solved] Springboot project introduces Font library error: java.awt.fontformatexception: bad table, tag = XXXXXX
- Abstract method and static method of java interface
- Java: How to Find the Minimum Value from a Random Array by Bubble Sort Method
- Java: How to use itext to export PDF text absolute positioning (implementation method)
- How to Solve IntelliJ IDEA Error: Cannot determine path to ‘tools.jar‘ library for 17 (C:\Program Files\Java\jd…
- Ali easyexcel error: org.apache.poi.ss.usermodel.font.setbold (z) V
- [Solved] IDEA springboot Startup Error: java.lang.UnsatisfiedLinkError: no tcnative-1 in java.library.path
- [Solved] error: method does not override or implement a method from a supertype
- [Solved] Method threw ‘java.lang.StackOverflowError‘ exception. Cannot evaluate
- [Solved] M1 Chip MacBook Pro Error: snappy-java FAILED_TO_LOAD_NATIVE_LIBRARY
- [Solved] Mvel2 Error: java.lang.VerifyError method: getKnownEgressType signature
- Asynchronous processing of HTTP request by Java_ Method 2: through deferredresult
- Asynchronous processing of HTTP request by Java_ Method 1: through callable
- [Solved] IDEA java Cannot resolve method ‘getName‘ in ***
- [Solved] Invocation of init method failed; nested exception is java.lang.NoSuchMethodError:
- JAVA: How to Use Minio to upload pictures
- Java Web uses AJAX POST Method 405 terror [Solved]
- [Solved] Error:Cannot determine path to ‘tools.jar‘ library for 17
- How to Solve FATAL ERROR in native method: JDWP on getting class status, jvmtiError=JVMTI_ERROR_WRONG_PHASE
- JAVA: How to Use Multipartfile to upload Files