Including images and font
Hello. I'm trying to include some resources (images and fonts) in my project. As of now is works if I run the project from Eclipse or if I export a runnable jar via Eclipse (all files in one jar). But if I build my project with ant it doesn't work.
Here is the addImage method:
Code :
public void addImage(String imagePath) {
/* Carica l'indirizzo dell'immagine */
URL imageUrl = getClass().getResource(imagePath);
/* Carica l'immagine */
Image myPicture = Toolkit.getDefaultToolkit().getImage(imageUrl);
ImageIcon image = new ImageIcon(myPicture);
/* Inserisci l'immagine nella gameText */
gameText.insertIcon(image);
}
which I call like this:
Code :
addImage("/jork/thegame/resources/images/intro-ork.png");
Here is the setFont method:
Code :
public final void setGameTextFont(String fontPath, int size) {
/* Carica la risorsa */
InputStream in = Gui.class.getResourceAsStream(fontPath);
try {
/* Crea un nuovo font */
Font font = Font.createFont(Font.TRUETYPE_FONT, in);
/* Applicalo alla gameText settando la dimensione a size */
gameText.setFont(font.deriveFont((float) size));
} catch (FontFormatException e) {
/* Salva il messaggio di errore */
Logger.log(e.getMessage());
} catch (IOException e) {
/* Salva il messaggio di errore */
Logger.log(e.getMessage());
}
}
which I call like this:
Code :
setGameTextFont("/jork/thegame/resources/fonts/LucidaTypewriterRegular.ttf", Constants.GAMETEXTFONT);
And finally here is my build.xml:
Code :
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." name="GestioneStudenti" default="compile"> <!-- default="sonar" -->
<property name="src.dir" value="./src" />
<property name="build.dir" location="./build" />
<property name="lib.dir" location="./lib" />
<!-- Add the Sonar task -->
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath path="/usr/share/ant/lib/" />
</taskdef>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="prepare" depends="clean">
<mkdir dir="${build.dir}"/>
</target>
<target name="compile" depends="prepare" description="Compila tutte le classi"> <!--depends="prepare"-->
<echo message="Compiling application sources..."/>
<javac srcdir="${src.dir}" destdir="${build.dir}"/>
<!-- classpathref = "compile.path" /-->
</target>
<target name="sonar" depends="compile" description="Invia il progetto a Sonar">
<echo message="WARNING::: Assicurasi che sonar sia in esecuzione!" />
<property name="sonar.sources" value="${src.dir}" />
<property name="sonar.java.source" value="1.6" />
<property name="sonar.java.target" value="1.6" />
<property name="sonar.binaries" value="${build.dir}" />
<property name="sonar.libraries" value="${lib.dir}" />
<!--property name="sonar.tests" value="${test.dir}" /-->
<!--property name="sonar.dynamicAnalysis" value="reuseReports" /-->
<sonar:sonar key="it.uniroma2:lmp" version="0.1" xmlns:sonar="antlib:org.sonar.ant"/>
</target>
</project>
Can anyone point out what I'm doing wrong?
Re: Including images and font
Eclipse takes care of copying resources (images, fonts, .property files etc.) for you during compilation and also remembers to include them when exporting to a Runnable jar.
However, Ant does not do all of that automatically. From your ant build files, I do not see any task to copy the fonts and images from "src.dir" to "build.dir". Please read up on Ant's copy task. You'll most probably need to copy your entire resources directory and make sure the "copy task" runs before your "jar task".
Re: Including images and font
Ok so I changed the prepare task in the build.xml like this:
Code :
<target name="prepare" depends="clean">
<mkdir dir="${build.dir}" />
<mkdir dir="${build.dir}/jork/thegame/resources" />
<copy todir="${build.dir}/jork/thegame/resources">
<fileset dir="${src.dir}/jork/thegame/resources" />
</copy>
</target>
And it works as expected. It seems a bit bulky so now I'm wondering if it is the correct way of doing it. Again thanks
EDIT: I've been contacted via other means and apparently this is the way to do so, except for the fact that copying the resource files over is more of a "compile" task than a "prepare" task.