Link to home
Start Free TrialLog in
Avatar of zhtet
zhtet

asked on

Java Zoom-Pan effect with functional mouselisteners

Hi,
    I am looking to create a zoom - pan effect on a jpanel with multiple elements in it with various listeners. I can create the zoom-pan effect relatively easily with Graphics2D's transformations and then drawing the panel. However, once that is done, all the components with mouselisteners are out of whack because their size and location are in the original coordinates instead of zoomed coordinates. I can't scale the mouse coordinates to fix this either, since i am using java's intrinsic mouselisteners to get the mousemoved,clicked etc events (or can i?) Any suggestions to work around this?

Thanks
Avatar of Mick Barry
Mick Barry
Flag of Australia image

u should be able to transform the event coords.
Avatar of zhtet
zhtet

ASKER

Hi objects,
      Unless I am interpreting it wrongly, I am not sure if that can help me. Reason being I have a onMouseEnter for each sub-jPanel within the main jPanel that I am performing the zoom on. So after I perform a zoom of scale 2.0 on each axis with Graphics2D within paintComponent, a panel with dimensions (0,0,100,100) for example will still trigger onMouseEnter within those dimensions instead of within (0,0,200,200)
     I can go through each of those sub jpanels and resize & relocate them so the mouse events trigger correctly and use this as the "zoom" instead of graphics2d's scale, and it **should** work i believe, but I am trying to avoid that if possible.

Thanks
the mouse event will fire when the mouse is in the panel, and tx used is really irrelevant.
Avatar of zhtet

ASKER

Hi again,
        I've tested this earlier and the mouse event doesn't fire till i get into the boundaries of the old jpanel dimension (0,0,100,100) in the previous example, instead of firing in the new dimensions of (0,0,200,200). Any reasons why? I am basically just scaling the entire jpanel using graphics2d.

Thanks
scaling doesn't change the dimensions of the panel. panel dimensions are always in screen coords.
Avatar of zhtet

ASKER

so does that mean I would have to manually resize the dimensions of the panel in order for the mouseEvents to fire correctly in the scaled/zoomed image?

Thanks
well the mouse events are really firing correctly, they are just in screen coords.
If you need them in a different coord space then you need to transform them
Avatar of zhtet

ASKER

Sorry for going back and forth, i'll increase the point value accordingly. The thing is i really dont mind which coord space the mouse events are; I just need the onMouseEnter/Over/etc for each of the jPanel to fire even in the resized/zoomed components. What is the best way to go about achieving this in your opinion?

If I transform the mouse events manually, dont i have to manually check for each of the mouseEvents for enter/over, instead of using the callbacks from Java?

Thanks
what exactly are you referring to as a 'zoomed' component.
Regardless of what transformations you do for your painting the component itself does not change in dimensions.

> If I transform the mouse events manually, dont i have to manually check for each of the mouseEvents > for enter/over, instead of using the callbacks from Java?

No, the mouse listener will get called when an event occurs. If you want the coords of the event in a different coord space then you just need to transform them appropriately.

Avatar of zhtet

ASKER

when i refer to zoomed component, i mean a jFrame which is of fixed height and width, but the contents of the jFrame has everything resized to be larger. So zoom of 2.0 in the e.g. above means object of (0,0,100,100) dimensions is now (0,0,200,200).
Each time i do a "zoom"/scale, even though everything is drawn bigger, but the components are still the same size, so for the user, everything is twice the size but since the mouseOver/Enter/etc events are still based on the original smaller components, so everything is inaccurate. Does that make sense now?

Thanks
yes, I understand. And what I've said above still holds.
You need to transform the (screen) coordinates into the coordinate system of the coords you are comparing against (or visa versa)
Avatar of zhtet

ASKER

i c... so basically there is no way for me to use the auto check provided by java except to get the screen coord of the mouse event and then perform a manual check myself for the objects it intersect in the viewport?

Thanks
what viewport are you referring to?
Avatar of zhtet

ASKER

screen viewport or rather the root jFrame... so basically i would do something like

given mouseEvent me and scale s and translate t
take (0,0) (top left) and transform into the xformed coord sys ((0,0) * s + t) to get its point in xformed coord sys.
then ((diff of the current (0,0) and xformed (0,0)) + (me.x, me.y)) to get mouseEvent's position in xformed sys.
then reverse xform this point ((p-t)/s) to get point it corresponds to in image of original size
then go through the obj hierarchy to find components it intersect with and then manually call the mouseEvents for those components with me argument

correct?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zhtet

ASKER

Thanks i got it!