Results 1 to 15 of 15
  1. #1
    Stewie is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default Images getting wiped sometimes [RESOLVED]

    Ok, so, I have a new applet I'm working on and have encountered a bit of a problem.

    I've developed it up to the point where I have a library of images that are rendered every quarter of a second or less. This works perfectly.

    However, it only works like this:
    If I use IE, I have to: Load IE, go to the applet, let it load, then use it. It works fine. If I refresh, it no longer renders the images, and tells me that their width is -1. If I use the console to clear the classloader cache then refresh the applet, the images display fine again.
    If I use FF, I have to: Load FF and go straight to the applet. No amount of refreshing or clearing the cache allows the images to display again.

    I currently use a loop on Init that adds all the images to my library array, then checks to see if their width is >= 0. If it is, the image is "loaded". Otherwise, it shows the loading bar, which sticks at a random percentage when using Firefox.

    How can I fix this so that it loads the images every time?

    Hope I explained this clearly enough. If anything is unclear please ask!

    Thanks in advance.
    Last edited by Stewie; 04-29-2009 at 06:22 AM. Reason: Added RESOLVED tag.

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Check that you are using init(), start(), stop() and destroy() correctly.
    Then check you are doing the drawing correctly (e.g. overriding paintComponent() and calling repaint() when necessary)
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    Stewie is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default

    paintComponent() and repaint() are called correctly, as they work for everything except images.

    What kind of things should I do to ensure that I am using init(), start(), stop() and destroy() correctly?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Hm, you may want to post code, but if you do, I've found that your chances of getting a helpful answer are increased greatly if:
    1) the code is small and thus quickly understandable. The code should just demonstrate the problem and do no more.
    2) the code is compilable and runnable. It requires no unposted classes other than the java core library.
    3) the code is posted using code tags.

    Best of luck!

  5. #5
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    init() will be called once when the applet is loaded, start() and stop() may be called any number of times, and finally destroy() will be called when the applet is removed from memory.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  6. #6
    Stewie is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Hm, you may want to post code, but if you do, I've found that your chances of getting a helpful answer are increased greatly if:
    1) the code is small and thus quickly understandable. The code should just demonstrate the problem and do no more.
    2) the code is compilable and runnable. It requires no unposted classes other than the java core library.
    3) the code is posted using code tags.

    Best of luck!
    The code is quite large and requires several self-created libraries so I'm not sure how much help the code itself will be. I can tell you though, that the images are loaded using Toolkit.getDefaultToolkit().getImage(). Not sure if that helps at all, but it's probably worth mentioning.

    Quote Originally Posted by OrangeDog View Post
    init() will be called once when the applet is loaded, start() and stop() may be called any number of times, and finally destroy() will be called when the applet is removed from memory.
    Well yeah, I guess I knew that already, I was wondering more along the lines of what they need to contain that may be of importance to this problem.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    The code is quite large and requires several self-created libraries so I'm not sure how much help the code itself will be.
    Myself, I don't want to see the entire code, but rather a distillation of the code that removes all non-essentials and demonstrates the problem -- an SSCCE. For details on how to create this, please check out this link here: Short, Self Contained, Correct Example
    I can tell you though, that the images are loaded using Toolkit.getDefaultToolkit().getImage(). Not sure if that helps at all, but it's probably worth mentioning.
    It doesn't help me, but we'll see; perhaps it will help others. If you get a decent response soon, then you'll know. If not, again I think that your chances of getting an answer will increase if you can create and post an SSCCE.

    Best of luck.

  8. #8
    Stewie is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default

    Java Code:
    public class Xayas extends Applet implements Runnable {
    	public static URL CodeBase;
    	public void init() {
    		ImageIO.setUseCache(false); // Tried with and without this.
    		x2DHandler.ImageLibrary = new x2DImageClass[0];
    	}
    	public void start() {
    		Xayas.CodeBase = getCodeBase();
    		x2DHandler.LoadAllImagesFromConfiguration();
    	}
    	public void run() {
    	}
    }
    
    public class x2DHandler {
    	public static int NextImageID = 1;
    	public static x2DImageClass[] ImageLibrary;
    	public static final int LIB_GRAPHICS = 1;
    
    	public static void LoadAllImagesFromConfiguration() {
    		LoadImageByFilename(x2DHandler.LIB_GRAPHICS,"loadingbg.gif");
    	}
    	public static void LoadImageByFilename(int LibNum, String name) {
    		URL Loc = null;
    		try {
    			Loc = new URL(Xayas.CodeBase,"images/" + name); // The java-forums part added itself... in the actual code, it's just the images directory.
    		} catch (Exception e) {
    			// Bad URL
    		}
    		Image Img = Toolkit.getDefaultToolkit().getImage(Loc);
    		x2DImageClass ObjImg = new x2DImageClass(LibNum,name,Img);
    		ImageLibrary = (x2DImageClass[]) functions.xArray.PushAnywhere(ImageLibrary,ObjImg);
    	}
    }
    
    public class x2DImageClass {
    	public int EncyclopediaID;
    	public int EncyclopediaType;
    	public String EncyclopediaName;
    	public Image ImageObj;
    	public BufferedImage Buffer;
    
    	public x2DImageClass(int Type, String Name, Image Img) {
    		EncyclopediaID = x2DHandler.NextImageID;
    		EncyclopediaType = Type;
    		EncyclopediaName = Name;
    		ImageObj = Img;
    		Buffer = null;
    		x2DHandler.NextImageID++;
    	}
    }
    
    public class xArray {
    	public static <E>E[] PushAnywhere(E[] GenArray, E Element) {
    		if (GenArray == null) { return null; }
    		if (Element == null) { return GenArray; }
    		int x = GenArray.length;
    		for (int i = 0; i < x; i++) {
    			if (GenArray[i] == null) {
    				GenArray[i] = Element;
    				return GenArray;
    			}
    		}
    		return PushBack(GenArray,Element);
    	}
    	public static <E>E[] PushBack(E[] GenArray, E Element) {
    		if (GenArray == null) { return null; }
    		if (Element == null) { return GenArray; }
    		int x = GenArray.length;
    		E NewArray[] = (E[])Array.newInstance(Element.getClass(),x+1);
    		System.arraycopy(GenArray,0,NewArray,0,x);
    		NewArray[x] = Element;
    		return NewArray;
    	}
    }
    Well, let's see if that helps any... Should have everything needed for an analysis.
    Last edited by Stewie; 04-23-2009 at 04:38 AM. Reason: Fixed up SSCCE.

  9. #9
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Compile errors: missing functions and x2DHandler.LIB_GRAPHICS. Xayas doesn't implement run().

    One of the main points of an SSCCE is that it compiles. Following naming conventions for classes, variables and methods would also make it easier.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  10. #10
    Stewie is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default

    Well, you have to realize that I expected it to compile. I didn't intentionally leave stuff out to make it difficult! I solely left it out in order to keep it short-and-sweet.

    I'm also not hip with the naming conventions; I've always done them my own way, as I'm generally the only one using my code. So, there's a bit of a problem there I guess.

    I have revised the above code according to the compiler errors you have provided.

  11. #11
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Still no sign of whatever "functions" is. Given that you won't have been able to compile it, how do you know that this code exhibits the problem you want to solve?

    At the moment I suspect it may be something to do with how you are storing your images. I strongly suggest using a Vector or ArrayDeque (if you want array-based queues) rather than xArray. A hybrid of generics and arrays is never a good sign. I would also expect to find all the code that is in start() to be in init(), as it involves the loading of resources. You can customise the loading animation in the html.
    Last edited by OrangeDog; 04-23-2009 at 06:58 AM.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  12. #12
    Stewie is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default

    I really have no clue what could be meant by this "functions" problem.
    EDIT: I realized what the "functions" issue is: it's the package name that contains the xArray class.

    xArray is simply a class for pushing and nullifying elements in a normal array (i.e. int[10]). As you can see, x2DHandler.ImageLibrary is just a basic array of images; it's just that xArray.PushAnywhere is a generic function to add and element to that array. I tested it with ints and Strings and had no issues... really not sure what's going on.

    Is there anything I could implement in destroy() to clear the cache of library images? Perhaps they're not loading because they think they are already loaded?
    Last edited by Stewie; 04-24-2009 at 02:11 AM.

  13. #13
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    If you want to be able to push into an array, then use a Vector. This will be much simpler and won't introduce bugs by you trying to write generic array classes yourself. You'd also get the removeAllElements() you use to clear it at destroy().
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  14. #14
    Stewie is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default

    Ahh, finally solved the problem. Turns out one of the functions was hitting a NullPointerException and halting the Rendering process. Thanks anyway!

  15. #15
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Lesson: Never silently trap exceptions when developing
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. XML Images
    By JavaWizz in forum XML
    Replies: 1
    Last Post: 10-17-2008, 11:19 AM
  2. images
    By amith in forum AWT / Swing
    Replies: 3
    Last Post: 06-27-2008, 09:38 PM
  3. images
    By amith in forum AWT / Swing
    Replies: 1
    Last Post: 05-20-2008, 11:54 AM
  4. Help with images...
    By toby in forum Java Applets
    Replies: 1
    Last Post: 08-04-2007, 06:25 AM
  5. Images in JSP
    By Daniel in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-05-2007, 07:01 AM

Posting Permissions

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