Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Image's Relative position / Jpanel

 
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

This topic is the continuation of my former topic “How to pass mouse event to a Label"
https://coderanch.com/t/673467/java/pass-mouse-event-Label#3151953

The question is regarding to the possibility to recover the coordinates of the Image created with : drawImage() relative to Jpanel
The image in my panel is wider and higher that the panel itself.

With help of  
public void mousePressed(MouseEvent evt)
and
public void mouseDragged(MouseEvent evt)
I succeeded (with mouse click entry point, and mouse Drag Points) to compute (xImgOrgRef,yImgOrgRef) == Image Relative position  / Jpanel.
This is working well.

Principle of code



I could observe that through zooming with


The coordinates of "Image Relative position  / Jpanel" will also change.

Unfortunately I don't see how to compute : (xImgOrgRef,yImgOrgRef) based on the Zoom Factor.

Is there a simple way to get (xImgOrgRef,yImgOrgRef) ?




Many many thanks in advance

Christian
 
Bartender
Posts: 4067
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Christian,

long time no see!

You do not need to let your image origin to depend on the zoom factor at all. If I refer to your drawing made in your other topic, then you have xImgOrgRef as offset to your panel origin. Now, given a zoom factor of z, we get that the actual image to draw has as x left coordinate

-xImgOrgRef × (1 + z)

and likewise as y top

-yImgOrgRef × (1 + z)

The width of the part of the image to draw is image.get width / (1 + z), and likewise for the height of that part.

Then all that is left is to use the overloaded g.drawImage variant

g.drawImage(image,  dx1, dx2, dy1, dy2, sx1 et cetera, see the api of the Graphics class.

So, you only need to maintain the x/yImgOrgRefs, independent of the zoom. I just programmed my version, the disadvantage was thar with increasing zoom factor, the dragging speed increased accordingly.  But maybe that is a petite bug. Have you considered using a JScrollPane with the advantage of getting free scrollbars into the bargain?

Edit: the drawing I referred to is above in your opening post too.
 
Christian Klugesherz
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Piet,

Thanks for your answer :-)
Really really appreciated.

I will try to compile all these information, and revert to you.

So if I understood, there is no method to allow to recover the offset of the image / origin panel. ?

Christian
 
Piet Souris
Bartender
Posts: 4067
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not that I'm aware of. It boils down to keeping the information in some variables and doing some plain old maths.

Given your question, there might be a difference in approach. Given a zoom factor, it is possible to zoom the complete image, calculating the zoomed offset and drawing that whole thing. What I am doing is calculating a part of the image such that when zoomed, fills up the panel. With my approach I get tht that larger the zoom factor is the faster the dragging... well, it was just a quick program yesterday evening. Here is my code, it may be intresting to compare both methods.
 
Christian Klugesherz
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Piet,

Finally I've got it :-) :-)
The solution was simple and linked to the order in the Affine Transformation.
-->  First Zoom, then translate to be sure to not pollute the offset with the zoom operation !!
Smart no ?

Here the code which is perfectly working.
The code has been improved relative to my first topic :-)

Your code was really helpful to improve and simplify my code.
Many thanks for that

In case you want to test, please use the attached image.

Merci beaucoup,

Met vriendelijke Groeten

Christian

R22.png
[Thumbnail for R22.png]
 
Christian Klugesherz
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hummmm just discovered a little BUG
Fixed here

 
grapes are vegan food pellets. Eat this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic