xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Qian Li" <qian....@googlemail.com>
Subject MouseEvent.getButton() always return 1
Date Tue, 18 Sep 2007 16:51:29 GMT
Dear All,

I have some problem with Batik Mouse Click event . Document of
MouseEvent.getButton() says
return value of 0 indicates left button, 1 for middle button, 2 for right
button. I have wrote a simple
java file to test it, however the getButton method always returns me 1. Does
anyone know if I did
something wrong?

Thank you very much.
Kind regards,
Qian

Here is the code, It's also in the attachment

/*
 * TestBatikMouseClick.java
 *
 * Created on September 18, 2007, 5:18 PM
 */

package testsvg;

import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.swing.JSVGCanvas;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;

public class TestBatikMouseClick extends javax.swing.JFrame {
    Document doc;
    Element svgRoot;
    JSVGCanvas svgCanvas;
    String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;

    /** Creates new form TestBatikMouseClick */
    public TestBatikMouseClick() {
        initComponents();

        svgCanvas = new JSVGCanvas();
        svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
        DOMImplementation impl = SVGDOMImplementation.getDOMImplementation
();
        String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
        doc = impl.createDocument(svgNS, "svg", null);

        svgRoot = doc.getDocumentElement();
        svgRoot.setAttribute("viewBox","0 0 500 500");
        svgRoot.setAttribute("preserveAspectRatio","xMinYMin meet");
        svgRoot.setAttributeNS(svgNS, "width", "500");
        svgRoot.setAttributeNS(svgNS, "height", "500");

        Element basicElement = doc.createElementNS(svgNS, "rect");
        basicElement.setAttributeNS(null, "x", "20");
        basicElement.setAttributeNS(null, "y", "20");
        basicElement.setAttributeNS(null, "height", "100");
        basicElement.setAttributeNS(null, "width", "100");
        basicElement.setAttributeNS(null, "style",
"fill:white;stroke:black;stroke-width:5;opacity:0.5");

        svgRoot.appendChild(basicElement);
        EventTarget t = (EventTarget)basicElement;
        t.addEventListener("click", new EventListener(){
            public void handleEvent(org.w3c.dom.events.Event evt) {
                doMouseClickEvent(evt); }
        }, false);


        svgCanvas.setDocument(doc);
        jPanelMain.add(svgCanvas);
    }

    public void doMouseClickEvent(org.w3c.dom.events.Event evt){
        org.w3c.dom.events.MouseEvent mEvt = (org.w3c.dom.events.MouseEvent
)evt;
        System.out.println("[BasicElementSVG::doMouseClickEvent] element
Mouse  Clicked  -- type="+mEvt.getType()+"   button="+mEvt.getButton());
    }

    private void initComponents() {
        jPanelMain = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jPanelMain.setLayout(new java.awt.BorderLayout());

        jPanelMain.setPreferredSize(new java.awt.Dimension(500, 500));
        getContentPane().add(jPanelMain, java.awt.BorderLayout.CENTER);

        pack();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TestBatikMouseClick().setVisible(true);
            }
        });
    }


    // Variables declaration - do not modify
    private javax.swing.JPanel jPanelMain;
    // End of variables declaration

}

Mime
View raw message