I don\'t get vertical image at all. how do i write code for flipping image verti
ID: 3725419 • Letter: I
Question
I don't get vertical image at all. how do i write code for flipping image vertically in java? below is my vertical flip code:
package pgrm8;
/**
* Filter that flips the image vertically.
* This class is COMPLETE. Don't change it. But model your other classes (such
* as FlipVerticalFilter) after it.
*/
public class flipVerticalFilter implements Filter
{
public void filter(PixelImage pi)
{
Pixel[][] data = pi.getData();
int width = pi.getWidth();
int mirrorPoint = width / 2;
Pixel leftPixel = null;
Pixel rightPixel = null;
{
for( int y = 0; y < pi.getHeight(); y++ )
{
for( int x = 0; x < mirrorPoint; x++ )
{
leftPixel = getPixel( x, y );
rightPixel = getPixel( width - 1 - x, y );
rightPixel.setColor( leftPixel.getColor() );
}
}
}
Explanation / Answer
/**
* Filter that flips the image vertically.
* This class is COMPLETE. Don't change it. But model your other classes (such
* as FlipVerticalFilter) after it.
*/
public class flipVerticalFilter implements Filter
{
public void filter(PixelImage pi)
{
Pixel[][] data = pi.getData();
int width = pi.getWidth();
int mirrorPoint = width / 2;
Pixel leftPixel = null;
Pixel rightPixel = null;
for( int y = 0; y < pi.getHeight(); y++ )
{
for( int x1 = 0, x2 = width-1; x1 < x2; x1++, x2-- )
{
leftPixel = getPixel( x1, y );
rightPixel = getPixel( x2, y );
//swap the colors
Color temp = leftPixel.getColor();
leftPixel.setColor(rightPixel.getColor());
rightPixel.setColor(temp);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.