This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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.
Joined: Apr 09, 2002
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.
Joined: Oct 13, 2005
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 x, y 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.
Joined: Apr 09, 2002
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.