How to Flip an Image Using Graphics2D in Java

Question

How can I flip an image vertically or horizontally using Graphics2D in Java?

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class ImageFlipper extends JPanel {
    private BufferedImage image;

    public ImageFlipper(BufferedImage img) {
        this.image = img;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        // Flip image horizontally
        g2d.scale(-1, 1);
        g2d.drawImage(image, -image.getWidth(), 0, null);

        // Alternative: Flip image vertically
        // g2d.scale(1, -1);
        // g2d.drawImage(image, 0, -image.getHeight(), null);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        BufferedImage img = ...; // Load your image here
        ImageFlipper flipper = new ImageFlipper(img);
        frame.add(flipper);
        frame.setSize(img.getWidth(), img.getHeight());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

Answer

Flipping an image in Java using the Graphics2D class involves applying a scaling transformation to the graphics context. This transformation effectively mirrors the image about a specified axis—horizontal or vertical. Below, we provide a guided approach to accomplish this using a custom JPanel.

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class ImageFlipper extends JPanel {
    private BufferedImage image;

    public ImageFlipper(BufferedImage img) {
        this.image = img;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        // Flip image horizontally
        g2d.scale(-1, 1);
        g2d.drawImage(image, -image.getWidth(), 0, null);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        BufferedImage img = ...; // Load your image here
        ImageFlipper flipper = new ImageFlipper(img);
        frame.add(flipper);
        frame.setSize(img.getWidth(), img.getHeight());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

Causes

  • The original image needs to be displayed in a flipped orientation.
  • Transformations such as scaling can help achieve the desired visual effect.

Solutions

  • Use the `scale` method of the Graphics2D class to flip the image.
  • Set the origin point to adjust the position of the drawn image after scaling.

Common Mistakes

Mistake: Forgetting to reset the transformation before drawing another component.

Solution: Always call `g2d.setTransform(new AffineTransform());` before drawing any other components.

Mistake: Not properly loading the image, resulting in a null reference.

Solution: Ensure that the image is loaded correctly before creating the ImageFlipper instance.

Helpers

  • Java image flipping
  • Graphics2D flip image
  • Java image manipulation
  • How to use Graphics2D
  • Java drawing images

Related Questions

⦿How to Split a String at the First Space Occurrence in Java

Learn how to split a string from the first space occurrence using Java with examples and explanations.

⦿How to Resolve Lombok Compilation Issues in Maven Projects

Struggling with Lombok not compiling in a Maven project Learn how to resolve these issues with our expert guide.

⦿How to Sort a Linked List in Java?

Learn how to efficiently sort a linked list in Java with stepbystep explanations and code examples.

⦿How to Convert a GUID to a Byte Array in C#

Learn how to effectively convert a GUID to a byte array in C with stepbystep examples and common mistakes to avoid.

⦿How to Check If a File Exists Before Using openFileInput in Android?

Learn how to verify file existence before calling openFileInput in Android with effective code examples and debugging tips.

⦿How to Resolve NullPointerException When Using List.add in Java

Learn how to fix NullPointerException in Java when calling list.add with solutions and code examples.

⦿How to Always Display Two Decimal Places for Doubles in Java?

Learn how to format double values in Java to always show two decimal places with examples and best practices.

⦿Why is the Value 09 Considered an Invalid Integer?

Learn why the number 09 is deemed invalid in programming its implications and how to handle leading zeros effectively.

⦿Understanding Spring Framework and Its Utilization of Interfaces

Explore how the Spring Framework uses interfaces to promote loose coupling and testability in Java applications. Learn best practices and common pitfalls.

⦿How to Set a Default Main Class in Java?

Learn how to set a default main class in Java projects effectively. Stepbystep guidance and code snippets included.

© Copyright 2025 - CodingTechRoom.com