I'm wondering how you would go about importing a font.
I'm trying to use a custom downloaded font but since most computers that would go to run this would not have this font as it's not a default font. How would I go about making the font work even if they don't have the font?
I'm using it for a gameover screen and need to display a score with it and want the score text to be the same font. This is the image,
In case it matters the font name on my computer is Terminal
Edit: I'm assuming it would have to have the font in the directory of the java file and there would be some way of using that but I'm not sure how. Or is there a better way?
Edit2: I have found a nice tutorial on how to do it but need some help on how I go about using this... click me for link
Edit3:
URL fontUrl = new URL("http://www.webpagepublicity.com/" + "free-fonts/a/Airacobra%20Condensed.ttf");
Font font = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream());
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);
g.setFont(font);
Error Message
File: F:\Computer Science\draw.java [line: 252]
Error: F:\Computer Science\draw.java:252: font is not public in java.awt.Component; cannot be accessed from outside package
Here is what I'm trying:
URL fontUrl = new URL("http://img.dafont.com/dl/?f=badaboom_bb");
Font font = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream());
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);
g.setFont(font);
Edit4:
File fontfile = new File("TexasLED.ttf");
File.toURI(fontfile).toURL(fontfile);
URL fontUrl = new URL("fontfile");
Error
Error: F:\Computer Science\draw.java:250: toURI() in java.io.File cannot be applied to (java.io.File)
Font font = Font.createFont(Font.TRUETYPE_FONT, new File("A.ttf"));
– TandratandyFile
is a short-sighted answer to a short-sighted question. TheFont
is effectively an application resource, so should be Jar'd and accessed by URL (and convert that to anInputStream
for use in thecreateFont()
method). – BouncerFont font = Font.createFont(Font.TRUETYPE_FONT, new File("1979.ttf"));
– Turbinalnew File(path + "1979.ttf")
– Tandratandynew File(System.getProperty("user.dir") + "1979.ttf");
ornew File(".\\1979.ttf");
ornew File("./1979.ttf");
. – Tandratandycannot find symbol "file"
– Turbinalsrc
folder as this will cause the file to be included in the binary - it will be inside a jar if we create the jar). Still, it will get the file if the code refers to it correctly. UseFile.getAbsolutePath()
to tell whether the file path is correct or not. – TandratandyFont font ..
is (presumably) declared within atry
block so the reference tofont
goes out of scope before theg.setFont(font);
line. Move the call tosetFont
to the last line thetry
block, since it makes no sense to set the font if there was a problem. Also, for debugging purposes, calle.printStacktrace()
within thecatch
. – Bouncer-
and that's it. It's not a problem with my code as it worked fine before. So I tried to use the direct link to download a font from dafont.com. I believe I did it right but it just appears in normal arial font as this image shows – Turbinalcannot find symbol symbol: method printStacktrace()
You might be young, but if you cannot get used to finding and reading the manual, you won't get very far in programming. Look at the methods of theException
class and see if you can spot the mistake I made when typing that suggestion. (Tip: It is a method inherited from another class.) – Bouncer-
with no error. I'm guessing that it's because the font doesn't have numbers in it made? – TurbinalFont
is 1px (a ridiculously small size). When printing a line of characters it comes out looking something like......
. See my edited answer for a fix. – BouncerFile
as you suspected, or more importantly, it can point to an entry in a Zip or Jar file. That is important because if this font is an inherent part of the application, it might as well be put in a Jar along with the classes. Also, you don't want to force the user machine to download 35Kb ofFont
every time it is run! – Bouncerillegal escape character
Location is"F:\Computer Science\Texas LED.ttf"
So can I not do this? – Turbinal"F:\\Computer Science\\Texas LED.ttf"
in Java. – Tandratandyjava.net.MalformedURLException: unknown protocol: f
– TurbinalFile
object 2) CheckFile.exists()
3) CallFile.toURI().toURL()
. – BouncerFile.toURI(fontfile)
<head-desk /> 1) Remember what I was saying about the documentation? 2) I did not suggest passing any arguments to either method call. 3) Add to that documentation hunt the fact that I meant an instance of afile
rather than the classFile
. 4) At now 30+ comments, it is suggesting to me that perhaps you should ask a new, specific, question about converting aFile
to anURL
. 5) But note that the method to load aFont
accepts either anInputStream
or aFile
! – Bouncer