• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Wanted: example mouse coordinates->world coordinates

 
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Siegfried Heintze
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(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
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
What's that smell? I think this tiny ad may have stepped in something.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic