Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Including images and font

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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:
    	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:
    addImage("/jork/thegame/resources/images/intro-ork.png");

    Here is the setFont method:
    	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:
    setGameTextFont("/jork/thegame/resources/fonts/LucidaTypewriterRegular.ttf", Constants.GAMETEXTFONT);

    And finally here is my build.xml:
    <?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
    Junior Member
    Join Date
    Sep 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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".
    Last edited by paramthegreat; September 4th, 2012 at 11:39 AM.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Including images and font

    Ok so I changed the prepare task in the build.xml like this:
    	<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; September 4th, 2012 at 12:59 PM.

Similar Threads

  1. problem with including contructor in method
    By backdoorliberty in forum Object Oriented Programming
    Replies: 7
    Last Post: June 16th, 2012, 11:57 AM
  2. Re: How to Change JTextArea font, font size and color
    By binokyo10 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 5th, 2012, 11:12 AM
  3. How to Change JTextArea font, font size and color
    By Flash in forum Java Swing Tutorials
    Replies: 7
    Last Post: January 14th, 2012, 09:47 PM
  4. Java: Links, images, font and divisions with HTML / CSS
    By Cyloc in forum AWT / Java Swing
    Replies: 3
    Last Post: August 2nd, 2011, 12:54 PM
  5. How to Change JTextArea font, font size and color
    By Flash in forum Java Code Snippets and Tutorials
    Replies: 4
    Last Post: July 8th, 2008, 01:45 PM