Skip to main content
gamedev.net
🔒 Locked

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

Started by CodeMachine Sep 20, 2006 at 4:11 PM 5 replies 1k views
Original Post
CodeMachine
CodeMachine
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
CaptainJester
CaptainJester
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);
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
shmoove
shmoove
MediaTracker needs a Component instance for it's constructor. Maybe you can pass one as an argument to Text.load().

shmoove
CodeMachine
CodeMachine
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
CaptainJester
CaptainJester
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.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
shmoove
shmoove
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.
CodeMachine
CodeMachine
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.