image width/height

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

Previous topic - Next topic

tntMaster

Hi,
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?

Thanks,
-tnt

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.

tntMaster

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.

bedelf

From ye olde docs:

Returns:
   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.

tntMaster

Thanks.

You mean MediaTracker?

bedelf

Ya.

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

Anonymous

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();
   thetracker.addImage(i_backImage,imageTotal);
   imageTotal++;
// 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;
 }

bedelf

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:

MediaTracker

public MediaTracker(Component comp)

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

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

So..

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

Should work. Again, you could also just do ImageIO.read(file); and be done with it I think.

tntMaster

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:
enableEvents(AWTEvent.KEY_EVENT_MASK);
(cannot resolve symbol)

Maybe you could help me with that too? :)

Thanks.
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...

bedelf

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:

enableEvents
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.

oNyx

>enableEvents(AWTEvent.KEY_EVENT_MASK);

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];

...
bla.addKeyListener(this);
...

public void gameLoop()
{
[blabla]
if(controls[KeyEvent.VK_ESCAPE]==1)
	runRenderLoop=false;

x=controls[KeyEvent.VK_RIGHT]-controls[KeyEvent.VK_LEFT];
[blabla]
}
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){}

tntMaster

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:
x=controls[KeyEvent.VK_RIGHT]-controls[KeyEvent.VK_LEFT];

Thank you :)

tntMaster

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

cfmdobbie

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

:wink:

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.

tntMaster