I have used this code below, and it comes up with this stack trace:
java.io.FileNotFoundException: font.ttf (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.io.FileInputStream.<init>(FileInputStream.java:101)
at com.ominious.core.graphics.Assets.getFont(Assets.java:55)
at com.ominious.core.graphics.Assets.loadImages(Assets.java:37)
at com.ominious.core.GamePanel.init(GamePanel.java:63)
at com.ominious.core.GamePanel.run(GamePanel.java:69)
at java.lang.Thread.run(Thread.java:744)
Exception in thread "Thread-1" java.lang.NullPointerException
at com.ominious.core.graphics.Assets.loadImages(Assets.java:49)
at com.ominious.core.GamePanel.init(GamePanel.java:63)
at com.ominious.core.GamePanel.run(GamePanel.java:69)
at java.lang.Thread.run(Thread.java:744)
I use this code (I call the method in a resource file that I know works)
private static Font getFont(String name) throws Exception {
Font font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(name));
return font;
}
And I call it here:
try {
FONT = getFont("font.ttf");
tileSprites = ImageIO.read(getClass().getResourceAsStream("/mom.gif"));
SPLASH_BACKGROUND = ImageIO.read(getClass().getResourceAsStream("/swag.gif"));
} catch (Exception e) {
Game.logger.log(LogType.ERROR_STACKTRACE);
e.printStackTrace();
}
(The class above works, my image loads)
Is there a reason why this doesn't work? Is there a better method? (And yes, I do have it in my directory)
It isn't that difficult to load a font from resources the same way you are loading the images. I know this question was asked a year ago, but i hope to finally provide an answer.
Simply use the documentation here, which details how to load custom fonts into a GraphicsEnvironment. It should look something like the following:
GraphicsEnvironment ge = null;
try{
ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, game.getClass().getResourceAsStream("/fonts/fantasy.TTF")));
} catch(FontFormatException e){} catch (IOException e){}
Note: I use classInstance.getClass().getResourceAsStream(String fileDir) to load the file from my resources directory in the Jar file.
After registering the font with the graphics environment, the font is available in calls to getAvailableFontFamilyNames() and can be used in font constructors.
Hope this answers your question finally!
Most probably because you're running this out of a jar, and there's no File object to get. Compare how you're loading your images with getResourceAsStream, which can find resources that are either unpacked as files (usually for development) or packaged into a jar. Use the same getResourceAsStream call in createFont.
Related
This question already exists:
Referencing Image within JAR [duplicate]
Closed 3 years ago.
I know this has been asked before but I can't get it to work after trying all variations.I have a small program that I'm wanting the launch frame to have an image displayed on it. I can achieve this by having the jar in the same folder as the image and referencing it, however when I try to reference within the jar file itself I keep getting error:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException
The code I am using is this:
private void imagePanel() {
setLayout(new BorderLayout());
BufferedImage image;
try {
image = ImageIO.read(this.getClass().getResource("/src/ticketMaster/img/logo.png"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
add(new JLabel(new ImageIcon(image)), BorderLayout.CENTER);
}
I'm not sure if the path is incorrect, I have made a img folder inside the package named img which contains the logo. I have tried all variations of it
/img/logo.png
/ticketMaster/img/logo.png
/src/ticketMaster/img/logo.png
I am not able to get the image to load like I would just referencing the image outside of the jar. I've used resourceasstream as well and not had any luck with that.
Any ideas here?
I dont know your folder or your package structure, but you can try to remove the first '/' for the path you given in the getRessource() method, it may be looking for a absulute path.
I'm implementing a GUI for a chess program I'm writing for a class. To make it look polished, I want to utilize a Chess Piece Font I obtained from the internet.
The file, chess.ttf, is located at the path Chess/resources/chess.ttf. I utilize the following code per Oracle's instructions (https://docs.oracle.com/javase/tutorial/2d/text/fonts.html):
try {
File file = new File("resources/chess.ttf");
//Returned font is of pt size 1
Font font = Font.createFont(Font.TRUETYPE_FONT, file);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, file));
//Derive and return a 12 pt version:
//Need to use float otherwise
//it would be interpreted as style
return font.deriveFont(12f);
}
catch (IOException|FontFormatException e) {
System.out.println("-Font Error-");
return null;
}
However, this throws an IOException. I ran getAbsolutePath() on the file and received /Users/[me]/eclipse-workspace/Chess/resources/chess.ttf so I'm assuming the file is being loaded correctly. Does anyone know what's going wrong with my code?
edit: Problem resolved? I tried InputStreams as suggested but that didn't work, yet upon reverting my code the computer finally stopped throwing IOExceptions. Isn't code the best?
You'd better copy the ttf using build script like ant or maven /Users/[me]/eclipse-workspace/Chess/bin/resources
ClassLoader loader = Thread.currentThread().getContextClassLoader();
System.out.println(loader.getResource(".").getPath()); // for tracing the working folder only
java.io.InputStream ins = loader.getResourceAsStream("resources/chess.ttf");
//Returned font is of pt size 1
Font font = Font.createFont(Font.TRUETYPE_FONT, ins);
I am using iTextPdf 5.5.3 to create PDF/A documents, I want the user to select custom fonts by uploading the .ttf file of the font, and becuase FontFactory.getFont() method only takes the font name as a string I have to write the uploaded file to the user's drive (I KNOW, I ASKED MY CUSTOMER FOR PERMISSION TO WRITE TO THE DRIVE) and then pass the path of the uploaded file to the getFont() method, after everything is finished I want to delete the uploaded files from the drive. Here is my code:
File fontFile = new File("d:/temp/testFont.ttf");
try {
FileOutputStream outStream = new FileOutputStream(fontFile);
outStream.write(the bytes of the uploaded font file);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Font font = FontFactory.getFont(fontFile.getAbsolutePath(), BaseFont.CP1250 , BaseFont.EMBEDDED);
fontFile.delete();
This code is not working, somehow the getFont() method is locking the font file and therefore the file is not being deleted. I tried lots of ways to do this, like: fontFile.deleteOnExit(); or FileDeleteStrategy.FORCE.delete("file path"); but nothing is working for me. Please advise. Thanks
I am not going to answer the question mentioned in the title of your post (because it is a secondary). Instead I am going to answer the question in the body (which is the essential question).
You claim that FontFactory.getFont() requires a font file on the file system. That is not incorrect. However, that doesn't mean you can't create a font from a byte[].
You are trying to solve your problem by saving a ttf on disk (which is forbidden by your customer), but that isn't necessary. In a way, your customer is right: it's not a good idea to save the TTF as a temporary file on disk (which is why I'm ignoring your secondary question).
Take a look at the following createFont() method:
public static BaseFont createFont(String name,
String encoding,
boolean embedded,
boolean cached,
byte[] ttfAfm,
byte[] pfb)
throws DocumentException,
IOException
This is how you should interpret the parameters in your case:
name - the name of the font (not the location)
encoding - the encoding to be applied to this font
embedded - true if the font is to be embedded in the PDF
cached - probably false in your case, as you won't be reusing the font in the JVM
ttfAfm - the bytes of the .ttf file
pfb - in your case, this value will benull (it only makes sense in the context of Type1 fonts).
Now you can meet the requirements of your customer and you do not need to introduce a suboptimal workaround.
Note: you are using iText 5.5.3 which is available under the AGPL. Please be aware that your customer will need to purchase a commercial license with iText Software as soon as he starts using iText in a web service, in a product,...
Looked at other posts on SO and they did not solve this issue.
I'm trying to load an image from my jar file. It is continuously coming up as null. The image is located under:
.Jar file > images > BLOCK.png
To load the image I am doing:
BufferedImage bImg;
URL url = getClass().getResource("/images/BLOCK.png");
try {
bImg = ImageIO.read(url);
} catch (IOException ex) {
Logger.getLogger(TileEngine.class.getName()).log(Level.SEVERE, null, ex);
}
url is null as is bImg.
Do not worry about case sensitivity as I've already checked that.
try this :
Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/BLOCK.png"));
Which jar file is the image in, compared with the class you're using to call getResource? If they're loaded by the same classloader, it should be fine.
Have you double-checked that the jar file actually contains the file?
Are you sure that your classloader is actually using the jar file at all (rather than .class files directly on disk, for example)?
If you have a short but complete program which demonstrates the problem, that would really help. (A console app would be ideal... we don't need to see the image, after all.)
I have an application which has a font stored within a jar file. It is loaded with:
public Font getChessFont()
{
InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream("fonts\\MERIFONTNEW.TTF");
Font toReturn;
try
{
toReturn = Font.createFont(Font.TRUETYPE_FONT, in);
}
catch (Exception e)
{
toReturn = gameInformation;
}
toReturn = toReturn.deriveFont(Font.PLAIN, squareSize);
return toReturn;
}
When running the program from Eclipse or a jar file this code loads the font sucessfuly. However, after I put the jar files into an ISO image and mount them to a disk the files fail to load. Any ideas as to what I'm doing wrong?
Apparently my comment was enough to solve this. So the question can be "answered", I have added the comment as an answer:
Resource paths usually should use forward slash (/) in the path (more like a URL) as this is platform independent.
Are the files/JARs on the disk on the classpath?