Results 1 to 9 of 9
Thread: Printing an available font list.
- 11-15-2011, 09:04 PM #1
Printing an available font list.
Hey guys, another newb question.
Why won't this work?
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.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); } }
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.
- 11-15-2011, 09:12 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: Printing an available font list.
For each of the symbols that are unknown check each of the things that can cause it.I'm getting missing symbol errors, but that error can be caused by many things, and I'm not sure which it is.
* 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
- 11-15-2011, 09:18 PM #3
Re: Printing an available font list.
Well I'm getting complaints re: the fonts variable. Is there something wrong with that declaration?
- 11-15-2011, 09:25 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: Printing an available font list.
Yes. There is no declaration! :)
A declaration will start with the type of the thing being declared:
Have a look at the GraphicsEnvironment API docs to see the type that getAvailableFontFamilyNames() returns.Java Code:Thing myThing = foo.methodReturningThing();
- 11-15-2011, 10:35 PM #5
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:
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?Java Code:[Ljava.lang.String;@1cc7b00c
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:
Also, I have one last question.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)); } }
If I code the println statement as two statements, like this:
It gives me the hash code again. Why?Java Code:Arrays.toString(fonts); System.out.println(fonts);
-
Re: Printing an available font list.
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.
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:Also, I have one last question.
If I code the println statement as two statements, like this:
It gives me the hash code again. Why?Java Code:Arrays.toString(fonts); System.out.println(fonts);
Java Code:String stringReturned = Arrays.toString(fonts); System.out.println(stringReturned);
- 11-15-2011, 11:29 PM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: Printing an available font list.
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[].Strongly typed indeed.
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.
- 11-15-2011, 11:59 PM #8
- 11-16-2011, 12:17 AM #9
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.This I will bear in mind next time, thanks guys. Problem solved!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.
Similar Threads
-
Set Font Color, Font Style & Font Family While Replacing Text
By sherazam in forum Java SoftwareReplies: 0Last Post: 08-18-2010, 10:31 AM -
Adding new fonts to the list of available font??
By Stephen Douglas in forum NetBeansReplies: 15Last Post: 04-18-2010, 02:33 PM -
Help on Printing the IDs of a list
By jboy in forum New To JavaReplies: 22Last Post: 09-11-2009, 02:37 PM -
[New to java]: HoW to set font from the available list in the system
By pndiwakar in forum AWT / SwingReplies: 4Last Post: 03-10-2009, 06:31 AM -
Revised Linked List printing method question
By CirKuT in forum New To JavaReplies: 7Last Post: 12-12-2008, 10:21 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks