Results 1 to 5 of 5
  1. #1
    dark ares is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default 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:
    Java 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:
    Java Code:
    addImage("/jork/thegame/resources/images/intro-ork.png");
    Here is the setFont method:
    Java 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:
    Java Code:
    setGameTextFont("/jork/thegame/resources/fonts/LucidaTypewriterRegular.ttf", Constants.GAMETEXTFONT);
    And finally here is my build.xml:
    Java 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?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Including images and font

    How are you running this after the ant build?
    Presumably running the main class which will be in the build directory...in which case is the image or font file in that directory in the correct place?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    dark ares is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default Re: Including images and font

    Quote Originally Posted by Tolls View Post
    How are you running this after the ant build?
    Presumably running the main class which will be in the build directory...in which case is the image or font file in that directory in the correct place?
    Well I obiuously am a retard. The resources package wasn't being copied into build/. If I manually copy them over it works as expected. Now my question becomes: should I add something to build.xml to cp th resources folder over to build or is there some other way of handling resources? Like a bestpractice of some sort?

  4. #4
    dark ares is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default Re: Including images and font

    Ok so I changed the prepare task in the build.xml like this:
    Java 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.
    Last edited by dark ares; 09-04-2012 at 08:59 PM.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Including images and font

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 3
    Last Post: 04-10-2012, 02:50 AM
  2. Convert pdf to html, including images
    By josechum in forum Advanced Java
    Replies: 0
    Last Post: 03-09-2012, 01:40 PM
  3. Replies: 1
    Last Post: 12-19-2010, 05:46 AM
  4. Replies: 0
    Last Post: 08-18-2010, 11:31 AM
  5. Replies: 5
    Last Post: 04-25-2009, 09:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •