我正在尝试用Java中的RGB创建每种颜色的图像

时间:2018-12-07 10:33:28

标签: java loops for-loop colors

因此,我正在尝试用Java创建一个图像,该图像代表RGB光谱中的每种颜色。但是,我是Java的新手(刚刚完成了我的第一门编程课程,《 Java入门》),因为遇到rgb的循环而遇到麻烦在(3,3,3)下面的代码中返回了最终值的颜色。我需要一种在图片的每个索引处返回每种颜色的方法。因此,在图片0的索引(0,0,0)处,索引1 (0,0,1)以此类推。现在是我的代码:

public static Color rainbowColor(int colorValue, int x){
Color newColor = null;
for(int r = 0; r < colorValue; r++){
  for(int g = 0; g < colorValue; g++){
    for(int b = 0; b < colorValue; b++){
      System.out.println(x + ": " + r + "--" + g + "--" + b);
      newColor = new Color(r,g,b);
    }
  }
}
return newColor;
}

public static Color[] rainbowPixels(int colorValue, int size){
Color[] newArray = new Color[size];
for(int x = 0; x < size; x++){
  newArray[x] = Rainbow.rainbowColor(colorValue, x);
}
return newArray;
}

public static Image rainbow(){
int colorValue = 4;
int size = colorValue*colorValue*colorValue;
int width = (int)Math.sqrt(size);
int height = (int)Math.sqrt(size);
Color[] pixels = Rainbow.rainbowPixels(colorValue, size);
System.out.println(size + "," + colorValue);
return new Image(width, height, pixels);
}

现在,将colorValue设置为4只是为了我正在处理一个小的图像,并且在Powershell中运行它并不会花费很长时间。另外,这些都在名为Rainbow.java的类中。

0 个答案:

没有答案