Hello Guest

image width/height

  • 15 Replies
  • 22026 Views
*

tntMaster

image width/height
« on: September 04, 2003, 22:42:21 »
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

*

Offline bedelf

  • ***
  • 196
image width/height
« Reply #1 on: September 04, 2003, 22:52:02 »
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

image width/height
« Reply #2 on: September 04, 2003, 22:59:59 »
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.

*

Offline bedelf

  • ***
  • 196
image width/height
« Reply #3 on: September 04, 2003, 23:04:40 »
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

image width/height
« Reply #4 on: September 04, 2003, 23:19:50 »
Thanks.

You mean MediaTracker?

*

Offline bedelf

  • ***
  • 196
image width/height
« Reply #5 on: September 04, 2003, 23:23:24 »
Ya.

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

*

Anonymous

image width/height
« Reply #6 on: September 04, 2003, 23:43:12 »
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;
  }

*

Offline bedelf

  • ***
  • 196
image width/height
« Reply #7 on: September 05, 2003, 00:03:29 »
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

image width/height
« Reply #8 on: September 05, 2003, 00:40:39 »
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...

*

Offline bedelf

  • ***
  • 196
image width/height
« Reply #9 on: September 05, 2003, 01:30:53 »
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.

*

Offline oNyx

  • ***
  • 177
  • 弾幕
image width/height
« Reply #10 on: September 05, 2003, 09:33:15 »
>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.
Code: [Select]

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

image width/height
« Reply #11 on: September 05, 2003, 11:10:33 »
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

image width/height
« Reply #12 on: September 05, 2003, 12:46:02 »
Let me speculate:
Is it to move the character horizontally? What if you want to apply a movement of more than 1 pixels?

image width/height
« Reply #13 on: September 05, 2003, 15:00:45 »
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

image width/height
« Reply #14 on: September 05, 2003, 17:31:08 »
Cool, thanks :)