1

I have an assignment to be done in Java, and I can't figure this out for the life of me. I'm supposed to use Graphics2D and Java.AWT. to mirror an image across both the x-axis and y-axis.

Current code is:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
public class DrawingImages
{
private Picture newCanvas = null;
private Graphics g = null;
private Graphics2D g2 = null;
private Picture pic1 = null;
private Picture pic2 = null;
private Color color = null;
private Pixel sourcePixel, targetPixel = null;
private Color sourceColor, targetColor = null;

DrawingImages(Picture canv, Color col)
{
    Picture sourcePicture = new Picture("WashingtonMonument.jpg");
    newCanvas = canv;
    newCanvas.setAllPixelsToAColor(Color.BLACK);
    for(int y = sourcePicture.getHeight()-1; y >0; y=y-1)
    {
        for(int x = sourcePicture.getWidth() - 1; x > 0; x = x - 1)
        {
            sourcePixel = sourcePicture.getPixel(x,y);
            sourceColor = sourcePixel.getColor();
            targetPixel = newCanvas.getPixel(x+sourcePicture.getWidth() -1,y+sourcePicture.getHeight()- 1);
            targetPixel.setColor(sourceColor);         
        }
    }


    g = newCanvas.getGraphics();
    g2 = (Graphics2D)g;  
}

}

John Garza
  • 94
  • 3
  • 12
  • Exactly what problem are you having with your current code? Can you read in the source picture correctly? Can you create a target picture, but the mirroring is wrong? – Nick Sep 14 '12 at 22:32
  • it doesn't create a mirror image. It reads the source image correctly, and it also puts it on the canvas, but not in a mirrored way – John Garza Sep 14 '12 at 22:34
  • I don't want to give the answer away. But let's say your source picture has a pixel at x=4, y=2. What pixel does that correspond to in the target picture? (You'll need to know the picture's width and height.) Can you figure out a general formula from that specific case? – Nick Sep 14 '12 at 22:37
  • if x = 4, it would be 4 + sourcePicture.getWidth(), so if the width was 200, the new x coordinate would be 204 - 1, so 203 for the final answer. Same thing is done for y, just with the right numbers, so let's use a 200x200 image, it would be located at 203, 203 on the canvas – John Garza Sep 14 '12 at 22:41
  • Maybe I'm misinterpreting your problem, but that doesn't seem right to me. I gather you're putting the target image on the same canvas, in a different location? Double-check your math, on paper with a diagram if necessary. – Nick Sep 14 '12 at 22:43
  • What's happening is that there's a separate image (WashingtonMonument.jpg in the code). Using nested for-loops, it is copied onto a new image constructed as a canvas in Java itself. I've been successful in that already. However, Now I need to mirror it on both axises. – John Garza Sep 14 '12 at 22:45
  • Mirroring on both axes is equivalent to a 180-degree rotation – Jim Garrison Sep 14 '12 at 22:56
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Sep 14 '12 at 23:24

1 Answers1

3

What does picture derive from? Image? If so, you can use Image.scale(-1, -1) to mirror the image directly.

If not, you could use a AffineTransform.getScaleInstance(-1, -1) directly on the Graphics context, but your going to have to translate the image's position

You could also take a look at AffineTransform.rotate() - how do I xlate, rotate, and scale at the same time? which uses this technique

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • using Image.scale(-1,-1) or in my case, newCanvas.scale(-1,-1) or sourcePicture.scale(-1,-1) says that it cannot find what scale is – John Garza Sep 14 '12 at 23:13
  • Okay, so use `AffineTransform.getScaleInstance` directly on the `Picture` graphics (`newCanvas.getGraphics()`) – MadProgrammer Sep 14 '12 at 23:55