I have the code below to create a mirror image of an RBG photo... However, I wou
ID: 3634758 • Letter: I
Question
I have the code below to create a mirror image of an RBG photo...However, I would like to flip the image upside-down...
Any ideas???
public class mirrorFilter extends Filter
{
public mirrorFilter(String name)
{
......
}
public void apply(OFImage image)
{
int height = image.getHeight();
int width = image.getWidth();
for(int y = 0; y < height; y++) {
for(int x = 0; x < width/2; x++) {
Color left = image.getPixel(x, y);
image.setPixel(x, y, image.getPixel(width-1-x, y));
image.setPixel(width-1-x, y, left);
}
}
}
}
Explanation / Answer
To do this, you will need to use the Graphics2D class in java.awt.Graphics2D. If this is an applet, you say paint(Graphics g) { Graphics2D g2d = (Graphics2D)g; } and use g2d when you would normally use g. To invert an image, your would need to use the scale (double sx, double sy). Inverting an image is the same as making the y scale negative one, so you would call this as g2d.scale(1, -1) This would turn the entire Graphics2D object upside down. If you need more information, you can look at the javadocs at http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Graphics2D.html