image width/height

Started by tntMaster, September 04, 2003, 22:42:21

Previous topic - Next topic


I'm making an old applet of mine into an application and I can't get the width or height of the image.
The old image.getWidth(this) doesn't work.
I changed it to image.getWidth(null) but still doesn't work.

Any ideas?



What number ARE you getting then? 0? Seems to me getWidth(null) should work. Are you sure your images are fully loaded? Check the docs against whatever feedback you are getting.


I'm getting -1 but the image is loaded. I mean it's painted fine. I just can't get its dimensions. I don't know, maybe there are things that I am not aware of, as fas as applications are concerned. I mean that thing works just fine in applets but not here...

Maybe I should use BufferdImage?

Quote from: "bedelf"What number ARE you getting then? 0? Seems to me getWidth(null) should work. Are you sure your images are fully loaded? Check the docs against whatever feedback you are getting.


From ye olde docs:

   the width of this image, or -1 if the width is not yet known.

Mind pasting your image loading code? That is suspect #1 for me right now.

BufferedImage should make no difference.



You mean MediaTracker?



You could also do a from the javax.imageio package and see if that makes your problem go away.


Oops! You meant "posting" there right?
>> Mind pasting your image loading code?

I totally misunderstood :)

Basically it's a huge applet. Although I got a small sample to work I can't do it for the whole. Here's an example of how I load an image:

   MediaTracker thetracker = new MediaTracker(null);

   Image i_backImage =
      (new ImageIcon(getClass().getResource("gfx/level1.jpg"))).getImage();
// other images follow plus check if loaded...

 private final Image getSprite(Image image, int i, int width, int height)
      int max_frames_per_line = image.getWidth(null) / width;
// ....
     return sprite;


Your still trying to convert this to an applicaton right?

You may need to pass the frame to mediatracker instead of null.

Ye olde docs:


public MediaTracker(Component comp)

   Creates a media tracker to track images for a given component.

   comp - the component on which the images will eventually be drawn


      MediaTracker mt = new MediaTracker( this/frame reference );
      Toolkit tk = Toolkit.getDefaultToolkit();
      Image i = tk.getImage( "blah.gif" );
      mt.addImage( i, 0 );
      try {
      } catch ( Exception e ) {
         System.out.println( e.toString() );
      int w = i.getWidth( null );

Should work. Again, you could also just do; and be done with it I think.


Thanks a lot! you were right about the frame in MediaTracker.
I got my game running now, but I just can't use this to enable the key listener:
(cannot resolve symbol)

Maybe you could help me with that too? :)

PS: I am not a complete idiot, I have created some quite nice game applets in the past, it's just that making an application out of an applet is a bit more tedious than I imagined...


No worries.

I'm not sure what your trying to do now though. Did you just want key events? Documentation/sun tutorials are your friends.

Ye olde docs:

protected final void enableEvents(long eventsToEnable)
   Enables the events defined by the specified event mask parameter to be delivered to this component.
   Event types are automatically enabled when a listener for that event type is added to the component.
   This method only needs to be invoked by subclasses of Component which desire to have the specified event types delivered to processEvent regardless of whether or not a listener is registered.

This doesn't really sound like what you want/proper. Have your class implement KeyListener, frame.addKeyListener( this );, and implement KeyTyped/Pressed/Released() instead.



Well... that works but I won't use it unless it's for a 4k competition and I like to save ~100 bytes :>

KeyListener is nicer.
... implements KeyListener
int [] controls = new int[256];


public void gameLoop()

public void keyPressed(KeyEvent ke)
	controls[ke.getKeyCode()&0xff] = 1;

public void keyReleased(KeyEvent ke)
	controls[ke.getKeyCode()&0xff] = 0;
public void keyTyped(KeyEvent ke){}


Thanks you guys! I let enableEvents() go, as I couln't get it to work no matter what :(
Basically I was using it in appltes, because it's supposed to be as low level as it can get in input reading, but anyway...

Onyx, would you mind explaining a bit the line:

Thank you :)


Let me speculate:
Is it to move the character horizontally? What if you want to apply a movement of more than 1 pixels?


x = (controls[KeyEvent.VK_RIGHT] - controls[KeyEvent.VK_LEFT]) * 2 ;


It's a simple but effective hack.  If you want to do anything clever with keyboard movement you'd be better off designing something more comprehensive.  For now it may do the trick, though!
ellomynameis Charlie Dobbie.