我想將畫布分成8個垂直的黑色和白色條紋。我寫了一個方法changeValue,每次循環執行時都改變顏色的值,但畫布完全是白色的。我認爲我的changeValue方法沒有按照假設做,但我無法解釋原因。使用RGB將畫布分成多個條紋
到目前爲止,我得到這個:
public class Stripe {
public boolean switch = false;
private void stripes(int[] pixels, int width, int height) {
// TODO set some values here
int counter = 1;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int pos = y * width + x;
int r, g, b;
// TODO code for the flag.
r = 255;
g = 255;
b = 255;
for (int i = 0; i < 8; i++) {
if (x < (counter * (width - 1))/8) {
r = changeValue();
g = changeValue();
b = changeValue();
}
counter++;
switch ^= true;
}
pixels[pos] = 0xFF000000 | (r << 16) | (g << 8) | b;
}
}
}
public int changeValue() {
if (switch) {
return 255;
} else {
return 0;
}
}
}
我應該不要寫8如果語句爲8條,但本作例子是意大利國旗的3個豎條紋但實際工作的代碼:
private void flagItalian(int[] pixels, int width, int height) {
// TODO set some values here
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int pos = y * width + x;
int r, g, b;
// TODO code for the flag.
r = 255;
g = 0;
b = 0;
if (x < 2 * (width - 1)/3) {
r = 255;
g = 255;
b = 255;
}
if (x < (width - 1)/3) {
r = 0;
g = 255;
b = 0;
}
pixels[pos] = 0xFF000000 | (r << 16) | (g << 8) | b;
}
}
}
更換我不知道,如果你可以爲一個變量使用交換機作爲一個名字,因爲它是在switch語句中也使用... – Lucurious