Reputation: 436
For some reason, this code will not render anything. Can someone explain to me why it doesn't do anything?
import java.awt.*;
import java.applet.*;
public class TEST extends Applet {
public void paint(Graphics g) {
int xSize = 255;
int ySize = 255;
byte R, G, B;
for(int x = 0; x < xSize; x++) {
for(int y = 0; y < ySize; y++) {
R = (byte) (x % y);
G = (byte) (y % x);
B = (byte) (y);
Color pixel = new Color(R, G, B);
g.setColor(pixel);
g.fillRect(x, y, 1, 1);
}
}
}
}
Upvotes: 1
Views: 76
Reputation: 436
I figured it out, it is trying to put R G and B values above 255. I just added %255 at the end of each one to fix it. Thanks for your help guys.
Upvotes: 0
Reputation: 159774
Its not possible to get the modulus of a number relative to 0
e.g. here
R = (byte)(x%y);
in the first iteration. You're probably seeing an ArithmeticException
occur at this point. You could simply start from 1
:
red = (byte) (x % (y + 1));
green = (byte) (y % (x + 1));
Upvotes: 1