Forum Stats

  • 3,816,609 Users
  • 2,259,213 Discussions
  • 7,893,518 Comments

Discussions

Font not being created - default used.

User_7DOJ8
User_7DOJ8 Member Posts: 1 Green Ribbon

I have a Java application made up of several JTextPane instances, all arranged using a combination of BoxLayout and OverlayLayout. All of the panes are created in a single class, I call 'ttPane' in a single class called 'Screens'. All JTextPane instances and all layout is performed in the EDT.

In the Screens class I create a font by reading a .ttf file and then deriving the final 'default'.

public static void establishFont() {

       try {

           Font foo = Font.createFont(Font.TRUETYPE_FONT, new File(Screens.fontFileName));

           defaultFont = foo.deriveFont(Font.BOLD, (float) Screens.fontSize);

           defaultFontFamily = defaultFont.getFamily();

           charWidth = FontWidth.getAvgWidth(defaultFont);

           charHeight = FontWidth.getHeight(defaultFont);

       } catch (FontFormatException | IOException e) {

           System.out.println("FontFormatException encountered while trying to create the font.");

       }      

   }

All variables mentioned (other than 'foo') are Screens class variables defined elsewhere as static. The class variable "defaultFont" is referenced in all of the rest of the system as the panes are created and arranged later in the Screens initialization. The font named in 'Screens.fontFileName' is a fixed-width font (Cousine Bold), and the .ttf file is read.

With this code, the program does NOT honor the createFont/deriveFont but reverts to the Java default of DIALOG, which is not acceptable for this application.

If I replace the above code with

public static void establishFont() {

defaultFont = new Font( Font.MONOSPACED, Font.BOLD, Screens.fontSize);

defaultFontFamily = defaultFont.getFamily();

    charWidth = FontWidth.getAvgWidth(defaultFont);

  charHeight = FontWidth.getHeight(defaultFont);

}

The program works properly using the monospaced font provided by Java, but that is also not acceptable since that font is really crappy, particularly when I run the application in a Windows system.

My code is being developed on a Linux system, no IDE involved, just text edit/compile/test. I'll attach some small images that illustrate the difference. The application is fairly substantial, over 10k LOC, and consists of 15+/- JTextPane instances.

Any help greatly appreciated.

Bill Lee