Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Magnifier only zooming towards top left corner #46

Open
ernOho opened this issue May 7, 2022 · 6 comments
Open

Magnifier only zooming towards top left corner #46

ernOho opened this issue May 7, 2022 · 6 comments

Comments

@ernOho
Copy link

ernOho commented May 7, 2022

Hi,
after using this library, I've noticed that the magnifier (from PaintToolType.MAGNIFIER) doesn't zoom in at clicked mouse location, but always towards the top left corner. I've attached my config. of the magnifier. I suspect that I have forgotten something.

With kind regards

image

@defano
Copy link
Owner

defano commented May 7, 2022

Hi @ernOho ,

There's nothing you normally have to do when creating the magnifier tool to make this work, provided that you've embedded your canvas in a JScrollPane component.

Here's how it works:

The JMonetCanvas looks to see if it's been embedded directly inside a JScrollPane view port. If it has, when you click the magnifier on the canvas, the canvas's scroll controller calls the JScrollPane to instruct it to center on the location you clicked. This can only work when your canvas is embedded in a JScrollPane.

To demonstrate, try this code:

    public static void main(String argv[]) {
        javax.swing.SwingUtilities.invokeLater(() -> {

            // Create and show Swing frame
            JFrame frame = new JFrame("My Pretty Picture");
            frame.setPreferredSize(new Dimension(640, 480));
            frame.pack();
            frame.setVisible(true);

            // Create a JMonet canvas and add it to the viewport of a JScrollPane
            JScrollPane scrollPane = new JScrollPane();
            JMonetCanvas myCanvas = new JMonetCanvas(new Dimension(640, 480));
            scrollPane.setViewportView(myCanvas);
            frame.getContentPane().add(scrollPane);

            PaintToolBuilder.create(PaintToolType.MAGNIFIER).makeActiveOnCanvas(myCanvas).build();
        });
    }

If you have a more complicated layout in which your JMonetCanvas cannot be the sole element added to the JScrollPane's view port then you will have to implement your own SurfaceScrollController and inject it into the canvas with myCanvas.setSurfaceScrollController(myScrollController);.

Does that help?

@ernOho
Copy link
Author

ernOho commented May 9, 2022

Hi @defano,
Thanks for your answer. I have been using Javafxs scrollpane and that's why it didn't work.

I have been trying to add swings jScrollPane to my jfx application. However, adding it to my jfx app , e.g. to a HBox (box.getChildren().addAll(..)) doesn't work.

So it seems implementing my own SurfaceScrollController(for JavaFxs ScrollPane) is a way to solve this? Unless there is another way to add a jScrollPane to a Javafx application?

kind regards

@defano
Copy link
Owner

defano commented May 9, 2022

This is clearly a flaw. The DefaultSurfaceScrollController was never designed to support JFX, and should have been. Good catch. This class should be updated to support both Swing and JFX scroll panes.

You can solve this problem immediately by implementing that logic and injecting your implementation of SurfaceScrollController into your canvas. If you get it working, share your code so we can incorporate it into DefaultSurfaceScrollController. Have a look at how DefaultSurfaceScrollController works—it's pretty simple.

Otherwise, I'll try to fix this sometime this week.

@ernOho
Copy link
Author

ernOho commented May 9, 2022

Hi. I'll get on it asap and share once it works.

@ernOho
Copy link
Author

ernOho commented May 31, 2022

Hi @defano,
After many failed attempts and hours, I have finally given up trying to implement my own SurfaceScrollController.
I would greatly appreciate it, if you could fix this issue.

kind regards

@defano
Copy link
Owner

defano commented Jun 2, 2022

Sorry for the frustration—I'll take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants