#JLabel centering after zoom

8 messages · Page 1 of 1 (latest)

fleet badger
#
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

public class Main extends JFrame {
    int startX, startY;
    double zoom = 1;
    public Main() {
        int height = 1000;
        int width = 1000;
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setSize(width, height);
        setVisible(true);
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        int w = width / 2;
        int h = height / 2;
        for (int a = 0; a < width; a++) {
            for (int b = 0; b < height; b++) {
                image.setRGB(a, b, 255);
            }
        }
        JLabel label = new JLabel(new ImageIcon(image));
        setContentPane(label);
        label.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                startX = e.getX();
                startY = e.getY();
            }
        });
        label.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                label.setLocation(label.getX() + e.getX() - startX, label.getY() + e.getY() - startY);
            }
        });
        label.addMouseWheelListener(e -> {
            int notches = e.getWheelRotation();
            if (notches < 0) {
                zoom *= 1.1;
            } else {
                zoom /= 1.1;
            }
            label.setIcon(new ImageIcon(image.getScaledInstance((int) (width * zoom), (int) (height * zoom), BufferedImage.SCALE_DEFAULT)));
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(Main::new);
    }
}
```The code above generates a 1000x1000 blue square. I have set up listeners to allow the blue square to be dragged around and zoomed in on. (Message too long)
pallid rainBOT
#

This post has been reserved for your question.

Hey @fleet badger! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

fleet badger
#

The problem is, if the square is dragged around and then zoomed in on, it automatically centers. I mostly understand why this happens, but putting label.setLocation after the zoom code does not seem to work.

#

If it's difficult to see, change line 20 fromjava image.setRGB(a, b, 255); to```java
image.setRGB(a, b, (int) (16777215 * Math.random()));

pallid rainBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

fleet badger
#

Hi

#

Still need help with this