Book Image

JMonkeyEngine 3.0 Cookbook

By : Rickard Eden
Book Image

JMonkeyEngine 3.0 Cookbook

By: Rickard Eden

Overview of this book

Table of Contents (17 chapters)
jMonkeyEngine 3.0 Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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);
      }
    }
}