You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by hardc0d3r <ha...@gmail.com> on 2007/08/28 18:52:52 UTC

can't draw on canvas unless there is a pre-drawn element

i cannot seem to draw on the canvas by mouse click if there is no drawn
element on load.. i only experienced this today. i didn't change my code in
the canvas.. does anyone had the same problem?
-- 
View this message in context: http://www.nabble.com/can%27t-draw-on-canvas-unless-there-is-a-pre-drawn-element-tf4342898.html#a12371778
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Re: can't draw on canvas unless there is a pre-drawn element

Posted by hardc0d3r <ha...@gmail.com>.
you're right about setting the dimension of the canvas.. thank you!!
but why does it work sometimes even without setting the width and height?
-- 
View this message in context: http://www.nabble.com/can%27t-draw-on-canvas-unless-there-is-a-pre-drawn-element-tf4342898.html#a12388166
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Re: can't draw on canvas unless there is a pre-drawn element

Posted by th...@kodak.com.
Hi HArdc0d3r,

hardc0d3r <ha...@gmail.com> wrote on 08/29/2007 12:08:19 AM:

> here is where the canvas is

   The code looks ok at first glance.  What exactly happens
when you start your applet and click? 

   You have a bunch of prints in the listeners you register
what prints get printed?

   Also I noticed that you add a map 'background' try giving it 
a fill and see if it get's rendered.

   Also your root SVG element has no width and height you might
try giving it a width and height.



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


Re: can't draw on canvas unless there is a pre-drawn element

Posted by hardc0d3r <ha...@gmail.com>.
here is where the canvas is

public class MapCanvas extends JPanel implements Observer {
    
    private static String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
    private JSVGCanvas svgCanvas;
    private SVGDocument doc;
    private SVGSVGElement svgRoot;
    private Element mapBG;
    private SVGPoint downPt;
    private RoadModel roadModel;
    
    public MapCanvas(SVGDocument doc, RoadModel roadModel) {
        setLayout(new BorderLayout());
        setBorder(new EtchedBorder());
        this.doc = doc;
        this.roadModel = roadModel;
        
        initComponents();
        add(svgCanvas, BorderLayout.CENTER);
    }
    
    private void initComponents() {
        svgCanvas = new JSVGCanvas();
    	svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
        DOMImplementation impl =
SVGDOMImplementation.getDOMImplementation();
        if(doc == null)
            doc = (SVGDocument)impl.createDocument(svgNS, "svg", null);

        registerListeners();
        svgCanvas.setDocument(doc);
    }
    
    public void registerListeners() {
        svgCanvas.addSVGDocumentLoaderListener(new
SVGDocumentLoaderAdapter() {
            public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
                System.out.println("Document Loading...");
            }
            public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
                System.out.println("Document Loaded.");
            }
        });
        svgCanvas.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {
            public void gvtBuildStarted(GVTTreeBuilderEvent e) {
                System.out.println("GVT Build Started...");
            }
            public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
                System.out.println("GVT Build Done...");
            }
        });
        svgCanvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
            public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
                System.out.println("Renderer Started...");
            }
            public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
                System.out.println("Renderer Completed...");
               
                UpdateManager um = svgCanvas.getUpdateManager();
                um.getUpdateRunnableQueue().invokeLater(new Runnable() {
                    public void run() {
                    	initCanvas();
                    }
                });
            }
        });
        
        svgRoot = doc.getRootElement();
        EventTarget t = (EventTarget)svgRoot;
        t.addEventListener("SVGLoad", new OnLoadAction(), false);
    }
    
    public class OnLoadAction implements EventListener {
        public void handleEvent(Event evt) { }
    }
    
    private void initCanvas() throws DOMException {
        try {
            doc = svgCanvas.getSVGDocument();
            svgRoot = doc.getRootElement();
            
            mapBG = doc.createElementNS(svgNS, "rect");
            mapBG.setAttributeNS(null, "id", "mapBG");
            mapBG.setAttributeNS(null, "x", "0");
            mapBG.setAttributeNS(null, "y", "0");
            mapBG.setAttributeNS(null, "width", "1500");
            mapBG.setAttributeNS(null, "height", "1153");
            mapBG.setAttributeNS(null, "pointer-events", "fill");
            mapBG.setAttributeNS(null, "style",
"fill:white;fill-opacity:.01");
            svgRoot.appendChild(mapBG);
            
            Element x = doc.createElementNS(svgNS, "rect");
            x.setAttribute("x", "25");
            x.setAttribute("y", "25");
            x.setAttribute("width", "100");
            x.setAttribute("height", "100");
            x.setAttribute("stroke", "#000000");
            svgRoot.appendChild(x);
            
            EventTarget t = (EventTarget)svgRoot;
            t.addEventListener("mouseup", new OnUpAction(), false);
        }
        catch(Exception e) {
        }
    }
    
    private SVGPoint localPt(Element elem, float x, float y) {
        SVGDocument svgDocument = svgCanvas.getSVGDocument();
        SVGMatrix mat = ((SVGLocatable)elem).getScreenCTM();
        SVGMatrix imat = mat.inverse();
        SVGPoint cPt = svgDocument.getRootElement().createSVGPoint();
        cPt.setX(x);
        cPt.setY(y);
        cPt = cPt.matrixTransform(imat);
        return cPt;
    }
    
    private class OnUpAction implements EventListener {
        public void handleEvent(Event evt) {
            DOMMouseEvent elEvt = (DOMMouseEvent)evt;
            EventTarget actionNode = elEvt.getTarget();
            Element actionElement = (Element)actionNode;
            downPt = localPt(mapBG, elEvt.getClientX(), elEvt.getClientY());
            
            System.out.println("DRAW CIRCLE");
            drawCircle(downPt);
        }
    }
    
    private void drawCircle(float x, float y) {
        Element handle = doc.createElementNS(svgNS, "circle");
        handle.setAttribute("id", "handle_" + x + "_" + y);
        handle.setAttribute("cx", x + "");
        handle.setAttribute("cy", y + "");
        handle.setAttribute("r", "5");
        handle.setAttribute("stroke", "#0000ff");
        handle.setAttribute("stroke-width", "2");
        handle.setAttribute("fill", "#FFFFFF");
        svgRoot.appendChild(handle);
    }
    
    private void drawCircle(SVGPoint pt) {
        drawCircle(pt.getX(), pt.getY());
    }

    public void update(Observable obs, Object arg) {
    }
}


and here is how i call it in my applet

public void init() {
        setNativeLookAndFeel();
        
        try  {
            map = loadMap("test.svg");
            doc = stringToSVG(map);
        }
        catch(Exception e) {
        }
        
        roadModel = new RoadModel();
        mapCanvas = new MapCanvas(doc, roadModel);
        mapEditorGUI = new MapEditorGUI(this, layerTree, mapCanvas,
roadModel, roadPanel);
        
        roadModel.addObserver(mapCanvas);
        roadModel.addObserver(mapEditorGUI);
        
        add(mapEditorGUI);
    }
-- 
View this message in context: http://www.nabble.com/can%27t-draw-on-canvas-unless-there-is-a-pre-drawn-element-tf4342898.html#a12380553
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Re: can't draw on canvas unless there is a pre-drawn element

Posted by th...@kodak.com.
Hi Hardc0d3r,

hardc0d3r <ha...@gmail.com> wrote on 08/28/2007 01:05:29 PM:

> ok.. my mistake.. it seems that i cannot draw anything on the canvas 
with or
> without a predrawn element.. this happens to me sometimes but i ignore 
it
> and just rerun my app.. now i cannot really draw anything..

   Sounds like a thread deadlock.  Is the app responsive at all?
Are you making your changes to the document in the RunnableQueue?
Can you reproduce the problem with a stripped down version?


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


Re: can't draw on canvas unless there is a pre-drawn element

Posted by hardc0d3r <ha...@gmail.com>.
ok.. my mistake.. it seems that i cannot draw anything on the canvas with or
without a predrawn element.. this happens to me sometimes but i ignore it
and just rerun my app.. now i cannot really draw anything..
-- 
View this message in context: http://www.nabble.com/can%27t-draw-on-canvas-unless-there-is-a-pre-drawn-element-tf4342898.html#a12372001
Sent from the Batik - Users mailing list archive at Nabble.com.


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