문제

this a code for a method that flips a square part of an image at a location that is passed from another java files. Currently this is one of my attempts to get it to work. Various other attempts have only allowed the code to go through a single iteration of X and will not continue to other iterations at the various Y levels.

public void flipVertical(int x, int y, int size)
  {
    int middle = x;
    Pixel leftPixel = null;
    Pixel rightPixel= null;
    Pixel value = null;
    int sizer = size/2;
    int sourceX = x - sizer;
    int targetX = x + sizer;
    int sourceY = y - sizer;
    int targetY = y + sizer;
    int green = 0;
    int blue = 0;
    int red  = 0;
    int green2 = 0;
    int blue2 = 0;
    int red2  = 0;

    //loops through the rows
    for (sourceY = y - sizer; sourceY < targetY; sourceY++)
    {
      //loop from start to end of X
      sourceX = x - sizer;
      for (sourceX = x - sizer; sourceX < targetX; sourceX++)
      {
        //get pixel locations
        leftPixel = getPixel (sourceX, sourceY);
        rightPixel = getPixel(targetX--, sourceY);

        // Swap colors with opposite pixels
        red2 = leftPixel.getRed();
        green2 = leftPixel.getGreen();
        blue2 = leftPixel.getBlue();

        red = rightPixel.getRed();
        green = rightPixel.getGreen();
        blue = rightPixel.getBlue();

        leftPixel.setRed(red);
        leftPixel.setGreen(green);
        leftPixel.setBlue(blue);

        rightPixel.setRed(red2);
        rightPixel.setGreen(green2);
        rightPixel.setBlue(blue2);

      }
    }
  }
도움이 되었습니까?

해결책

You are not resetting targetX for each row, even though you are modifying it (targetX--) in your loop. This would process the first row correctly, but not the remaining rows. You would need:

 //loop from start to end of X
 sourceX = x - sizer;
 targetX = x + sizer; // <- reset targetX
 for (sourceX = x - sizer; sourceX < targetX; sourceX++)
    ...

Note also that your initialization of sourceX in the for loop (as well as your initialization of it when it is declared at the start of your function) is redundant.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top