Wednesday 16 February 2005
More of building custom GUIs in java with batik
Par obi, Wednesday 16 February 2005 à 22:13 :: TreeGarden
Some developments about the FCU protoype I talked a few monthes ago. This principle is going to be used for a real simulator of an european military transport aircraft. I cannot show anything, but in terms of GUI, it's just a matter of adding (many) buttons.
While making new buttons with different behaviours, we faced a problem: how to get the coords of a mouse event, which cames in terms of pixels, in terms of Canvas units. The solution is simple and allows to go back and forth between screen space and Document space. The JSVGCanvas on which you are drawing provides a getViewBoxTransform() method, which gives the AffineTransform to convert from pixels space to document space. The code is simple and looks like that:
class MyCanvas extends JSVGCanvas {
...
((EventTarget)myElement).addEventListener("mousedown", new EventListener(){
public void handleEvent(org.w3c.dom.events.Event evt) {
AffineTransform userToScreen = getViewBoxTransform();
DOMMouseEvent q = (DOMMouseEvent)evt;
Point2D documentPoint = new Point2D.Double(0.0,0.0);
try
{
userToScreen.inverseTransform(new Point2D.Double(q.getClientX(), q.getClientY()), documentPoint);
}
catch(NoninvertibleTransformException e) {
System.out.println(e);
return;
}
//documentPoint is the coordinates of the event in the Document space. Neat !
...
So, what's the point ? Just that it took us half a day to fin the right way do it, and I want to keep a trace for the next time I'll try to do it.
And if this can help sommeone ...