• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3596
  • Last Modified:

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
0
zhtet
Asked:
zhtet
  • 8
  • 8
1 Solution
 
objectsCommented:
u should be able to transform the event coords.
0
 
zhtetAuthor Commented:
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
0
 
objectsCommented:
the mouse event will fire when the mouse is in the panel, and tx used is really irrelevant.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
zhtetAuthor Commented:
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
0
 
objectsCommented:
scaling doesn't change the dimensions of the panel. panel dimensions are always in screen coords.
0
 
zhtetAuthor Commented:
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
0
 
objectsCommented:
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
0
 
zhtetAuthor Commented:
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
0
 
objectsCommented:
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.

0
 
zhtetAuthor Commented:
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
0
 
objectsCommented:
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)
0
 
zhtetAuthor Commented:
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
0
 
objectsCommented:
what viewport are you referring to?
0
 
zhtetAuthor Commented:
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
0
 
objectsCommented:
you'll find life a easier by using a tx matrix.

What i usually do when dealing with multiple coord systems it to have my code operate in a single coord system, and transform any coords that are recieved into that system.
That way there isn't any confusion as to what coordinate system a given point is in.
0
 
zhtetAuthor Commented:
Thanks i got it!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now