TAGS :Viewed: 19 - Published at: a few seconds ago

[ How can I flip an image using a nested for loop? ]

I want to make a program that uses x and y counter variables to read pixels from the bottom right corner all the way to the top left corner from an image. The program would assign the bottom right pixel to the top left and would assign every pixel in this way which would flip the image.

My main code so far:

Scanner reader = new Scanner(System.in);
APImage image = new APImage("MrB.JPG");
image.draw();

int width = image.getWidth();
int height = image.getHeight();
APImage brandnew = new APImage(width, height);

int y = 0;
for(y < height; y--;)
{
    int x = width - 1
    for(x >= 0; x++;)
    {
        Pixel opixel = image.getPixel(x, y);
    }
}

System.out.println("Press the return key to continue");
reader.nextLine();
brandnew.draw();

Any help will be appreciated, cheers.

Answer 1


This should do it:

for(int x = 0; x < width; y++){
    for(int y = 0; y < height; y++){
        flipped.setPixel(width - 1 - x, height - 1 - y, original.getPixel(x,y));
    }
}