How to get a list of font names from Java

In Java, we can create a Font object as such:

new Font("Helvetica", Font.PLAIN, 12); 

My question is: how to get the whole list of font names from Java, for example "Helvetica", which we can use as an argument for the Font constructor?

I tried the following, but can not find "Helvetica" in all the lists.

  GraphicsEnvironment ge; ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); String[] names = ge.getAvailableFontFamilyNames(); Font[] allFonts = ge.getAllFonts(); for(int x=0; x<names.length; x++) System.out.println(names[x]); for(int x=0; x<allFonts.length; x++){ System.out.println(allFonts[x].getName()); System.out.println(allFonts[x].getFontName()); System.out.println(allFonts[x].getFamily()); System.out.println(allFonts[x].getPSName()); } 

Edit: More importantly, I also want to know what is the first attribute call in the new Font("What attribute is this?", Font.PLAIN, 12) font constructor new Font("What attribute is this?", Font.PLAIN, 12)

Q: Is it a font name, family name, fontFace, name or what?

+5
source share
4 answers

On your system, this font may be mapped to something else.

 Font helvetica = new Font("Helvetica", Font.PLAIN, 12); System.out.println(helvetica.getFontName(Locale.US)); 

and i get

 SansSerif.plain 

To output the names of all local fonts, you can use something like

 GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment(); Font[] allFonts = ge.getAllFonts(); for (Font font : allFonts) { System.out.println(font.getFontName(Locale.US)); } 
+3
source
 new Font("Helvetica", Font.PLAIN, 12); 

In this case, it is better to use something like:

 new Font(Font.SANS_SERIF, Font.PLAIN, 12); 

This will cause unecorated Font be used by default for this OS.

On Windows, it will be Arial. On OS X, it will be Helvetica. On * nix machines, this can be either the 3rd or undecorated Font .


In response to your specific question, I always found the string "font family" useful for instantiating a font.

+1
source

I know this is an old question, but there are some unanswered questions.

More importantly, I also want to know what the first attribute call in Font constructor new Font ("What attribute is this?", Font.PLAIN, 12) Q: Is this the font name, surname, font, name or what?

If you decompile the Java class using the IDE (I use IntelliJ), you will see:

 public Font(String name, int style, int size) { this.name = (name != null) ? name : "Default"; this.style = (style & ~0x03) == 0 ? style : 0; this.size = size; this.pointSize = size; } public String getName() { return name; } public String getFontName() { return getFontName(Locale.getDefault()); } 

This suggests that the name you use when calling the constructor can be obtained using getName, but calling getFontName will return your text by default. That is why you can set a name for Helvetica, then call getFontName and return something other than Helvetica.

0
source

this program will show you a list of all the fonts in your system:

 import java.awt.GraphicsEnvironment; public class ListJavaFonts { public static void main(String[] args) { String fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); for ( int i = 0; i < fonts.length; i++ ) { System.out.println(fonts[i]); } } } 
0
source

Source: https://habr.com/ru/post/1214393/


All Articles