Java: 我的高度图生成器只写二进制
Java: My height map generator only writes binary
所以今天我开始了一个新项目。我想在 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();
}
}
}
代码很简单,下一步我打算试试perlin noise。但首先我需要解决这个问题:
Generated Heightmap
heightmap.png中的像素要么全白,要么全黑。图像中没有灰色,这在高度图中当然是必需的。有谁知道我做错了什么?
是BufferedImage.TYPE_BYTE_GRAY
部分吗?如果是这样,我应该改用什么?
经过朋友的指点,我找到了解决办法。
我使用 BufferdImage.TYPE_INT_RGB
而不是 BufferedImage.TYPE_BYTE_GRAY
。所以这确实是我出错的地方。我还添加了对象 Color randomColor
,其中 RGB 值都共享相同的整数,值从 0 到 255。然后在 BufferedImage.setRGB
中,我使用颜色代码 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();
}
}
}
现在 heightmap.png 给出了我预期的结果:Heightmap.png
所以今天我开始了一个新项目。我想在 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();
}
}
}
代码很简单,下一步我打算试试perlin noise。但首先我需要解决这个问题: Generated Heightmap
heightmap.png中的像素要么全白,要么全黑。图像中没有灰色,这在高度图中当然是必需的。有谁知道我做错了什么?
是BufferedImage.TYPE_BYTE_GRAY
部分吗?如果是这样,我应该改用什么?
经过朋友的指点,我找到了解决办法。
我使用 BufferdImage.TYPE_INT_RGB
而不是 BufferedImage.TYPE_BYTE_GRAY
。所以这确实是我出错的地方。我还添加了对象 Color randomColor
,其中 RGB 值都共享相同的整数,值从 0 到 255。然后在 BufferedImage.setRGB
中,我使用颜色代码 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();
}
}
}
现在 heightmap.png 给出了我预期的结果:Heightmap.png