[java] Collision mask, and creating image from array

Started by
5 comments, last by Eventh 19 years, 12 months ago
Im making a lemming game, and want to use a collision mask "image" for collision detection, ala worms. I have a material-interface wich contains 8 diffrent materials a pixel can be (byte), and each material have a int describing its color (int rgb). I have a 2-dimension-array, byte[width][height] for the pixels on the screen. When debugging i want to print the pixels in the right color on my screen (swing, jpanel). Since graphics didnt have a way to draw/fill a point/pixel, i tried with creating rectangles with the size 1x1 and graphics.setColor() before i fill the rectangle:

for (..) {
 for (..) {
  g.setColor( new Color( int[][] ) )
  g.fillRect( x, y, 1, 1);
 }
}
 
Doing it this way, it was very slow and used 99% of my cpu, since i call the paint method every 80 ms, and does this each time.. I then tried to make a Image with ImageIcon(byte[]), and also testet with bufferedImage/Raster, but i had problems getting it to work. My question is this: 1: What is the best way to display my array of pixels? Is there 2: Is there any tutorials or codes i can look at who does this? (I tried gooogling for a few hours, but didnt find anything usefull) 3: Any good hints for my collision mask? [edited by - Eventh on April 22, 2004 7:38:51 AM] [edited by - Eventh on April 22, 2004 7:39:26 AM] [edited by - Eventh on April 22, 2004 7:39:55 AM]
Advertisement
You should use an 1-dimensional array instead of a 2-dimenstioan one:

the2DArray=new int[width][height];
The 1D array can contain the 2D array like this:
the1DArray[(width*y)+x]=the2DArray[x][y];

Now you can convert the 1D-array to a java.awt.Image using a java.awt.image.MemoryImageSource:
MemoryImageSource src=new MemoryImageSource(width,height,the1DArray,0,width);
Image img=Toolkit.getDefaultToolkit().createImage(src);

You can draw the image on the graphics.
Don't forget the alpha value of your colors must be FF:
You won't see 0x000000 (it is black, but 100% transparent)
You will see 0xff000000 (it is black and 100% visible)

Concerning your other question, I don't know what a collision mask is.

[edited by - koroljov on April 22, 2004 12:16:52 PM]
Why do my programs never work on other computers?
Thanks for the reply, with a collision mask i mean a bitmap or something like that. Not realy sure what its called.
Answer to the first question
// create graphics context and imageGraphics graphics = getGraphics();BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);DataBufferInt data = (DataBufferInt)(image.getRaster().getDataBuffer());// get the framebufferint pixel[] = data.getData();// manipulate the pixels like thispixel[x+width*y] = (r<<16) | (g<<8) | b;// paint framebuffer on screengraphics.drawImage(image,0,0,w,h,null);

Bufferedimage is much faster then MemoryImageSource on JVM versions 1.2+.

A suggestion. Add a similar example as the one above as this question comes up from time to time..
quote:
A suggestion. Add a similar example as the one above as this question comes up from time to time..

I meant to the FAQ ofcoarse
quote:Original post by Anonymous Poster
Bufferedimage is much faster then MemoryImageSource on JVM versions 1.2+.

Thanks..

quote:Original post by Anonymous Poster
quote:
A suggestion. Add a similar example as the one above as this question comes up from time to time..

I meant to the FAQ ofcoarse


I''ll think about it. I''d actually prefer to keep the FAQ code-free though. Links, however, are acceptable...

http://chaos.webhop.org

This topic is closed to new replies.

Advertisement