drawImage not working

Hello there, Java-Gaming people. I’m new to the forum, so maybe a little introduction is in order. So I’m this guy from the Netherlands, I have programmed in Gamemaker quite extensively, with which I started at a young age. Mind you I didn’t do the Drag and Drop stuff, but “proper” coding, and I have in gamemaker constructed some stuff that I think is pretty nifty, I have for example at a point programmed an online top down shooter from scratch (as far as that goes in gamemaker). However, this year at university (First year Software Science), I was introduced to Java, and after some tests I found out that it is an insane amount faster then Gamemaker, so I decided to move to Java, and I have succeeded(sp?) in creating a simple mario platformer, with image animations, a particle system and some gibberish, but I have now moved on to a new more structured project (An isometric RTS), BUT somewhere something must be going wrong because it does not draw images.

Tl;dr:
This is an extract from my script, this is the image initialization:

public class Tile extends GameImage
{
  int x,y;
  int type;
  TileGrid grid;
  Image image=Toolkit.getDefaultToolkit().getImage("Grass.png");

And this is the “draw” script, which I know to work, since it does draw the lines, but it does not draw the image, and I have no idea why.

  public void draw(Graphics2D g)
  {
    int dx,dy;
    dx=grid.X(x);
    dy=grid.Y(y);
    g.drawImage(image,dx-16,dy-8,null);    
    g.drawLine(dx-16,dy,dx,dy-8);
    g.drawLine(dx,dy-8,dx+16,dy);    
    g.drawLine(dx-16,dy,dx,dy+8);
    g.drawLine(dx,dy+8,dx+16,dy);        
  }

Help would be appreciated (:, in the meanwhile this forum has quite some resources and information to absorb.

What are the image’s width and height? If they are both -1, then that means your image was not found and an empty Image object was returned.

Dimensions are 32x16, but that is not stated anywhere in the code, should the program somehow know these values?

EDIT:
I added System.out.println(image.getWidth(null));to test what you said, and it returns -1, so you seem to have nailed the problem. Also that was a pretty fast response :P. So how would I fix it loading an empty image?

You get the Image’s width and height by doing:


System.out.println(image.getWidth(null) + " " + image.getHeight(null));

I fixed the problem by replacing the getImage URL with this:
Image image=Toolkit.getDefaultToolkit().getImage(“C:/Users/Bob/Desktop/Java game engine/Skeleton/Grass.png”);

How would I go about loading Grass.png from my current path? Because it is a bit silly to do it like this.

It is best to use the Class.getResource(String) method. Using “grass.png” will look for the file inside the package (folder) of that Class. Using a forward slash ("/") at the beginning will look from the root of your package hierarchy.

So let’s say I have a class called MyClass inside package com.ra4king.mypackage, then MyClass.class.getResource(“grass.png”) or myClassInstance.getClass().getResource(“grass.png”) will look for that file inside the mypackage folder. The same file will be found if I do MyClass.class.getResource("/com/ra4king/mypackage/grass.png") or the getClass() way.

All’s working smooth now, thanks for your quick and usefull responses!

Glad to help :slight_smile:

Don’t forget to Appreciate (click the Appreciate button to the right of the Quote button) anyone that helps you on this forum. Keeps moods up ;D

ra4king, you’re a gratuitous medal whore! :stuck_out_tongue:

;D

Where is this appreciate button you speak of?

http://img197.imageshack.us/img197/7690/nobutton.png

EDIT: it just appeared, maybe you need 5 posts to be able to appreciate?

There’s a reason he ranks #1 in awkwardness!
http://www.java-gaming.org/index.php?action=social
Oh how I love my closed source queries.

I may be loud, but you can’t ignore me ;D

ra4king -> Post visibility -> Hide all posts :stuck_out_tongue:

But I know you won’t do that because you <3 me :persecutioncomplex: