aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Wanted: example mouse coordinates->world coordinates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Wanted: example mouse coordinates->world coordinates" Watch "Wanted: example mouse coordinates->world coordinates" New topic
Author

Wanted: example mouse coordinates->world coordinates

Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 376
Can soneome show me a little example that uses JScrollpane and converts mouse coordinates to world coordinates using the MouseListener observer?

I'm trying to implement hit testing with JScrollPane and I don't understand the coordinates I'm getting from the MouseListener. Do I have to interogate JScrollpane for the position of the scrollbars to learn how to transform mouse coordinates into world coordinates or is the MouseLister already doing that for me?
Thanks,
Siegfried
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 376
(1) How do I draw a little circle at the current mouse location? Here is my attempt and it is not working.

(2) Also, when I have not moved the scroll bars yet, why are not the pre and post conversion coordinates identical?

(3) Can you explain the argument to getCenter? Is this argument implicitly a data member because it is declared final? I have never seen this in a java tutorial before. Does this feature have a name?

Thanks again!
Siegfried

Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 376
Forgot #4:
(4) When I scroll to the right and scroll back left, the panel (my grid) is not redrawn correctly.

Siegfried
Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 376
Woops -- could issue #4 be the same problem Craig Woods already addressed when he told me to use g2.draw(at.createTransformedShape(shape[ii])); instead of g2.transform(at);?

I think so. I wish sun would fix that bug. Maybe I'll try another jvm like ibm's jikes.
Siegfried
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
The Points dest and mldst are the same. The convertPoint arguments are the same for each
one. All you need, so far, is mouse coordinates in the panel coordinate system. I included
the converted point (converted to the JFrame coordinate system) for illustration. It isn't
really useful in this app.
In response to your questions:
1 — in mousePressed Point p is the mouse position on the JPanel panel. We know this
because we added the MouseListener to panel so it must report coordinates in the panel
coordinate system. To place a circle at the mouse location we center a new Ellipse2D over
the mouse point obtained from the MouseEvent in mousePressed.

2 — the two points are in different coordinate systems: the MouseEvents are
reporting locations in the panel coordinate system, the converted point is in the JFrame
coordinate system. So the x and y values should differ by the difference in sizes of the
frame and the child component panel. This difference is the sum of the insets of all
parents of panel: JScrollPane, contentPane, JFrame. Console shows this when code is run.

3 — this is required by the compiler. Removing the final modifier results in this
compiler error:

The argument type is Component (superclass of JFrame); selected because Component is the
type required in the convertPoint method. This counts as an implicit cast. It could just
as easily have been JFrame, Frame or Window.
I don't know if there is a name for this use of final.
4 — I do not see any difference before/after scrolling left/right.
Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 376
Yipee! that works! Now I just have to figure out why drawing tiny circles with the mouse does not work in the GraphicsOnly.java I have tried to enhance.

But I have another question:

Suppose I want to implement a location or context map that is always down in the extreme lower right hand corner that never scrolls off. You have probably seen them: they display the entire map and then show a rectangle that moves around in that map as you pan and zoom.

Does swing have a feature to support this? Perhaps there is a plane I can write to that is immune to the scroll bars?

Thanks,
Siegfried
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wanted: example mouse coordinates->world coordinates
 
Similar Threads
Mouse listener question
how to get location of mouse click with respect to picture?
display mouse pointer coordinates
Not able to receive mouse events on JPanel
How to add mouse listener?