Cutting an image into a smaller image?

I have made a tilebased quix game and I would like to split an image up into smaller pictures of 4x4 pixels and store them in an Image[][] array.

Does anyone know a handy way of doing this?

I found this post about if but I dont get the point of it:
http://forums.java.sun.com/thread.jspa?threadID=514205

(Sorry bout the little incedent I had with lazyness 'bout the AffineTransform post I had a week ago. No bad feelings, hu :slight_smile: )

BufferedImage has a getSubImage (or something similar) that you can use to slice up an image into chunks. But don’t use the sub images as actuall tile images to draw to screen - they actually just reference the original image so the drawing speed is rubbish. Create yourself a new 4x4 image and draw the sliced image data into it.

Incidentally, 4x4 is way too small for tiles if you’ve actually drawing them - the draw overhead will be massive. 32x32 is about right for a 640x480 screen.

Well I wanted to have some sort of big image to apear on all the dark gray lines, another one for alle the gray spots and so on. You say that wouldn’t be possible?

a link for my game:
http://stephan.syska.dk/javating/quix2/quix2.zip

There is a batch file created to run the jar file with this line:

java -classpath quix2.jar main.RunMe

Well I would like to try to use that method (getSubImage) from BufferedImage.

Do you know how to convert an image from Image to BufferedImage?


BufferedImage newImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGBA);
Graphics g = newImage.getGraphics();
g.drawImage(originalImage,0,0,null);

Kev

wel, Kevglass how would suggest I should cut an image down to a 2D array?

I found subimage a little slow in the past (I had to cut images for Mini Adventure). I ended up doing something like…


Image originalImage = (get a 256x256 image here)
int xsections = originalImage.getWidth(null) / TILE_SIZE;
int ysections = originalImage.getHeight(null) / TILE_SIZE;

BufferedImage[][] tiles = new BufferedImage[xsections][ysections]

for (int x=0;x<xsections;x++)
{
  for (int y=0;y<ysections;y++)
  {
     tiles[x][y] = new BufferedImage(TILE_SIZE,TILE_SIZE,BufferedImage.TYPE_INT_ARGB);
     Graphics g = tiles[x][y].getGraphics();
     g.drawImage(originalImage,-x*TILE_SIZE,-y*TILE_SIZE,null);
  }
}

This draws the whole original image into the smaller buffered images but because of clipping just causes a section of the original image to be drawn (hence the offsets on the image draw). It seemed to me that drawing images was quicker than extracting sections of image. Strange I know, but it matched the results I got.

Kev

Well lets say I have an image that is 300x300 called myImage. Then I want a new image called myCenter which should contain the center 10x10 pixels of image01.

Is there a way of breaking this down then? I guess that would solve my entire problem.

(I have a hard time getting the bufferedImage thing to work.)

Well it doesnt matter. I wont be needing it for this project anyway. I found another, less cool, but more simple way of boosting my graphics. thankyou anyway.