views:

9

answers:

1

I have the following lines in my Java applet init function:

try {
  this.font = Font.createFont(
    Font.TRUETYPE_FONT,
    new File("fonts/myfont.ttf")
  ).deriveFont(24f);
  GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(font);
} catch(Exception ex){
  System.out.println(ex);
}

When I open the applet with the appletviewer, the font is loaded as expected. But when I open the HTML page with any web browser, I get this error in the Java Console:

java.security.AccessControlException: access denied (java.io.FilePermission fonts/myfont.ttf read)

and the default font is loaded.

(How) can I fix that?

+1  A: 

You can't access local files with applets---this is by design!

You can (and should) embed the font file inside your .jar file, and use Class.getResourceAsStream or the like to access it (Font.createFont has an overload that takes an InputStream, which is exactly the type that Class.getResourceAsStream returns).

Chris Jester-Young
I'd like this but I don't know how to do it... Could you please post a code example?
elektronikLexikon
@elektronikLexikon: I can't (because I think it's straightforward enough to do by yourself), but start by playing with (and reading about) `Class.getResourceAsStream`, making sure you understand how that function locates resources. Put the font file in the correct place (usually in the same location as your `.class` files), then play with it till it works. Once it works, passing it to `Font.createFont` is easy.
Chris Jester-Young
@Chris Jester-Young: Thank you! I think I get this to work.
elektronikLexikon
now it works! Thank you very very much!
elektronikLexikon