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/23 22:53:26 UTC

Trying to draw my overlay

Hi

I've been looking in to your answers and trying to build my app. But I'm 
stuck now. I restructured it to use some methods to build the app, as 
oppose to a huge main(). It should be very clear to anyone that I am not 
very familiar with Java. So sometimes I have no idea what I'm doing. And 
usually just follow my IDE's (eclipse) reccommendations  (hence the 
'static' methods). So I hope you don't spill your coffee laughing when 
looking at my code :). But bare in mind, this is the first thing I've 
ever written.

I just need this to draw some simple visible image on top of my SVG and 
then later figure out how to move it around. But since I have no clue 
where to start, I think I've started out wrong, so maybe you could give 
me input on how to restructure this poor thing to make my foundation 
proper. Here we go:

import java.awt.*;
import java.awt.Graphics;

import javax.swing.*;

import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.gvt.GVTTreeRendererAdapter;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;

public class mapIt {
   
    public static void main(String[] args) {
        JFrame frame = buildGUI();
       
        frame.setSize(200, 600);
        frame.setVisible(true);
             
   }

   public static JSVGCanvas buildCanvas(){
       JSVGCanvas canvas = new JSVGCanvas();

       canvas.setBackground(Color.green);
       canvas.setURI("file:/home/riis/P2/maps/gangen.svg");

       canvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
          public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
                  System.out.print("Rendering done");
                  Graphics box = new Graphics();
                  paint(box);
           }
       });
      
       return canvas;
   }
   
   public static JFrame buildGUI(){
           JFrame f = new JFrame("PANTS - MapIt");
          
           JSVGCanvas svgCanvas = buildCanvas();
      
           JSlider slider = new JSlider();
           
           JPanel panel = new JPanel(new BorderLayout());

           panel.add("North", slider);

           panel.add("Center", svgCanvas);

           f.getContentPane().add(panel);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        return f;
   }

   public void paint(java.awt.Graphics g) {
           g.setColor(java.awt.Color.RED);
           g.fillRect(10,10,100,100);
       }
  
}


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


Re: Trying to draw my overlay

Posted by James Shaw <he...@btopenworld.com>.
Lasse Riis wrote:

> Hi
>
> I've been looking in to your answers and trying to build my app. But 
> I'm stuck now. I restructured it to use some methods to build the app, 
> as oppose to a huge main(). It should be very clear to anyone that I 
> am not very familiar with Java. So sometimes I have no idea what I'm 
> doing. And usually just follow my IDE's (eclipse) reccommendations  
> (hence the 'static' methods). So I hope you don't spill your coffee 
> laughing when looking at my code :). But bare in mind, this is the 
> first thing I've ever written.
>
Lasse, you're completely mad! :-P  One problem I see with your code is 
manually creating a Graphics object and trying to paint to it.  I think 
you should try doing Component.getGraphics(), but I'm not sure which 
component you should choose to paint to.  Personally, I would create the 
image in the SVG DOM, and use event handlers to move it around.

Hope that helps, at least a little bit!
James Shaw

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


Re: Trying to draw my overlay

Posted by Tonny Kohar <to...@kiyut.com>.
Hi,

> But in order to get that, I'd like to know if anyone has knowledge of 
> projects that use Batik to do something similar to what I'm trying to 
> do; using and overlay to draw (and perhaps move) some graphics over an 
> SVG-image. I'd like to study some source code. I think that would help 
> me a lot.

Try to look at the source code for Batik Squigle/SVGBrowser especially
the Scale/Zoom function, it draw dash-ed rectangle as overlay when you
drag the mouse with ctrl key pressed.

Regards
Tonny Kohar
-- 
Sketsa 
SVG Graphics Editor
http://www.kiyut.com



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


Re: Trying to draw my overlay

Posted by Lasse Riis <lr...@tnb.aau.dk>.
Thomas DeWeese wrote:

>    Using an overlay is pretty trivial, your problems seem to mostly be
> with the Java language.  This is not an appropriate forum to learn
> Java.  There are _lots_ of resources for that.

I feared this would come up :)
Yes, I thinks this is the core of my problem; I'm learning OOP, Java and 
Batik all at the same time, programming my first GUI app. I apologise If 
I've been too annoying with my questions. But I hope to return with 
more, once I get my Java straight.

But in order to get that, I'd like to know if anyone has knowledge of 
projects that use Batik to do something similar to what I'm trying to 
do; using and overlay to draw (and perhaps move) some graphics over an 
SVG-image. I'd like to study some source code. I think that would help 
me a lot.

Doing all sorts of boring classes using Java tutorials will not help me 
getting this GUI done :)

Lasse Riis

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


Re: Trying to draw my overlay

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

> Do you want to keep track (i.e. store it) of your overlay information? 
> or will it be volatile, on a per session basis?
>
Volatile, I Don't see any reason to embed them in the SVG and/or let it 
be "saveable". But if the option can be kept open relatively easy, it 
would of course be better. But primarily I want to be able to update the 
display often without any slow renders.

> You want to have the position-indicator to remain the same size (on 
> screen), regardless of the zoom level of your map, right?

    Undecided. I'd like the coordinate-mapping between the overlay and 
the map to stay the same if possible. But I guess a constant-sized 
indicator will look better. The problem is, we might have to indicate 
areas of the map, if our positioning engine is not precise enough, in 
that case, the tranparent circle, rectangle, whatever, will have to 
scale with the map

>
> You want to indicate access routes, elevator position, etc. Right?

Not sure I know what you mean by this. But I could imagine indicating 
exits, stairs and such with a highlighted area. But I guess that could 
be done in the SVG, but having it in the overlay would of course allow 
for switching it on and off. And that would be a nice feature.

>
> How are the new positions will be calculated? By moving the 
> indicators, or by modifing the Zoom-pan of the map?
>
Not sure what you mean here either. The new positions will arrive as UDP 
datagrams from the positioning backend/engine and when they do, the 
indicator should move. When it gets closer to the edge of the zoom, the 
map should pan. So both I guess. But if one option is terribly complex, 
then the simpler might have to do.

Lasse Riis

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


Re: Trying to draw my overlay

Posted by Andres Toussaint <an...@onemileup.com>.
Do you want to keep track (i.e. store it) of your overlay information? 
or will it be volatile, on a per session basis?

You want to have the position-indicator to remain the same size (on 
screen), regardless of the zoom level of your map, right?

You want to indicate access routes, elevator position, etc. Right?

How are the new positions will be calculated? By moving the indicators, 
or by modifing the Zoom-pan of the map?

This is important to define the best approach.

Andres.

On Feb 28, 2005, at 10:38 AM, Lasse Riis wrote:

> Andres Toussaint wrote:
>
>> Lasse:
>>
>> Could you detail what it is you want to do with the overlays. I can 
>> think of two uses for them:
>>
>> 2. Not related to the SVG. That is, your SVG will be simply the 
>> "background image", for your Java 2D Graphics canvas.
>
> This is exactly what it's for. The SVG will be a map of the building, 
> we use SVG because it scales so well on different 
> window/monitor-sizes. So I'll just be using the overlay to draw some 
> form of position-indicator, an arrow, or a dot something like that. 
> And then, as new positions are calculated, probably move it and draw a 
> trail from the previous position. I think an overlay is the best 
> option for this.
>
> But I need to read up on Java as a whole. So I'd like to see some 
> source code. Preferrably something that is very similar to what I'm 
> doing.
>
> 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: Trying to draw my overlay

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

> Lasse:
>
> Could you detail what it is you want to do with the overlays. I can 
> think of two uses for them:
>
> 2. Not related to the SVG. That is, your SVG will be simply the 
> "background image", for your Java 2D Graphics canvas.

This is exactly what it's for. The SVG will be a map of the building, we 
use SVG because it scales so well on different window/monitor-sizes. So 
I'll just be using the overlay to draw some form of position-indicator, 
an arrow, or a dot something like that. And then, as new positions are 
calculated, probably move it and draw a trail from the previous 
position. I think an overlay is the best option for this.

But I need to read up on Java as a whole. So I'd like to see some source 
code. Preferrably something that is very similar to what I'm doing.

Lasse Riis

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


Re: Trying to draw my overlay

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

Could you detail what it is you want to do with the overlays. I can 
think of two uses for them:

1. Related to the SVG content. to draw Scale-unaware selection objects 
(such as a bounding box, a resize or reshape indicator, etc.) or 
elements that are not part of the drawing, such as guides. Or you want 
to annotate your SVG with Graphics2D objects, for some specific reason.

2. Not related to the SVG. That is, your SVG will be simply the 
"background image", for your Java 2D Graphics canvas.

With a little more information as to what it is you want to accomplish 
with the overlays, we may be able to help you better.

I have done selection objects both as an overlay and as part of the SVG 
document, so, although the overlay seems like a simpler solution, event 
handling makes the option of having the selection elements as part of 
the SVG a better solution (but you have to make these objects "scale 
and pan aware" so they remain the same size regardless of your SVG 
scale factor.)

Andres.

On Feb 28, 2005, at 6:04 AM, Lasse Riis wrote:

> Thomas DeWeese wrote:
>
>>    Using an overlay is pretty trivial, your problems seem to mostly be
>> with the Java language.  This is not an appropriate forum to learn
>> Java.  There are _lots_ of resources for that.
>
> I feared this would come up :)
> Yes, I thinks this is the core of my problem; I'm learning OOP, Java 
> and Batik all at the same time, programming my first GUI app. I 
> apologize If I've been too annoying with my questions. But I hope to 
> return with more, once I get my Java straight.
>
> But in order to get that, I'd like to know if anyone has knowledge of 
> projects that use Batik to do something similar to what I'm trying to 
> do; using and overlay to draw (and perhaps move) some graphics over an 
> SVG-image. I'd like to study some source code. I think that would help 
> me a lot.
>
> Doing all sorts of boring classes using Java tutorials will not help 
> me getting this GUI done :)
>
> Lasse Riis
>
> P.S. Our mailserver went down, and it seems this message didn't reach 
> the mailinglist. So if this is a duplicate, I apologize.
>
> ---------------------------------------------------------------------
> 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: Trying to draw my overlay

Posted by Lasse Riis <lr...@tnb.aau.dk>.
Thomas DeWeese wrote:

>    Using an overlay is pretty trivial, your problems seem to mostly be
> with the Java language.  This is not an appropriate forum to learn
> Java.  There are _lots_ of resources for that.

I feared this would come up :)
Yes, I thinks this is the core of my problem; I'm learning OOP, Java and 
Batik all at the same time, programming my first GUI app. I apologize If 
I've been too annoying with my questions. But I hope to return with 
more, once I get my Java straight.

But in order to get that, I'd like to know if anyone has knowledge of 
projects that use Batik to do something similar to what I'm trying to 
do; using and overlay to draw (and perhaps move) some graphics over an 
SVG-image. I'd like to study some source code. I think that would help 
me a lot.

Doing all sorts of boring classes using Java tutorials will not help me 
getting this GUI done :)

Lasse Riis

P.S. Our mailserver went down, and it seems this message didn't reach 
the mailinglist. So if this is a duplicate, I apologize.

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


Re: Trying to draw my overlay

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

> No, but It won't compile without it either. At least not here. My IDE 
> tells me to make it static, but i don't understand why.

    But the problem was that you were using 'java.awt.Color.RED'
not 'java.awt.Color.red'.  What is below works.

> Someone should do a thorough howto on using an overlay in a jsvgcanvas 
> and put it in the wiki. At least I think so :)

    Using an overlay is pretty trivial, your problems seem to mostly be
with the Java language.  This is not an appropriate forum to learn
Java.  There are _lots_ of resources for that.

-----

import java.awt.*;
import java.awt.Graphics2D;
import javax.swing.*;

import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.gvt.GVTTreeRendererAdapter;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
import org.apache.batik.swing.gvt.Overlay;

public class MapIt implements Overlay {
     public static void main(String[] args) {
         JFrame frame = buildGUI();
         frame.setSize(200, 600);
         frame.setVisible(true);
     }

     public static JSVGCanvas buildCanvas(){
         final JSVGCanvas canvas = new JSVGCanvas();

         canvas.setBackground(Color.green);
 
canvas.setURI("file:/C:/Apps/cygwin/home/l449433/dev/batik/xml-batik/samples/anne.svg");

         canvas.getOverlays().add(new MapIt());
         return canvas;
     }
     public static JFrame buildGUI(){
         JFrame f = new JFrame("PANTS - MapIt");
         JSVGCanvas svgCanvas = buildCanvas();
         JSlider slider = new JSlider();
         JPanel panel = new JPanel(new BorderLayout());

         panel.add("North", slider);

         panel.add("Center", svgCanvas);

         f.getContentPane().add(panel);

         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         return f;
     }

     public void paint(Graphics g) {
         g.setColor(java.awt.Color.red);
         g.fillRect(10,10,100,100);
     }

}

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


Re: Trying to draw my overlay

Posted by Lasse Riis <lr...@tnb.aau.dk>.
Thomas DeWeese wrote:

>
>>   public static void paint(Graphics g) {
>
>            ^^^^^^ This wasn't here last time.  It won't compile
> with this.

No, but It won't compile without it either. At least not here. My IDE 
tells me to make it static, but i don't understand why.
Someone should do a thorough howto on using an overlay in a jsvgcanvas 
and put it in the wiki. At least I think so :)

Lasse

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


Re: Trying to draw my overlay

Posted by Thomas DeWeese <Th...@Kodak.com>.
Lasse Riis wrote:
> Thomas DeWeese wrote:
> 
>> You are close, basically you want to make your class implement
>> the Overlay interface from Batik, and associate your mapIt instance
>> with the canvas as an overlay
> 
> Well I guess I'm getting there, but I need more help. Perhaps a good 
> restructure of my app is in order?
> 
> 
> I got this now, and of course it still doesn't work. (I didn't quite 
> understand James' ideas):
> 

>   public static void paint(Graphics g) {
            ^^^^^^ This wasn't here last time.  It won't compile
with this.

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


Re: Trying to draw my overlay

Posted by Lasse Riis <lr...@tnb.aau.dk>.
Thomas DeWeese wrote:

> You are close, basically you want to make your class implement
> the Overlay interface from Batik, and associate your mapIt instance
> with the canvas as an overlay

Well I guess I'm getting there, but I need more help. Perhaps a good 
restructure of my app is in order?


I got this now, and of course it still doesn't work. (I didn't quite 
understand James' ideas):

import java.awt.*;
import java.awt.Graphics2D;
import javax.swing.*;

import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.gvt.GVTTreeRendererAdapter;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
import org.apache.batik.swing.gvt.Overlay;

public class mapIt implements Overlay{
   
    public static void main(String[] args) {
        JFrame frame = buildGUI();
       
        frame.setSize(200, 600);
        frame.setVisible(true);
             
   }

   public static JSVGCanvas buildCanvas(){
       final JSVGCanvas canvas = new JSVGCanvas();

       canvas.setBackground(Color.green);
       canvas.setURI("file:/home/riis/P2/maps/gangen.svg");

       canvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
          public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
                  System.out.print("Rendering done");
                  Graphics box = canvas.getGraphics();
                  paint(box);
           }
       });

       canvas.getOverlays().add(new mapIt());    
      
       return canvas;
   }
   
   public static JFrame buildGUI(){
           JFrame f = new JFrame("PANTS - MapIt");
          
           JSVGCanvas svgCanvas = buildCanvas();
      
           JSlider slider = new JSlider();
           
           JPanel panel = new JPanel(new BorderLayout());

           panel.add("North", slider);

           panel.add("Center", svgCanvas);

           f.getContentPane().add(panel);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        return f;
   }

   public static void paint(Graphics g) {
           g.setColor(java.awt.Color.RED);
           g.fillRect(10,10,100,100);
       }
  
}


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


Re: Trying to draw my overlay

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

> I've been looking in to your answers and trying to build my app. But I'm 
> stuck now. I restructured it to use some methods to build the app, as 
> oppose to a huge main(). [...] Here we go:
> 

You are close, basically you want to make your class implement
the Overlay interface from Batik, and associate your mapIt instance
with the canvas as an overlay

import org.apache.batik.swing.gvt.Overlay;  // Interface

public class mapIt implements Overlay {  // Note Overlay!!!!
   public static JSVGCanvas buildCanvas(){
       JSVGCanvas canvas = new JSVGCanvas();

       canvas.setBackground(Color.green);
       canvas.setURI("file:/home/riis/P2/maps/gangen.svg");

	// It will now call your paint method whenever it
	// update the canvas!
       canvas.getOverlays().add(new mapIt());

       return canvas;
   }

     public static JFrame buildGUI(){
           JFrame f = new JFrame("PANTS - MapIt");
                    JSVGCanvas svgCanvas = buildCanvas();
                JSlider slider = new JSlider();
                     JPanel panel = new JPanel(new BorderLayout());

           panel.add("North", slider);

           panel.add("Center", svgCanvas);

           f.getContentPane().add(panel);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        return f;
   }

   public void paint(java.awt.Graphics g) {
           g.setColor(java.awt.Color.RED);
           g.fillRect(10,10,100,100);
       }

}

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