Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4222384

Font(name,style,size) works correctly ONLY AFTER getAvailableFontFamilyNames()

XMLWordPrintable

    • 2d
    • kestrel
    • x86
    • windows_95, windows_98, windows_nt



      Name: dbT83986 Date: 03/21/99


      I want to use a windows true type font for displaying and
      printing. But when I use for example

      font = new Font("Tahoma",Font.PLAIN,20);

      I get the Font Arial not Tahoma. When I first call

      GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

      or

      GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();

      then afterwards the above Font constructor works perfectly well
      for all truetype fonts on my system.


      Try the following program with and without the call to
      getAvailableFontFamilyNames():

      ------------------------------------------------------------------
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;

      public class FontBug extends JFrame
      {
          JButton b1,b2,b3,b4;
          JLabel l1,l2,l3,l4;
          Font f1,f2,f3,f4;

          public FontBug()
          {
      addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e)
      {
      System.exit(0);
      }
      });

      b1 = new JButton("The quick brown fox jumps over the lazy dog.");
      b2 = new JButton("The quick brown fox jumps over the lazy dog.");
      b3 = new JButton("The quick brown fox jumps over the lazy dog.");
      b4 = new JButton("The quick brown fox jumps over the lazy dog.");

      l1 = new JLabel();
      l2 = new JLabel();
      l3 = new JLabel();
      l4 = new JLabel();


      /*
      * Next I try to allocate 4 different fonts.
      *
      * The first two "standard" fonts work as they should.
      *
               * The other two "more special" fonts just give you ARIAL,
      * UNLESS you call "getAllFonts" or "getAvailableFontFamilyNames"
      * first.
      *
      * The "getAvailableFontFamilyNames" seems to initialize something
      * in "sun.java2d.SunGraphicsEnvironment" or in
      * "sun.awt.font.NativeFontWrapper" that makes the font selection
      * work right.
      *
      */


              // Try once without and once with the following line!

      // GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();


      // these two standard fonts work
      f1 = new Font("Arial",Font.PLAIN,20);
      f2 = new Font("serif",Font.PLAIN,20);

      // these two fail and give you Arial
      // choose any "special" truetype fonts installed on your system
      f3 = new Font("Garamond",Font.PLAIN,20);
      f4 = new Font("Tahoma",Font.PLAIN,20);


      b1.setFont(f1);
      b2.setFont(f2);
      b3.setFont(f3);
      b4.setFont(f4);

      l1.setText(f1.toString());
      l2.setText(f2.toString());
      l3.setText(f3.toString());
      l4.setText(f4.toString());

      GridLayout glay = new GridLayout(4,2);
      glay.setVgap(10);
      glay.setHgap(10);

      Container cp = getContentPane();
      cp.setLayout(glay);

      cp.add(b1);
      cp.add(l1);
      cp.add(b2);
      cp.add(l2);
      cp.add(b3);
      cp.add(l3);
      cp.add(b4);
      cp.add(l4);
          }


          public static void main(String[] args)
          {
      FontBug fontBug = new FontBug();
      fontBug.pack();
      fontBug.setVisible(true);
          }

      }

      ------------------------------------------------------------------
      (Review ID: 55855)
      ======================================================================

      Name: rlT66838 Date: 06/11/99


      Calling Font.getFont("Tahoma",Font.PLAIN,12) returns this font:
      java.awt.Font[family=Arial,name=Tahoma,style=plain,size=12]

      Subsequent drawing using that font object uses the Arial font data instead of Tahoma.
      (Review ID: 54090)
      ======================================================================

      Name: rlT66838 Date: 06/11/99


      Calling Font.getFont("Tahoma",Font.PLAIN,12) returns this font:
      java.awt.Font[family=Arial,name=Tahoma,style=plain,size=12]

      Subsequent drawing using that font object uses the Arial font data instead of Tahoma.
      (Review ID: 54090)
      ======================================================================

      Name: krT82822 Date: 07/07/99


      Java 1.2.2 reports that it is possible to use a
      system or "physical" font.

      However, one must first retrieve all fonts via
      GraphicsEnvironment.getAllFonts() before this works.

      This is a time-consuming operation.

      In many cases, where the user has many fonts, this is
      _very_ time-consuming.

      For systems wishing to display international characters
      via the Lucida fonts, this problem is annoying, especially
      considering Java's reputation for being perhaps the leading
      Unicode-ready and international language.
      (Review ID: 85294)
      ======================================================================

      Name: wl91122 Date: 07/20/99


      Serialized local fonts loaded with getAllFonts load always as
      Font "Arial".

      Load and compile the following code. But uncomment the commented
      block first.

      import java.awt.*;
      import java.io.*;
      import javax.swing.*;

      public class TestFont {

        static Font someFnt=null;
        static Font loadedFnt=null;

        public static void main(String args[]) {
         /*
          System.out.println("Loading all fonts ...");
          java.awt.GraphicsEnvironment ge =
              java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment();
          Font[] allFonts = ge.getAllFonts();
          for (int i=0;i<allFonts.length;i++){
            if (allFonts[i].getFontName().equals("BeesWax"))
               someFnt = allFonts[i];
          }
          System.out.println("... found Font "+someFnt.getFontName());
          try {
            FileOutputStream fo = new FileOutputStream("someFont");
            ObjectOutputStream oo = new ObjectOutputStream(fo);
            oo.writeObject(someFnt);
            oo.close();
            fo.close();
          } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error - writing some font failed! ");
          }
          System.out.println("some font successfully written");
          */
          try {
            FileInputStream in = new FileInputStream("someFont");
            ObjectInputStream oi = new ObjectInputStream(in);
            loadedFnt = (Font)oi.readObject();
            oi.close();
            in.close();
            System.out.println("loaded font "+loadedFnt.getFontName());
          } catch (Exception e) {
           System.err.println("Failed to load some font");
            System.exit(1);
          }
        }

      }

      The Program will report that it wrote and loaded back the font
      BeesWax. Which is perfect.

      Now comment the (same!) block out again, so the program will not
      load all system fonts nor will write them to file.
      Of course it will load the prevousely written file from the
      first run. (If you didn' cheat and removed it meanwhile)

      You will find the program has loaded the font "Arial". (Printing
      proves that this is true!)

      Something must have been still there, which was not serialized,
      at the first run, what made the loaded font the same as the written.
      (Review ID: 52274)
      ======================================================================

      Name: krT82822 Date: 08/23/99


      1. Until GraphicsEnvironment#getAvailableFontFamilyNames() has been invoked, fonts other than Monospaced, Dialog, DialogInput etc. are not available.

      2.
      public class TestMain
      {
         public static void main(String args[])
         {
            String fontFamilyNames[];
            Font font;


            fontFamilyNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
            font = new Font("Lucida Bright", Font.PLAIN, 12);
            System.out.println("font = " + font);
            System.exit(0);
         }
      }

      The above code works; i.e. the output is:

      font = java.awt.Font[family=Lucida Bright,name=Lucida Bright,style=plain,size=12]

      However, if the getAvailableFontFamilyNames() method invocation is commented out, the output is:

      font = java.awt.Font[family=Arial,name=Lucida Bright,style=plain,size=12]

      I.e. I get an Arial font in stead of the Lucida Bright...

      3. N/A

      4. N/A

      5.
      java -version
      java version "1.2.2"
      Classic VM (build JDK-1.2.2-W, native threads, symcjit)

      java -fullversion
      java full version "JDK-1.2.2-W"

      6.

      The code works as expected on Solaris JDK 1.2.1, native threads, sunwjit.
      (Review ID: 94242)
      ======================================================================

            pkejriwasunw Parry Kejriwal (Inactive)
            dblairsunw Dave Blair (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: