Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 615325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:12:30+00:00 2026-05-13T18:12:30+00:00

I use Graphics2D in Java to scale and rotate the picture I draw. I

  • 0

I use Graphics2D in Java to scale and rotate the picture I draw. I now want to be able to tell what the original coordinates were when I click on a certain point in the picture. So given the rotated and scaled coordinates I want to calculate the original ones. Is there a simple way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:12:30+00:00Added an answer on May 13, 2026 at 6:12 pm

    If you keep a copy of the AffineTransform you use when you paint the image, you can use
    AffineTransform.inverseTransform(Point2D ptSrc, Point2D ptDst)
    to transform a device space coordinate back to user space

    Edit: If you capture the current transform of the Graphics2D while painting, beware of the Graphics2D being re-used for multiple lightweight children of the same window/panel, because then the transform will be relative to the parent component but the mouse coordinates will be relative to the child. You need to capture the changes you make to the transform not its final value. Example:

    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    import java.awt.geom.AffineTransform;
    import java.awt.geom.NoninvertibleTransformException;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import javax.imageio.ImageIO;
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    
    public class Main {
        public static void main(String[] args) throws MalformedURLException, IOException {
            JFrame frame = new JFrame();
            Box box = new Box(BoxLayout.Y_AXIS);
            BufferedImage image = ImageIO.read(new URL("http://sstatic.net/so/img/logo.png"));
            AffineTransform xfrm1 = AffineTransform.getScaleInstance(0.95, 1.25);
            xfrm1.rotate(-0.3);
            box.add(new ImageView(image, xfrm1));
            AffineTransform xfrm2 = AffineTransform.getShearInstance(0.1, 0.2);
            xfrm2.scale(1.3, 0.9);
            box.add(new ImageView(image, xfrm2));
            frame.add(box);
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    }
    
    @SuppressWarnings("serial")
    class ImageView extends JComponent {
        @Override
        public void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            try {
                paintXfrm = g2d.getTransform();
                paintXfrm.invert();
                g2d.translate(getWidth() / 2, getHeight() / 2);
                g2d.transform(xfrm);
                g2d.translate(image.getWidth() * -0.5, image.getHeight() * -0.5);
                paintXfrm.concatenate(g2d.getTransform());
                g2d.drawImage(image, 0, 0, this);
            } catch (NoninvertibleTransformException ex) {
                ex.printStackTrace();
            }
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(image.getWidth() * 2, image.getHeight() * 2);
        }
    
        ImageView(final BufferedImage image, final AffineTransform xfrm) {
            this.canvas = image.createGraphics();
            canvas.setColor(Color.BLACK);
            canvas.setStroke(new BasicStroke(3.0f));
            this.image = image;
            this.xfrm = xfrm;
            addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    try {
                        mouseDownCoord = e.getPoint();
                        paintXfrm.inverseTransform(mouseDownCoord, mouseDownCoord);
                    } catch (NoninvertibleTransformException ex) {
                    }
                }
    
                @Override
                public void mouseExited(MouseEvent e) {
                    mouseDownCoord = null;
                }
            });
            addMouseMotionListener(new MouseMotionAdapter() {
                @Override
                public void mouseDragged(MouseEvent e) {
                    Point p = e.getPoint();
                    try {
                        paintXfrm.inverseTransform(p, p);
                        if (mouseDownCoord != null) {
                            canvas.drawLine(mouseDownCoord.x, mouseDownCoord.y, p.x, p.y);
                            for (Component sibling: getParent().getComponents()) {
                                sibling.repaint();
                            }
                        }
                        mouseDownCoord = p;
                    } catch (NoninvertibleTransformException ex) {
                        ex.printStackTrace();
                    }
                }
            });
        }
    
        private Graphics2D canvas;
        private BufferedImage image;
        private AffineTransform xfrm;
        private AffineTransform paintXfrm;
        private Point mouseDownCoord;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 365k
  • Answers 365k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use the data in the /proc filesystem to… May 14, 2026 at 3:57 pm
  • Editorial Team
    Editorial Team added an answer Do you still get the error when you use a… May 14, 2026 at 3:57 pm
  • Editorial Team
    Editorial Team added an answer You can use array_chunk to create a single array comprised… May 14, 2026 at 3:57 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.