• Advertisement
Sign in to follow this  

[java] Loading bar

This topic is 4171 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've looked everywhere I can't seem to find any tutorials on making a loading bar in Java to show how much of the media has been loaded etc. Could anyone point me in the right direction? I'm sure it has something to do with MediaTrackers but whenever I've tried something it's failed.

Share this post


Link to post
Share on other sites
Advertisement
A progress bar is basically just a rectangle which is gradually filled so

DrawRectangle(0, top, (width * progress) / maxProgress, height, color);

should do the trick. This is, as you can see, not java specific. You said you didn't want swing so I guess you're drawing the gui parts yourself?

Share this post


Link to post
Share on other sites
Yes. I know how to draw the rectangle that's fine. It's just how to tell whether an image has loaded and increasing it without the whole Applet freezing that's the trouble. I wanted to get a more personal feel with my GUI so I am writing those parts myself. An example of the sort of thing I would like is like the one in RuneScape (Which I happen to be playing at the time :P)

Share this post


Link to post
Share on other sites
I think he updates based on each image loaded, not on how much of an image is loaded.

If that is what you want, then just use MediaTracker and a separate id for each image.


MediaTracker mt = new MediaTracker(this);
for(int i=0;i<numImages;i++) {
image = getImage("somfile.jpg");
mt.addImage(image, i);
}

for(int i=0;i<numImages;i++) {
while(!mt.checkID(i)) {
try {
//required to keep this thread from locking others
Thread.sleep(20);
}
catch(InterruptedException e) {}
}
//updateProgress here
}




If you want to use ImageIO instead of the old way:

for(int i=0;i<numImages;i++) {
image = ImageIO.read("somefile");
//updateProgress here
}



Since ImageIO blocks until the image is loaded, you don't need MediaTracker.

Share this post


Link to post
Share on other sites
Thank you. I will incorporate this into my Sprite class. I've never used ImageIO before it seems to be a lot simpler...

Share this post


Link to post
Share on other sites
Quote:
Original post by Dom_152
Thank you. I will incorporate this into my Sprite class.


On your Sprite class?!? Don't you think this responsibility belongs to another class? (i.e a GameState? =)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement