Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Build a JavaFX Application It's About Time Sounds About Right

Matthew Francis
Matthew Francis
6,967 Points

Resources and toExternalForm();

For example, when we want to implement a new font type, you would usually do this:

Font.loadFont(getClass().getResource("/fonts/VarelaRound-Regular.ttf").toExternalForm(), 10);

My understanding:

getClass() = returns the class name of getResource() , so it would be java.class.resource

getResource() = gets the non-java file(a resource) from the resource folder.

toExternamForm() = returns a file url (is that: /fonts/VarelaRound-Regular.ttf?), what is the purpose of returning a file url?

1 Answer

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

I'm not sure that is 100% right, but let me try to explain this how I see it:

  1. getClass() returns "blueprint" of the class you are into, so for example, if your code is inside Main class, it will return "blueprint" of Main and NOT "...class name of getResource...":
public class Main () {
    public void start() {
         getClass(); 
         // equivalent to
        this.getClass(); 
        // return "blueprint" of "Main" class
    }
}

For more on getClass() read here:

http://stackoverflow.com/questions/32919106/how-does-getclass-in-java-work

  1. getResource(String name) is used to load resources starting from this Class location. I think better name for this method would be getREsourceUrlFromClassLocation. So program is looking for resource file, starting from specified Class location, and returns java.net.URL class.

  2. toExternalForm() returns String representation of java.net.URL object:

http://docs.oracle.com/javase/6/docs/api/java/net/URL.html#toExternalForm()

So overall why it is needed and why we do that...

Our goal is to load resource file into Font class.

In order to achieve that we need to provide correct path for resource file using which this Font can be loaded (in this case tff file)

Now we don't have web application, so we have to play with relative paths of where our classes situated.

That's why we say

  • get class where we run method
  • get URL of resource relatively (from the class that was get)
  • convert URL to string in order to pass argument to Font.load
Matthew Francis
Matthew Francis
6,967 Points

Thank you so much for this! concise answer as always, but I just want to double check on this:

That's why we say

get class where we run method

get URL of resource relatively (from the class that was get)

convert URL to string in order to pass argument to Font.load

When we get the url it would return the file locaiton (eg; java/file/location) and toExternalForm() turn it into a string (eg; "java/file/location"), corect?

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

"When we get the url it would return the file location (eg; java/file/location)" - we get instance of the java.net.URL class, and for some reason, not quite understandable to me, instead of just toString(), we use toExternalForm(). Although as I just looked to documentation toString() and toExternalForm() are actually exactly the same:

http://docs.oracle.com/javase/6/docs/api/java/net/URL.html#toString()

http://docs.oracle.com/javase/6/docs/api/java/net/URL.html#toExternalForm()