Why doesn't this work?

I'm trying to combine 4 of the same image into a 4x4 tile. For some reason when I call drawImage() it only paints 1 of the tiles. I've found that if i move the 1 image at all, the rest is off-screen. The image itself is 34x34 pixels and the panel I'm trying to draw it on is at least 10 times larger. Here is a snip-it of my code if anyone could please look over it and give me any advice or tell me what I'm doing wrong I'd greatly appreciate it.

publicvoid paint(Graphics g){

int size = 34;

int grassx, grassy;

grassx = size * 7;

grassy = size * 0;

int spritex, spritey;

spritex = sprite.intFromDouble(spriteDimensions.getWidth());

spritey = sprite.intFromDouble(spriteDimensions.getHeight());

g.drawImage(img,0,0,size,size,grassx,grassy,grassx + size,grassy + size,

null);

g.drawImage(img,size,0,size,size,grassx,grassy,grassx + size,grassy +

size,null);

g.drawImage(img,0,size,size,size,grassx,grassy,grassx + size,grassy + size,null);

g.drawImage(img,size,size,size,size,grassx,grassy,grassx + size,grassy + size,null);

g.drawImage(sprite.getCharacters(),0,0,size,size,spritex,spritey, spritex+size,spritey + size,null);

}

img is an Image created by ImageIO.read(new File(location.toURI()))

The code only visibly draws the image at 0,0 and the sprite that is also located at 0,0.

Any help anyone can give me would be very appreciated.

[1882 byte] By [drawimagea] at [2007-11-27 7:29:18]
# 1

grassy = size * 0;

will always 0.

g.drawImage(img,0,size,size,size

the first 4 integer parameter is destination point. i think you wrongly assuming the third & fouth are width & height. they are destination coordinate, & will result only 1 line image with your code

j_shadinataa at 2007-7-12 19:09:27 > top of Java-index,Java Essentials,Java Programming...
# 2

I'm drawing a sub-image of a larger image containing all my tiles. grassx and grassy is the x,y coordination of the sub image i want to draw. If I'm not mistaken, the method is drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)

I also do know that the 3rd and 4th parameters are destination coords. I'm trying to place all 4 tiles right next to each other so it's seamless.

Message was edited by:

drawimage

drawimagea at 2007-7-12 19:09:27 > top of Java-index,Java Essentials,Java Programming...
# 3

okay... think this:

assume size = 10.

you draw from (0, size) to (size, size) which mean (0,10) to (10, 10).

here is coordinat system illustration (y base on top) :

(0, 0) (10, 0)

(0,10) (10, 10)

for first image (0, 0) to (10, 10)... okay, thats correct

second (0,10) to (10, 10) ... it will scaled the image to fit from (0, 10) to (10, 10).

thrid & fouth drawing also do same mistake.

j_shadinataa at 2007-7-12 19:09:27 > top of Java-index,Java Essentials,Java Programming...
# 4

& i also look at your fifth till eighth parameter.

they are same... you can simplify your code by creating sub image first, instead of croping them each time you need them.

fortunately, u are using BufferedImage that already support subimage.

BufferedImage img = ImageIO.read(new File(location.toURI()));

Image cropedImg = img.subImage(grassx, grassy, size, size); // here, 3rd & 4th are width & height.

g.drawImage(cropedImg, 0, 0, this); // lot simpler...

g.drawImage(cropedImg, 0, size, this);

g.drawImage(cropedImg, size, 0, this);

g.drawImage(cropedImg, size, size, this);

j_shadinataa at 2007-7-12 19:09:27 > top of Java-index,Java Essentials,Java Programming...
# 5
Oh! I see what you are saying now, thank you so much for your help. I will try this now.This worked perfectly, thank you again for your help.Message was edited by: drawimage
drawimagea at 2007-7-12 19:09:27 > top of Java-index,Java Essentials,Java Programming...
# 6
welcome
j_shadinataa at 2007-7-12 19:09:27 > top of Java-index,Java Essentials,Java Programming...