Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2004

Tip: Looking for answers? Try searching our database.

getImage can't find my gif

Thread view: 
chris - 13 Jan 2004 22:46 GMT
Even though I've added the directory where logo.gif lives to
CLASSPATH, this line doesn't work:

Image myImage = Toolkit.getDefaultToolkit().getImage("logo.gif");

No errors, but just doesn't show up in GUI.

If I add the path as follows it works:

Image myImage = Toolkit.getDefaultToolkit().getImage("C:\\temp\\logo.gif");

I've seen people using this, .getImage( getCodeBase(), "file.gif"),
but I can't use that with Toolkit, I think. Compiler doesn't like it.

I've also seen something about image file loading in java being slow,
so maybe I need to add a delay after the getImage call?

thanks.

chris
Andrew Thompson - 14 Jan 2004 06:23 GMT
...
| I've seen people using this, .getImage( getCodeBase(), "file.gif"),
| but I can't use that with Toolkit, I think. Compiler doesn't like it.

Did you think to try..
.getImage( getCodeBase().toString() + "file.gif")?
chris - 14 Jan 2004 18:00 GMT
> ...
> | I've seen people using this, .getImage( getCodeBase(),
[quoted text clipped - 4 lines]
> Did you think to try..
> .getImage( getCodeBase().toString() + "file.gif")?

Thanks for the suggestion. As you can tell, I'm a Java newbie.

I get unresolved symbol for getCodeBase().

Here's my imports:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.ImageIcon;
import javax.swing.border.Border;
import java.util.*;
import java.io.BufferedWriter;
import java.io.PrintWriter;
import java.io.IOException;

chris
Andrew Thompson - 14 Jan 2004 23:03 GMT
....
| > Did you think to try..
| > .getImage( getCodeBase().toString() + "file.gif")?
|
| Thanks for the suggestion. As you can tell, I'm a Java newbie.

As you might be able to tell (very soon),
I f***ed up!  Sorry, I misread your question
as being an Applet (where you _can_ use
getCodeBase() ), as a more general thing,
for applets, application, jar files...
you can use the magical

Class.getResource("file.gif");

To get an URL to the resource.

HTH
(this time!)

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
Swede - 18 Jan 2004 22:01 GMT
Hiya!
This thread solved my problem with relative paths as well.
Thanks for sharing!
/Lars

> ....
> | > Did you think to try..
[quoted text clipped - 21 lines]
> * http://www.1point1C.org/ 1.1C - Superluminal!
> * http://www.AThompson.info/andrew/ personal site
Thomas Weidenfeller - 15 Jan 2004 07:36 GMT
> Thanks for the suggestion. As you can tell, I'm a Java newbie.

Newbie questions are supposed to go to comp.lang.java.help.

> I get unresolved symbol for getCodeBase().

Read the API documentation for exactly(!) the version of Java you are
using. Check the documentation for the package and class name of that
method.

Regarding your GUI problems, I would suggest you work through the
beginner's tutorial on Sun's web site first, followed by the GUI tutorials.

Also, consider not wasting your time with applets, but start with
applications. That way you have one thing less to worry about (no
worries about ancient VMs in browsers and differences in plug-in behavior)

/Thomas
Andrew Thompson - 15 Jan 2004 11:50 GMT
...
|  > I get unresolved symbol for getCodeBase().
|
| Read the API documentation for exactly(!) the version of Java..
<snip good links>

The OP was probably reading the correct
doc's Thomas, trouble is I 'guessed' it was an
applet and led the the conversation astray..

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
hiwa - 14 Jan 2004 09:42 GMT
> Even though I've added the directory where logo.gif lives to
> CLASSPATH, this line doesn't work:
[quoted text clipped - 16 lines]
>
> chris

CLASSPATH only pertains to Java classes. Also, you should not
use absolute path name if you want Java program portability
across different platforms/environments.
chris - 14 Jan 2004 21:10 GMT
> Even though I've added the directory where logo.gif lives to
> CLASSPATH, this line doesn't work:
[quoted text clipped - 16 lines]
>
> chris

Finally solved it. Not sure how/why though. Found a posting elsewhere
that suggested this:

Image myImage = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("logo.gif"));

It must have something to do with the fact that I'm running as an
application? getCodeBase seems to be related to applet, and not app?

chris
David Postill - 14 Jan 2004 22:33 GMT
 /**
  * Get the document URL.
  *
  * @return the <code>URL</code> of the document containing the applet.
  *
  * @since JToolkit 0.2
  */
 public URL getCodeBase()
   {
   // Try getting the codebase system parameter, If that isn't available then do the following
   // hack.
   //
   // Hack! Loop through each item in CLASSPATH, checking if the appropriately named .class file
   // exists there.
   //
   // Bug! This doesn't account for .jar/.zip files.
   String codebase = System.getProperty( "codebase " );

   try
     {
     if ( codebase == null )
       {
       String path = System.getProperty( "java.class.path" );
       Enumeration enum = new StringTokenizer( path, System.getProperty( "path.separator" ) );

       while ( enum.hasMoreElements() )
         {
         String dir = ( String ) enum.nextElement();
         String filename = dir + File.separatorChar + fName + ".class";
         File file = new File( filename );

         if ( file.exists() )
           {
           String urlDir = dir.replace( File.separatorChar, '/' );

           return new URL( "file://" + urlDir + "/" );
           }
         }
       }
     else
       {
       return new URL( codebase );
       }
     }
   catch ( MalformedURLException e )
     {
     return null;
     }

   return null;
   }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.