Results 1 to 9 of 9
  1. #1
    Daimoth's Avatar
    Daimoth is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Printing an available font list.

    Hey guys, another newb question.

    Why won't this work?
    Java Code:
    import java.awt.*;
    
    public class FontList
    {
    	public static void main(String [] args)
    	{		
    	GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
    	fonts = g.getAvailableFontFamilyNames();
    	System.out.println(fonts);
    	}
    }
    Basically I'm trying to develop a cheesy little font selector program, but I needed to know how to get the fonts from my graphics environment (I'm on OS X). I figured I'd start by printing them to the console. It took me a long time to realize that I needed to use the getLocalGraphicsEnvironment method before that could happen, but I'm still not sure why I need to do that in the first place. I'm guessing the GetAvailableFontFamilyNames method isn't static, so I needed to get out of a static context.

    Anyways, the program, as is, yields missing symbol errors, but that error can be caused by many things, and I'm not sure which it is.
    Last edited by Daimoth; 11-15-2011 at 09:11 PM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Printing an available font list.

    I'm getting missing symbol errors, but that error can be caused by many things, and I'm not sure which it is.
    For each of the symbols that are unknown check each of the things that can cause it.

    * It is a variable that you forgot to declare
    * It is a variable spelt incorrectly
    * It is a class but you didn't import its package
    * It is a class but you spelt it wrong
    * It is a method but you splet it wrong
    * It is a method but you sent it the wrong number of arguments, or arguments of the wrong type

  3. #3
    Daimoth's Avatar
    Daimoth is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Re: Printing an available font list.

    Well I'm getting complaints re: the fonts variable. Is there something wrong with that declaration?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Printing an available font list.

    Yes. There is no declaration! :)

    A declaration will start with the type of the thing being declared:

    Java Code:
    Thing myThing = foo.methodReturningThing();
    Have a look at the GraphicsEnvironment API docs to see the type that getAvailableFontFamilyNames() returns.

  5. #5
    Daimoth's Avatar
    Daimoth is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Re: Printing an available font list.

    Will do.

    Update:

    Okay, so the method returns an array. I initialized it properly, as a String[]. Strongly typed indeed.

    However, what got printed is:

    Java Code:
    [Ljava.lang.String;@1cc7b00c
    I assume I don't know how to print arrays, but what is that? Is it a hash code or something? Can I glean any information from one like that?



    Okay, so this led to the program functioning as intended - it's fixed. It prints out a (surprisingly large) list of fonts to the console. Here it is, on the off chance that it will be useful to someone:

    Java Code:
    import java.awt.*;
    import java.util.*;
    
    public class FontList
    {
    	public static void main(String [] args)
    	{		
    	GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
    	String[] fonts = g.getAvailableFontFamilyNames();
    	System.out.println(Arrays.toString(fonts));
    	}
    }
    Also, I have one last question.

    If I code the println statement as two statements, like this:

    Java Code:
    Arrays.toString(fonts);
    System.out.println(fonts);
    It gives me the hash code again. Why?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Printing an available font list.

    Quote Originally Posted by Daimoth View Post
    However, what got printed is:

    Java Code:
    [Ljava.lang.String;@1cc7b00c
    that is the toString() representation of a String array. If you want to see the contents you must either loop through the array or use the Arrays.toString(...) method that you've discovered.

    Also, I have one last question.

    If I code the println statement as two statements, like this:

    Java Code:
    Arrays.toString(fonts);
    System.out.println(fonts);
    It gives me the hash code again. Why?
    Well, what does Arrays.toString(fonts); do? It doesn't change the array that is passed in to the method in any way, but it creates and returns a String that shows all of the items held in the array. But your code doesn't assign the String returned to any variable, and so you are basically discarding the String returned and then printing the same old unchanged array. To solve this, consider not discarding the String returned from the Arrays.toString(...) method:

    Java Code:
    String stringReturned = Arrays.toString(fonts);
    System.out.println(stringReturned);

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Printing an available font list.

    Strongly typed indeed.
    Indeed. It's a gotcha until you're used to fact that although it says it returns String[] you *still* have to declare the variable as String[].

    Also with the Arrays.toString(), you just get used to the fact that these sorts of methods tend to return something rather than change the object in some way. If you think about it, it has to be the way describes since once fonts is declared as String[] it will remain String[] for all time and cannot be changed to String.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default Re: Printing an available font list.

    Quote Originally Posted by pbrockway2 View Post
    It is a method but you splet it wrong
    Oh the irony.

  9. #9
    Daimoth's Avatar
    Daimoth is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Re: Printing an available font list.

    Well, what does Arrays.toString(fonts); do? It doesn't change the array that is passed in to the method in any way, but it creates and returns a String that shows all of the items held in the array. But your code doesn't assign the String returned to any variable, and so you are basically discarding the String returned and then printing the same old unchanged array.
    Also with the Arrays.toString(), you just get used to the fact that these sorts of methods tend to return something rather than change the object in some way.
    This I will bear in mind next time, thanks guys. Problem solved!

Similar Threads

  1. Replies: 0
    Last Post: 08-18-2010, 10:31 AM
  2. Adding new fonts to the list of available font??
    By Stephen Douglas in forum NetBeans
    Replies: 15
    Last Post: 04-18-2010, 02:33 PM
  3. Help on Printing the IDs of a list
    By jboy in forum New To Java
    Replies: 22
    Last Post: 09-11-2009, 02:37 PM
  4. Replies: 4
    Last Post: 03-10-2009, 06:31 AM
  5. Revised Linked List printing method question
    By CirKuT in forum New To Java
    Replies: 7
    Last Post: 12-12-2008, 10:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •