Question

I am trying to make a program in Java that uses Perlin noise to make a black and white height map. I tried using the code from here to implement the noise. I used the code below to try to make the noise make a height map but what I get is something like this instead of getting a height map.

BufferedImage img;
img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB );

int[] pixel = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();

for(int i = 0; i < (WIDTH*HEIGHT); i++)
{
    pixel[i] = (int) perlin.PerlinNoise(i, i);   
} 
Was it helpful?

Solution

You can use the BufferedImage.TYPE_BYTE_GRAY as follows: Create you noise-array of type int[] where the numbers in the array should be between [0,255]. Let's assume this pixel array is called pixelData, then the following should work

BufferedImage img = new BufferedImage(WIDTH, HEIGHT, 
                                     BufferedImage.TYPE_BYTE_GRAY);
img.getRaster().setPixels(0, 0, WIDTH, HEIGHT, pixelData);

File output = new File("image.jpg");
try {
  ImageIO.write(img, "jpg", output);
} catch (IOException e) {
  e.printStackTrace();
}

If you want to have a complete example, then please look at the following question: Issue with Perlin Noise in Java

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top