aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Coordinates and Mouse Events Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Coordinates and Mouse Events" Watch "Coordinates and Mouse Events" New topic
Author

Coordinates and Mouse Events

Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 828
Hi all,
I'm using AffineTranform with Graphics2D to "shrink" the display of a JPanel I have. I was wondering is there a way to capture a mouseclick, get the coordinates of that mouseclick, and determine the actual coordinates of the click in relation to the image but before the transformation was applied?
Is there a way to pass in a coordinate and fire a mouse click event for that coordinate? Since the original click would fire off another mouse click event I would need to fired off mouse click event to not continue the cycle of firing mouse events. Thanks in advance if you can help with one or both questions.

Rob
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40059
    
  28
I would suggest you copy the Graphics object before trying to apply affine transforms to it. Graphics2D g2 = (Graphics2D)g.create(); (or I think so). That means the original Graphics object won’t be distorted because some affine transforms cannot be completely undone. You keep the original for future reference and for anything you aren’t using affine transforms for.
Not certain, but I think that original Graphics object will retain the original location information you need.
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 828
Neat but I'm just wondering how I could use that to accomplish what I need to do. When I "shrink" the display all the nested objects change position and for each object I have it so the user can click it and a popup comes up displaying information on that particular object. I will still need to know the relationship between the clicked location of the shrinked graphics object and the original unless there's something else I can do. Thanks for the information.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40059
    
  28
Sorry for delay; there have been problems with the website.
Let’s imagine you have a 200×200px display, and you translate the origin to the centre (100, 100), and you scale by (0.5, 0.5).
[By the way, you know you can get your xy coordinates the way you are used to seeing them on paper by moving the origin to the centre [translate(width / 2, height / 2)] and scaling, I think by (1.0, -1.0)? Otherwise, the co-ordinates usually start at the top left.]
Now, you find that something like (50, 50) moves to (75, 75) and (150, 50) moves to (125, 75).
x1 = xorigin + (x0 - xorigin) × scale.
Similarly for y, remembering whether you are going upwards or downwards.
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 828
That's one part of the problem. I need to determine the position and set off a click event for the old location. If the object is at 200,200 but the Graphics is scaled and the object is now showing at 100,100 I need to somehow set off a click event for that object so the appropriate action is taken (i.e. popup with more information for the JPanel, at that location, is shown). Any ideas on how to do that? Thanks for the info and post.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Coordinates and Mouse Events