xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Hai Yun Tao" <hytparadi...@hotmail.com>
Subject (Urgent please!!!) Incorrect mouse button value triggered from DOM mouse events
Date Sun, 08 Apr 2007 14:52:50 GMT
I am creating a simple svg application using JSVGCanvas. I am trying to add 
mouse event listeners to the svg elements (rect, group etc.). I follow the 
standard routine, using EventTarget.addEventListener(). My listeners are 
able to receive mouse events correctly, but the MouseEvent.getButton() does 
not return the correct value (which should be 0 for left, 1 for middle, 2 
for right i think). To be more specific, no matter which button i pressed, i 
always get 1. I try to look in depth and i think there might be some mouse 
or key mask problems.

The following is the program that can reproduce the problem. You can do a 
quick test by copy and paste, and compile it with batik 1.7 and its required 
jar files. And try it again with the batik version 1.6. From what i got 
batik 1.6 will generate the right mouse button, but in 1.7, 
MouseEvent.getButton() always return 1.


* Copyright (c) 2005 - 2007 PeterHi
* All rights reserved.
* Copyright (c) 2005 - 2007 by Hai Yun Tao
* All rights reserved.

package com.peterhi.err;

import java.awt.BorderLayout;
import java.awt.Dimension;
import org.apache.batik.dom.svg12.SVG12DOMImplementation;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
import org.apache.batik.swing.gvt.GVTTreeRendererListener;
import org.w3c.dom.Element;
import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;
import org.w3c.dom.events.MouseEvent;
import org.w3c.dom.svg.SVGDocument;

public class Main extends javax.swing.JFrame
                  implements GVTTreeRendererListener,
                             EventListener {

    // the canvas
    private JSVGCanvas canvas;
    // the rect you can click, used to test mouse events
    private Element rect;

    public Main() {

    private void initComponentsEx() {
        // Set up canvas
        canvas = new JSVGCanvas();
        canvas.setPreferredSize(new Dimension(640, 480));
        add(canvas, BorderLayout.CENTER);

        try {
            // Create a document
            String ns = SVG12DOMImplementation.SVG_NAMESPACE_URI;
            SVG12DOMImplementation impl = (SVG12DOMImplementation)
            SVGDocument doc = (SVGDocument )
                impl.createDocument(ns, "svg", null);
            // Create a rectangle to reproduce the error
            rect = doc.createElementNS(ns, "rect");
            rect.setAttributeNS(null, "x", "0");
            rect.setAttributeNS(null, "y", "0");
            rect.setAttributeNS(null, "width", "1000");
            rect.setAttributeNS(null, "height", "1000");
            rect.setAttributeNS(null, "style", "fill:red");
            // put it into document

            // set document to the canvas
            canvas.setDocument(doc); // I think gvt renderer
                                     // will start working
        } catch (Exception ex) {


    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
    private void initComponents() {

    }// </editor-fold>

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Main().setVisible(true);

    // Variables declaration - do not modify
    // End of variables declaration

    public void gvtRenderingPrepare(GVTTreeRendererEvent e) { }

    public void gvtRenderingStarted(GVTTreeRendererEvent e) { }

    public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
        System.out.println("render completed, adding event listeners");
        // I appended my listener here, which works perfectly
        // back in batik 1.6
        EventTarget et = (EventTarget )rect;
        // subscribe listener here, take mouse down as
        // an example
        et.addEventListener("mousedown", this, false);

    public void gvtRenderingCancelled(GVTTreeRendererEvent e) { }

    public void gvtRenderingFailed(GVTTreeRendererEvent e) { }

    public void handleEvent(Event evt) {
        // Prove that we are able to received mouse down event
        System.out.println("mouse down trigerred!!!");

        // Here is where the problem comes
        // Check the value of MouseEvent.getButton(), which,
        // no matter what button is pressed, will always show 1

        MouseEvent me = (MouseEvent )evt;
        short button = me.getButton();
        System.out.println("button pressed: " + button);



Batik Version: Batik 1.7 Beta 1 (This problem does not occur in Batik 1.6)
Program IDE: NetBeans IDE 5.5
OS: Windows Server 2003
JDK version: Java SE Version 1.5.0 (build 1.5.0_11-b03)
Mouse: standard mouse with left, right, and a scrollable middle button

Please help me solve the problem as soon as possible. Thanks in advance.

Express yourself instantly with MSN Messenger! Download today it's FREE! 

To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org

View raw message