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 Lasse Riis <lr...@tnb.aau.dk> on 2005/02/22 18:55:27 UTC

Example of using an overlay

Hello

I've been reading a lot, but I can't seem to gather a complete picture. 
So could someone present some example code, of say drawing a rectangle 
on an overlay?

I don't understand how the dimensions of the overlay relates to the 
dimensions of the SVG, and I don't even know what the dimensions of the 
SVG is (is it the pixels currently on screen?). I've been trying to use 
the getDocumentSize method on jsvgcanvas, inherited from jsvgcomponent, 
or is it? It gives a null-pointer exception....so somethings wrong I guess.

So could someone present some code of drawing a "small" rectangle in the 
center of an overlay on a JSVGCanvas?

Lasse Riis

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


Re: Example of using an overlay

Posted by Andres Toussaint <an...@onemileup.com>.
To paint in swing on top of a JSVGCanvas:

The only thing you need to do is to call PaintChildren in the paint 
method of the parent component to the JSVGCanvas, and then paint your 
overlay. The parent can be a JPanel, a JSVGScrollPane, a Viewport, etc. 
In the example provided the most relevant method is the paint(g), all 
other stuff is generic Batik canvas.

To do a swing paint() overlay, as adapted from the JSVGCanvas tutorial 
from the Baitk web site:

public class OverlayPanel extends JPanel {
	JSVGCanvas svgCanvas = new JSVGCanvas();
	boolean isVisible = false;

	public void paint(java.awt.Graphics g) {
		paintChildren(g); // give JSVGCanvas a chance to render
							// before we draw the overlay

		//Some overlay sample.
		if (isVisible) {  // in case the overlay should be drawn
									// only if we have a SVG document.
									// if you track the gvtRenderingCompleted, you
									// can have a boolean to activate once the
									// document is displayed.
			g.setColor(java.awt.Color.RED);
			g.fillrect(10,10,100,100);
		}
		//End of overlay sample.
	}

	//Some routine to load the SVG document
	public setSVGDocument(String uri) {
		try {
              svgCanvas.setURI(f.toURL().toString());
			  isVisible = false;
        } catch (IOException ex) {
              ex.printStackTrace();
        }
	}

	// Constructor that creates the listeners, and adds the
	//	JSVGCanvas to the panel.
	public OverlayPanel() {
         this.add("Center", svgCanvas);

         // 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) {
                 label.setText("Build Started...");
             }
             public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
                 label.setText("Build Done.");
                 frame.pack();
             }
         });

         svgCanvas.addGVTTreeRendererListener(new 
GVTTreeRendererAdapter() {
             public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
                 label.setText("Rendering Started...");
             }
             public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
                 label.setText("");
				  isVisible = true;  // Our document is visible.
										// Please note that you may need to
										// force a redraw of the swing component
										// to make it paint after this stage.
             }
         });
     }


On Feb 22, 2005, at 2:11 PM, Lasse Riis wrote:

> Andres Toussaint wrote:
>
>> Hi:
>>
>> Please define what do you mean by overlay.
>>
>> Is it a Swing paint() layer on top of your SVG JSVGCanvas? Or is it 
>> another element of SVG on top of the first element?
>>
> I mean a Swing paint() layer....
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
> For additional commands, e-mail: batik-users-help@xml.apache.org
>
>

Re: Example of using an overlay

Posted by Lasse Riis <lr...@tnb.aau.dk>.
Andres Toussaint wrote:

> Hi:
>
> Please define what do you mean by overlay.
>
> Is it a Swing paint() layer on top of your SVG JSVGCanvas? Or is it 
> another element of SVG on top of the first element?
>
I mean a Swing paint() layer....

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


Re: Example of using an overlay

Posted by Andres Toussaint <an...@onemileup.com>.
Hi:

Please define what do you mean by overlay.

Is it a Swing paint() layer on top of your SVG JSVGCanvas? Or is it 
another element of SVG on top of the first element?

For the dimensions of your SVG, the representation you see on screen is 
a conversion of the units you indicate in your SVG Doc (pts, inches, 
cm) and the viewBox attribute (if set).

The getDocumentSize method is available after the document is loaded, 
you you must make sure you are calling it in or after the 
documentLoadingCompleted event is triggered.

Andres.

On Feb 22, 2005, at 12:55 PM, Lasse Riis wrote:

> Hello
>
> I've been reading a lot, but I can't seem to gather a complete 
> picture. So could someone present some example code, of say drawing a 
> rectangle on an overlay?
>
> I don't understand how the dimensions of the overlay relates to the 
> dimensions of the SVG, and I don't even know what the dimensions of 
> the SVG is (is it the pixels currently on screen?). I've been trying 
> to use the getDocumentSize method on jsvgcanvas, inherited from 
> jsvgcomponent, or is it? It gives a null-pointer exception....so 
> somethings wrong I guess.
>
> So could someone present some code of drawing a "small" rectangle in 
> the center of an overlay on a JSVGCanvas?
>
> Lasse Riis
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
> For additional commands, e-mail: batik-users-help@xml.apache.org
>
>



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


Re: Example of using an overlay

Posted by Thomas DeWeese <Th...@Kodak.com>.
Lasse Riis wrote:

> I've been reading a lot, but I can't seem to gather a complete picture. 
> So could someone present some example code, of say drawing a rectangle 
> on an overlay?

   The swing.gvt.AbstractZoomInteractor does exactly that based on
mouse events, I would copy the code and start from there.

> I don't understand how the dimensions of the overlay relates to the 
> dimensions of the SVG,

    They don't directly.  The overlay is drawn in screen coordinates.
If you want to know how the SVG maps to the screen you can call
'getViewBoxTransform()' to get the mapping from the SVG viewBox to
screen pixels.

> and I don't even know what the dimensions of the 
> SVG is (is it the pixels currently on screen?). I've been trying to use 
> the getDocumentSize method on jsvgcanvas, inherited from jsvgcomponent, 
> or is it? It gives a null-pointer exception....so somethings wrong I guess.

    Most of this stuff only makes sense to try and work with after
the first GVT rendering completes.  So structure your code to register
a GVTTreeRendererListener and only start poking the canvas after the
image is rendered the first time.

> So could someone present some code of drawing a "small" rectangle in the 
> center of an overlay on a JSVGCanvas?

     class myOverlay implements Overlay {
         protected BasicStroke wide = new BasicStroke(3);
         protected BasicStroke narrow = new BasicStroke(1);

         /**
          * Paints this overlay.
          */
         public void paint(Graphics g) {
            Graphics2D g2d = (Graphics2D)g;

            g2d.setColor(Color.black);
            g2d.setStroke(wide);
            g2d.drawRect(50, 50, 100, 100);  // draw rectangle.

            g2d.setColor(Color.white);
            g2d.setStroke(narrow);
            g2d.drawRect(50, 50, 100, 100);  // draw rectangle.
         }
     }

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