【发布时间】:2015-01-11 06:35:09
【问题描述】:
出于某种原因,我可以使用 setRGB 更改缓冲图像,但不能使用光栅中的实际 int 数组:
这行得通
BufferedImage img = new BufferedImage(32, 32, BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < 32; y++) {
for (int x = 0; x < 32; x++) {
int gray = (int) (MathUtil.noise(x, y) * 255); //I have tested the noise function, and know it works fine
img.setRGB(x, y, gray << 16 | gray << 8 | gray);
}
}
这不是
BufferedImage img = new BufferedImage(32, 32, BufferedImage.TYPE_INT_RGB);
int[] data = ((DataBufferInt) img.getData().getDataBuffer()).getData();
for (int y = 0; y < 32; y++) {
for (int x = 0; x < 32; x++) {
int gray = (int) (MathUtil.noise(x, y) * 255); //I have tested the noise function, and know it works fine
data[x + y * 32] = gray << 16 | gray << 8 | gray;
}
}
噪声函数:
public static float noise(int x, int y) {
int n = x + y * 57;
n = (n << 13) ^ n;
return Math.abs((1.0f - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f));
}
编辑
没关系,我修好了。我需要使用 getRaster :P
【问题讨论】:
-
@5gon12eder 没有错误,图像应该是不连贯的噪声,但结果是黑色的
-
你为什么要重新发明轮子(通过你的
noise函数)? -
@specializt 因为我在java中没有找到真正做到这一点的方法(每次都没有初始化一个新的随机数)
-
嗯...这就是他们的关键字
static final是为了... -
@specializt "如果没有这种技能,程序员可能永远不会成为开发人员" 抱歉,但我有我的应用程序的确切目标。我想在我创建它之前我已经知道我将要使用这个方法。我知道它会满足我的需求,并且不会妨碍我成为一名开发人员。
标签: java bufferedimage