DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2004
    Posts
    4

    Using ImageIcons in java applets

    Hi,

    I'm new to this site because I'm having a problem changing one of my java swing applications to an applet so that I can put it on my website. The application is a little poker game I made, and it works great (even as an applet) except for when I run it using a web browser. No matter what I do, it seems that I cannot load the images into my program running it from internet explorer. The images are .jpgs and are in a folder one up from the .class location; I am loading the images using the ImageIcon class, and setting the icons onto buttons. The weird thing is that it all works when I run the applet in my compiler, so I don't know what's wrong. ANY help will be greatly appreciated! Thank you very much!

    Babak Rashidi

  2. #2
    Join Date
    Jul 2004
    Posts
    4
    No one has any answers?

  3. #3
    Join Date
    Feb 2004
    Posts
    808
    youre attempting to read the images directly off disk, rather than usint getDocumentBase(), and hence youre running into a security exception, because applets arent allowed to access the local disk


    http://forums.javaboutique.internet....&threadid=2490
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  4. #4
    Join Date
    Jul 2004
    Posts
    4

    Thanks for your help!

    Wow, you guys had quite the post about it before, thanks a lot! I've made the change, and my problem is better than it used to be (the program now doesn't stop running) except I still have ONE problem, that being that the images still don't show up. Anyway, it looks like it's a problem with where the images are stored or something (Although I know that's not it) but I can figure it out (and if I can't... I'll be back!) And btw cjar, your tutorial on jar files is awesome!!! I recommend anyone trying to put things on the internet to have a look!! I'm actually hoping that may solve my present problem. Thanks!

    Babak

  5. #5
    Join Date
    Aug 2004
    Posts
    2
    I had difficulty with using imageicons in an applet, so I nicked the following code from Sun:

    protected static ImageIcon createImageIcon(String path, String description) {
    java.net.URL imgURL = SwingApplet5.class.getResource(path);
    if (imgURL != null) {
    return new ImageIcon(imgURL, description);
    } else {
    System.err.println("Couldn't find file: " + path);
    return null;
    }


    Then in the method that uses the imageicon, I used:

    ImageIcon copyIcon = createImageIcon("images/copy.gif", "Copy");

    where the images folder is in the folder containing the applet.
    HTH

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links