This site requires JavaScript, please enable it in your browser!
Greenfoot back
TheGoldenProof
TheGoldenProof wrote ...

2018/9/3

Iterate through pixels

TheGoldenProof TheGoldenProof

2018/9/3

#
I'm trying to iterate through all the pixels and change their color. I know the way to do this is to make an array and then do something like this:
int width = 500;
int height = 500;
someType[] pixles = new someType[width*height];

for (int i = 0; i < height; i++) {
    for (int j = 0; j < width; j++) {
        n = j*width + i;
        someType[n] = //however you get a pixel;
    }
}
I don't know what type I should make the list. Is there a Pixel type? Basically, what I need to know is what kind of list I need to make, how I can get a pixel at a location, and how I can set that pixel.
danpost danpost

2018/9/3

#
TheGoldenProof wrote...
Basically, what I need to know is what kind of list I need to make, how I can get a pixel at a location, and how I can set that pixel.
The World instance method getBackground returns a GreenfootImage object that contains the array you are trying to create. You can then use the GreenfootImage instance method setPixelAt to change a screen pixel by setting a color to any pixel of that image. You can acquire the Color object assigned to a pixel by using the getColorAt method on a GreenfootImage instance.
You need to login to post a reply.