2016-10-23 15 views
0

그래서 오늘 새 프로젝트를 시작했습니다. 나는 자바 간단한 높이 맵 생성기를 만들고 싶어, 그래서 나는 다음과 같은 시도 :Java : 내 높이 맵 생성기는 바이너리 만 기록합니다.

import java.awt.image.BufferedImage; 
    import java.io.File; 
    import java.io.IOException; 
    import javax.imageio.ImageIO; 

    public class Heightmap { 


    public static int width = 200; 
    public static int height = 200; 

    public static void main(String[] args) { 

     BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); 
     for(int x = 0; x < width; x++){ 
      for(int y = 0; y < height; y++){ 
       bufferedImage.setRGB(x, y, (byte)(Math.random() * 256 + 128)); // + 128 because byte goes from -128 to 127 
      } 
     } 

     File outputFile = new File("heightmap.png"); 
     try { 
      ImageIO.write(bufferedImage, "png", outputFile); 
     }catch (IOException ioex){ 
      ioex.printStackTrace(); 
     } 
    } 
} 

코드는 매우 간단합니다, 나는 다음 단계로 소음 펄린 시도 할 계획이다. 중 하나를 완전히 흰색, 또는 완전히 검은 색 heightmap.pngGenerated Heightmap

픽셀 :하지만 먼저 나는이 문제를 해결해야합니다. 이미지에는 회색이 없으며 하이트 맵에는 물론 필요합니다. 아무도 내가 뭘 잘못했는지 알아?

BufferedImage.TYPE_BYTE_GRAY 부분입니까? 그렇다면 대신 무엇을 사용해야합니까?

답변

1

친구가 올바른 길을 걷고 나면 해결책을 찾았습니다.

BufferedImage.TYPE_BYTE_GRAY 대신 BufferdImage.TYPE_INT_RGB을 사용했습니다. 그래서 내가 실제로 잘못 들어간 곳입니다. 또한 Color randomColor 개체를 추가했습니다. RGB 값은 모두 0에서 255까지의 동일한 정수를 공유합니다. 에서 randomColor의 색 코드를 사용합니다 (R, G, B = 255는 #FFFFFF를 제공합니다. 화소의 값 (X, Y))을 백색 :

import java.awt.Color; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 

public class Heightmap { 


public static int width = 200; 
public static int height = 200; 

public static void main(String[] args) { 

    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
    for(int x = 0; x < width; x++){ 
     for(int y = 0; y < height; y++){ 
      int randomValue = (int)(Math.random() * 256); 
      Color randomColor = new Color(randomValue, randomValue, randomValue); 

      bufferedImage.setRGB(x, y, randomColor.getRGB()); 
     } 
    } 

    File outputFile = new File("heightmap.png"); 
    try { 
     ImageIO.write(bufferedImage, "png", outputFile); 
    }catch (IOException ioex){ 
     ioex.printStackTrace(); 
    } 




} 

}

은 이제 I heightmap.png은 예상했던 준다 : Heightmap.png