The ImageGenerator class

The ImageGenerator class is used in the Using noise to generate a terrain recipe of Chapter 3, World Building. The code to create this class is as follows:

public class ImageGenerator {

  public static void generateImage(float[][] terrain){
    int size = terrain.length;
    int grey;

    BufferedImage img = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
    for(int y = 0; y < size; y++){
      for(int x = 0; x < size; x++){
        double result = terrain[x][y];

        grey = (int) (result * 255);
        int color = (grey << 16) | (grey << 8) | grey;
        img.setRGB(x, y, color);

      }
    }

    try {
      ImageIO.write(img, "png", new File("assets/Textures/heightmap.png"));
    } catch (IOException ex) {
          Logger.getLogger(NoiseMapGenerator.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.251.128