Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-19-2009, 08:08 AM
Member
 
Join Date: Apr 2009
Posts: 7
Rep Power: 0
Stewie is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 04-19-2009, 09:36 PM
OrangeDog's Avatar
Senior Member
 
Join Date: Jan 2009
Location: Cambridge, UK
Posts: 838
Rep Power: 2
OrangeDog is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-19-2009, 10:39 PM
Member
 
Join Date: Apr 2009
Posts: 7
Rep Power: 0
Stewie is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-20-2009, 01:19 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,393
Rep Power: 8
Fubarable is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-20-2009, 04:33 AM
OrangeDog's Avatar
Senior Member
 
Join Date: Jan 2009
Location: Cambridge, UK
Posts: 838
Rep Power: 2
OrangeDog is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-20-2009, 06:36 AM
Member
 
Join Date: Apr 2009
Posts: 7
Rep Power: 0
Stewie is on a distinguished road
Default
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.

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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-20-2009, 01:39 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,393
Rep Power: 8
Fubarable is on a distinguished road
Default
Quote:
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
Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 04-22-2009, 01:07 AM
Member
 
Join Date: Apr 2009
Posts: 7
Rep Power: 0
Stewie is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 04-22-2009, 03:34 AM
OrangeDog's Avatar
Senior Member
 
Join Date: Jan 2009
Location: Cambridge, UK
Posts: 838
Rep Power: 2
OrangeDog is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 04-23-2009, 04:39 AM
Member
 
Join Date: Apr 2009
Posts: 7
Rep Power: 0
Stewie is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 04-23-2009, 06:41 AM
OrangeDog's Avatar
Senior Member
 
Join Date: Jan 2009
Location: Cambridge, UK
Posts: 838
Rep Power: 2
OrangeDog is on a distinguished road
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.
__________________
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way

Last edited by OrangeDog; 04-23-2009 at 06:58 AM.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 04-24-2009, 02:04 AM
Member
 
Join Date: Apr 2009
Posts: 7
Rep Power: 0
Stewie is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 04-24-2009, 02:32 AM
OrangeDog's Avatar
Senior Member
 
Join Date: Jan 2009
Location: Cambridge, UK
Posts: 838
Rep Power: 2
OrangeDog is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 04-29-2009, 06:21 AM
Member
 
Join Date: Apr 2009
Posts: 7
Rep Power: 0
Stewie is on a distinguished road
Default
Ahh, finally solved the problem. Turns out one of the functions was hitting a NullPointerException and halting the Rendering process. Thanks anyway!
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 04-29-2009, 04:46 PM
OrangeDog's Avatar
Senior Member
 
Join Date: Jan 2009
Location: Cambridge, UK
Posts: 838
Rep Power: 2
OrangeDog is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +2. The time now is 07:07 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org