2

I am using java.awt.Frame for my Java application window which is being refreshed from a loop inside main.

The application behaves exactly as it should when it is run from Eclipse, but when I package it into a jar, It draws the first screen, but then nothing else after that.

when I try switching the window to a JFrame, it works, but only a portion of the images get drawn and updated.

I'm not sure what the problem is? If it runs fine from eclipse, shouldn't it run the exact same in a jar file?

EDIT: I figured out the problem. Its due to fonts not loading from the jar file. Is there a way to get these to load correctly? This is my code for them:

Font font = Font.createFont(Font.TRUETYPE_FONT, 
  obj.getClass().getClassLoader().getResource(fontName));
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
lufinkey
  • 342
  • 4
  • 15
  • Tell us what the `fontName` variable contains, where the file of the font is in your jar, and what `obj.getClass()` returns. – JB Nizet Dec 03 '12 at 21:00
  • This question is similar to http://stackoverflow.com/q/5669477/130224 – reprogrammer Dec 03 '12 at 21:01
  • I already tried what he said, and fontName is a string that holds "/Fonts/custom.ttf" – lufinkey Dec 03 '12 at 21:10
  • I also meant to put a .ToURI() in the post but thats not the problem – lufinkey Dec 03 '12 at 21:14
  • Start by removing the leading slash from the font name, and if it still doesn't work, answer the rest of my questions. – JB Nizet Dec 03 '12 at 21:16
  • So, how about answering the rest of my questions? It's the third time I ask you to answer these questions. Do you really want to be helped? – JB Nizet Dec 03 '12 at 21:25
  • I did if you loom at the other comments. I said that fontName holds "/Fonts/custom.ttf". The font is literally in /Fonts in the jar. i tried removing the slash and that didn't work. And obj.getClass is a method in the object class, the base class of every object in java – lufinkey Dec 03 '12 at 21:29
  • It's the first time you say where the file is in the jar. And I have 120K reputation, so don't you think I know what `obj.getClass()` does, and that Object is the base class of every object. I'm asking you what it *returns*, i.e. what the value of `obj.getClass()` is, i.e. what the class of the object referenced by the variable `obj` is. – JB Nizet Dec 03 '12 at 22:33
  • 1
    I honestly could care less what your reputation is. there is absolutely no reason to be condescending. I already figured out what the problem was. So for anyone who has this problem in the future, you have to use getResourceAsStream() rather than getResource – lufinkey Dec 04 '12 at 00:31

2 Answers2

1

Get an URL to it, then see this answer for creating it and registering it amongst the available fonts.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

Try this code for loading font files from within a .jar file.

import java.awt.Font;
import java.awt.FontFormatException;
import java.io.IOException;
import java.net.URISyntaxException;

public class FontLoader
{
    private ResourceLoader loader;

    public FontLoader(String fontFilePath)
    {
        loader = new ResourceLoader(fontFilePath);
    }

    public Font getFont(int fontStyle, float fontSize) throws FontFormatException, IOException, URISyntaxException
    {
        Font font = Font.createFont(Font.TRUETYPE_FONT, loader.getResource());

        font = font.deriveFont(fontStyle, fontSize);

        return font;
    }
}

Note that you also need my ResourceLoader class which you can find here:
How do I load a file from resource folder?

Community
  • 1
  • 1
BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185