public class DnD {
public static void main(String[] args) { JFrame f = new JFrame("Batik"); MouseEvent2 app = new MouseEvent2(f); f.getContentPane().add(app.createComponents());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
f.setBounds(50,50,400, 400);
f.setVisible(true);
}
JFrame frame;
JButton button = new JButton("Load...");
JButton zoomInButton = new JButton("Zoom In");
....
public JComponent createComponents() {
.....
// Set the JSVGCanvas listeners.
svgCanvas.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() {
public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
label.setText("Document Loading...");
}
public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
label.setText("Document Loaded.");
}
});
svgCanvas.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {
public void gvtBuildStarted(GVTTreeBuilderEvent e) {
}
public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
label.setText("Build Done."); svgCanvas.setMySize((Dimension)svgCanvas.getSVGDocumentSize());
frame.setSize((Dimension)svgCanvas.getSVGDocumentSize()); frame.pack(); } });
svgCanvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() { public void gvtRenderingPrepare(GVTTreeRendererEvent e) { label.setText("Rendering Started..."); } public void gvtRenderingCompleted(GVTTreeRendererEvent e) { label.setText(""); } });
// ggf the mouse event stuff
// Forces the canvas to always be dynamic even if the current // document does not contain scripting or animation.
svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
svgCanvas.addSVGLoadEventDispatcherListener
(new SVGLoadEventDispatcherAdapter() {
public void svgLoadEventDispatchStarted
(SVGLoadEventDispatcherEvent e) {
// At this time the document is available...
document = svgCanvas.getSVGDocument();
svgCanvas.setSVGRoot();
window = svgCanvas.getUpdateManager(). getScriptingEnvironment().createWindow();
// Registers the listeners on the document // just before the SVGLoad event is // dispatched. registerListeners(); } });
.... return panel; }
public void registerListeners() {
svgRoot = (SVGOMElement)svgCanvas.getSVGDocument().getRootElement();
EventTarget root = (EventTarget)svgRoot;
// Adds a 'onclick' listener
root.addEventListener( SVGConstants.SVG_MOUSEDOWN_EVENT_TYPE , new OnNewClickAction(), false);
}
public void removeListeners() {
svgRoot = (SVGOMElement)svgCanvas.getSVGDocument().getRootElement();
EventTarget root = (EventTarget)svgRoot;
// Remove a 'onclick' listener
root.removeEventListener( SVGConstants.SVG_MOUSEDOWN_EVENT_TYPE , new OnNewClickAction(), false);
}
public void registerListeners(EventTarget aTarget) {
aTarget.addEventListener( SVGConstants.SVG_MOUSEDOWN_EVENT_TYPE , new OnDownAction(), false);
aTarget.addEventListener( SVGConstants.SVG_MOUSEUP_EVENT_TYPE , new OnUpAction(), false);
aTarget.addEventListener( SVGConstants.SVG_MOUSEMOVE_EVENT_TYPE , new OnMoveAction(), false);
}
public void removeListeners(EventTarget aTarget) {
aTarget.removeEventListener( SVGConstants.SVG_MOUSEDOWN_EVENT_TYPE , new OnDownAction(), false);
aTarget.removeEventListener( SVGConstants.SVG_MOUSEUP_EVENT_TYPE , new OnUpAction(), false);
aTarget.removeEventListener( SVGConstants.SVG_MOUSEMOVE_EVENT_TYPE , new OnMoveAction(), false);
}
....
public class OnMoveAction implements EventListener {
public void handleEvent(Event evt) {
DOMMouseEvent q = (DOMMouseEvent)evt; if(selected){
NamedNodeMap map = node.getAttributes();
//place it so that the mouse-pointer is in the center of the object
SVGOMElement svgCurrentElement = (SVGOMElement)target;
SVGContext context = svgCurrentElement.getSVGContext();
Rectangle2D box2D = context.getBBox();
int height = (int)box2D.getX();
int width = (int)box2D.getY();
AffineTransform AT = new AffineTransform();
AT.translate((double)(q.getClientX()-width/2),(double)(q.getClientY()-height/2));
context.setScreenTransform( AT );
}
}
}
/* public class OnMouseDownAction implements EventListener {
public void handleEvent(Event evt) {
// Select Element to drop
DOMMouseEvent dme = (DOMMouseEvent)evt;
EventTarget nuovoTarget = dme.getTarget();
svgCurrentElement = (SVGOMElement)nuovoTarget;
}
} */
public class OnDownAction implements EventListener {
public void handleEvent(Event evt) {
DOMMouseEvent q = (DOMMouseEvent)evt;
target = q.getTarget();
removeListeners();
registerListeners(target);
node = (Node)target;
selected = true; }
}
public class OnUpAction implements EventListener {
public void handleEvent(Event evt) { removeListeners(target);
registerListeners();
target = null;
selected = false; }
}
}
}
--
Email.it, the professional e-mail, gratis per te: http://www.email.it/f
Sponsor:
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid=&d=15-5
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]