Search code examples
javaswingtooltipaffinetransform

Java, Swing: tooltip and affine transformation issue


Writing GUI in Java, an interesting problem has appeared. I apologize for a slightly longer code.

There is class Graphic derived from JPanel which displays uploaded raster. It implements several features with the raster data including the zoom operations. It also supports a mouse click event which stores coordinates of a point and displays it over the raster. The raster is fitted with the affine transformation.

 import java.awt.*;
 import java.awt.event.*;
 import java.awt.geom.*;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.util.Locale;
 import javax.imageio.ImageIO;
 import javax.swing.*;

 public class Graphic extends JPanel{

    private BufferedImage image; 
    private AffineTransform trans;
    Point2D.Double point;

    public Graphic () {
            try {image = ImageIO.read(new File("e:/Work/test.jpg"));}
            catch (Exception e) {}
            trans = new AffineTransform();
            trans.translate(0, 0);
            trans.scale(1, 1);
            point = new Point2D.Double(0,0);
            this.setToolTipText("");

            addMouseListener(new MouseAdapter() {
                    public void mouseClicked(MouseEvent e) {
                            point.x = (e.getPoint().getX() - trans.getTranslateX()) / trans.getScaleX();
                            point.y = (e.getPoint().getY() - trans.getTranslateY()) / trans.getScaleY();
                            System.out.println(trans);  //Print affine transformation parameters
                            System.out.println(e.getPoint().getX() + " " + e.getPoint().getY()); //Cursor coordinates
                            repaint();
                    } 
            });
    }

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

            if (image != null ){    
                    trans = g2d.getTransform();
                    Dimension size = this.getVisibleRect().getSize();
                    double sx = (size.getWidth() - image.getWidth()) / 2;
                    double sy = (size.getHeight() - image.getHeight()) / 2;
                    trans.translate(sx, sy);
                    g2d.setTransform(trans);
                    g2d.drawImage(image, 0, 0, this);        
                    g2d.fillOval((int)point.x - 10, (int)point.y - 10,  20,  20);
                    g2d.dispose();
            }
    }

    @Override
    public String getToolTipText(MouseEvent e) { 
            double x = (e.getX() - trans.getTranslateX()) / trans.getScaleX();
            double y = (e.getY() - trans.getTranslateY()) / trans.getScaleY();
            return String.format(Locale.ROOT, "%2.2f", x) + "  " + String.format(Locale.ROOT, "%3.2f", y); 
    }

    public static void main(String[] args){
            JFrame f = new JFrame();
            f.setSize(800, 600);
            f.add(new Graphic());
            f.setVisible(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

The class redefines the tool tip method and displays coordinates of the cursor.

Before a tool tip appears the mouse click into the picture works as expected

Figure1: mouse click, without tool tip

a circle appears at the right location.

Figure 2: drawn point mark, without tool tip

Subsequently, let us repeat theses steps after a tool tip appears. The mouse click at the picture

Figure 3: mouse click, tool tip appears

behaves unexpectedly, and the point is drawn far away.

Figure4: drawn point mark, tool tip appeared

Debugging the code, the following problem has has been found... When a tool tip appears, the shift ratios sx=mo2, sy=m12 in the affine transformation change from

AffineTransform[[1.0, 0.0, -1927.5], [0.0, 1.0, -1435.1]]
379.0 339.0  //Cursor coordinates xc, yc

to

AffineTransform[[1.0, 0.0, -2303.5], [0.0, 1.0, -1794.5]]
376.0 339.0  //Cursor coordinates xc, yc

To avoid a shift of the entire situation, instead m02 and m12 shifts, the transformed coordinates point.x, point.y should be corrected adding cursor coordinates + something. Is it a bug in the Swing library or a feature :-) ?

Thanks for very much for your comments, help or explanation...

The raster file: test.jpg.


Solution

  • The solution is surprisingly simple...

    It is necessary to call another repaint() before coordinates of a point are stored. Therefore, the mouse press event foregoing the mouse click event is utilized:

    @Override
    public void mousePressed(MouseEvent e) {
        repaint();
    }