Solved

Java Zoom-Pan effect with functional mouselisteners

Posted on 2007-12-01
16
3,548 Views
Last Modified: 2013-12-14
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
Comment
Question by:zhtet
  • 8
  • 8
16 Comments
 
LVL 92

Expert Comment

by:objects
ID: 20389408
u should be able to transform the event coords.
0
 

Author Comment

by:zhtet
ID: 20389521
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
 
LVL 92

Expert Comment

by:objects
ID: 20389675
the mouse event will fire when the mouse is in the panel, and tx used is really irrelevant.
0
 

Author Comment

by:zhtet
ID: 20389707
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
 
LVL 92

Expert Comment

by:objects
ID: 20389751
scaling doesn't change the dimensions of the panel. panel dimensions are always in screen coords.
0
 

Author Comment

by:zhtet
ID: 20389799
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
 
LVL 92

Expert Comment

by:objects
ID: 20389882
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
 

Author Comment

by:zhtet
ID: 20389913
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 92

Expert Comment

by:objects
ID: 20390345
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
 

Author Comment

by:zhtet
ID: 20390437
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
 
LVL 92

Expert Comment

by:objects
ID: 20390450
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
 

Author Comment

by:zhtet
ID: 20390755
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
 
LVL 92

Expert Comment

by:objects
ID: 20390757
what viewport are you referring to?
0
 

Author Comment

by:zhtet
ID: 20390796
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
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 20392824
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
 

Author Comment

by:zhtet
ID: 20393066
Thanks i got it!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now