• Advertisement
Sign in to follow this  

[java] Static class - Load image (width/height problem)

This topic is 4172 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

Hi I've problem finding the width/height of an image loaded within a static class of mine. A small example:
public class Text {
 private static Image[] _characters = null;
 
 private static void load() {
  _characters = new Image[26];
 
  // Load image (containing subimages a-z) from drive.
  Image img = Toolkit.getDefaultToolkit().getImage("characters.png");
 
  // Clip and save each character to '_characters'.
  // =====> How do I retrieve the width and height of "img" ?? <=====
  // I need somehow to wait for the loading above to finish.
  // MediaTracker can't be use within a static class, or am I wrong?
 }
 
 private static void drawString(Graphics g, String s, int x, int y) {
  if(_characters==null)
   load();
 
   // Draw string to screen.
   ...
  }
}

Kind Regards

Share this post


Link to post
Share on other sites
Advertisement
MediaTracker


Image img = Toolkit.getDefaultToolkit().getImage("picture.gif");
try {
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(img, 0);
tracker.waitForID(0);
} catch (Exception e) {}
int width = img.getWidth(this);
int height = img.getHeight(this);

Share this post


Link to post
Share on other sites
MediaTracker needs a Component instance for it's constructor. Maybe you can pass one as an argument to Text.load().

shmoove

Share this post


Link to post
Share on other sites
Hi Captain and shmoove

Yes, I tried to use MediaTracker and as you say, shmoove, it doesn't work without a component instance.

It worked perfectly when I sent "this" (JPanel) as the component instance, but I needed to pass this reference in several steps. And that's ugly :)

I make the call within another class that also does not inherit a super-class.
And to pass this component reference through out the "whole" application is kind of ugly, isn't it? :)

So shmoove, it works. Thanks!

But couldn't it be solved another way?
Without having to pass a component reference as parameter through every call that indirectly call drawString(...)?

Kind Regards

Share this post


Link to post
Share on other sites
Two options.

1. Implement the ImageObserver interface and pass that into the MediaTracker.

2. Use javax.imageio.ImageIO.read(image file). It blocks until the image is loaded.

Share this post


Link to post
Share on other sites
You could have a component that's statically available (like making your JPanel a singleton or something), or create a dummy Component in load() to use with the MediaTracker. But to be honest with you it sounds like you need to rethink your application's design.

shmoove

Disclaimer: Unless there's another class that can do what MediaTracker does. Standard Java isn't really my thing.

Share this post


Link to post
Share on other sites
Thanks Captain, I will check out the alternatives...

Shmoove, how would you implement the functionality to draw userdefined (image) text?
I have several images (contains subimages a-z), and want to use these to draw text-strings to a Graphics object.

Should I create an instance of my "Text" class instead of having it static? If you do that, you may end up with A LOT of instances. Isn't a static solution better? With a static one you only read and construct the images (a-z) once.

Or should I make the class nonstatic and only having the internal a-z images static?

I think the best solution is to have it all public, then you can access it everywhere without having to create a instance each time...

Or am I wrong?

(
Im creating a game, and instead of having hundreds of lines of code in the paint-method, I've split up the drawing to several classes (that handles a unique thing (like menu, highscore, background etc)).

Within the main-paint I do:
_highscore.draw(g,...);
_background.draw(g,...);
etc...

And in several of these classes I use my "Text" class above (drawString(...))
)

Is this way of design bad?

Kind Regards

Share this post


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

  • Advertisement