Archived

This topic is now archived and is closed to further replies.

[java] Collision mask, and creating image from array

This topic is 4984 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Answer to the first question

// create graphics context and image

Graphics graphics = getGraphics();
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
DataBufferInt data = (DataBufferInt)(image.getRaster().getDataBuffer());
// get the framebuffer

int pixel[] = data.getData();

// manipulate the pixels like this

pixel[x+width*y] = (r<<16) | (g<<8) | b;

// paint framebuffer on screen

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

Share this post


Link to post
Share on other sites
Guest 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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites