how to reflect an image in java

809 views Asked by At

I am creating a program where I have to reflect an image horizontally and vertically. I have created a geometric shape image, but I am having a hard time figuring out how to flip my picture. I was wondering if someone could help me and tell me what to do to flip a picture. Thanks

My code so far is :

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;

class DrawingDemoV3
{
    Picture canvas = null;
    Graphics g = null;
    Graphics2D g2 = null;

    DrawingDemoV3(int length, int height, Color color)
    {
        canvas = new Picture(length, height);
        canvas.setAllPixelsToAColor(color);
        g = canvas.getGraphics();
        g2 = (Graphics2D)g;
        g2.setColor(color);    
    }

    public void drawAFilledOval(Color color, int x1, int y1, int width, int height)
    {
        g2.setColor(color);
        g2.fillOval(x1, y1, width, height);
    }

    public void drawARectangle(Color color, int x1, int y1, int width, int height)
    {
        g2.setColor(color);
        g2.drawRect(x1, y1, width, height);
    }

    public void drawAFilledRectangle(Color color, int x1, int y1, int width, int height)
    {
        g2.setColor(color);
        g2.fillRect(x1,y1, width, height);
    }

    public void drawALine(Color color, int x1, int y1, int x2, int y2)
    {
        g2.setColor(color);
        g2.drawLine(x1,y1,x2,y2);
    }

    public Picture getDrawing()
    {
        return canvas;
    }
}

public class DrawingDemoTesterV3
{
    public static void main(String[] args)
    {
        DrawingDemoV3 drawing1 = new DrawingDemoV3(200, 200, Color.BLACK);   

        drawing1.drawAFilledRectangle(Color.PINK, 90, 0, 20, 200);
        drawing1.drawAFilledRectangle(Color.PINK, 0, 90, 200, 20);
        drawing1.drawARectangle(Color.CYAN, 40, 40, 120, 120);
        drawing1.drawALine(Color.ORANGE, 0,0, 200, 200);
        drawing1.drawALine(Color.ORANGE, 200,0, 0, 200);
        drawing1.drawAFilledOval(Color.YELLOW, 80, 80, 38, 36);

        Picture picture1 = drawing1.getDrawing();
        picture1.show();
    }
}
1

There are 1 answers

0
Adam On

Flipping/Reflection on an image.

The goal is to use Affine Transform for sequences of translations, scales, flips, rotations, and shears applied to 2D points and mapping.

While link only answers are typically frowned on, I don't want to just copy paste and rip off a perfectly explained answer and fill in his words with mine.