Hi there,

I solved the 'picking' problem by adding a mouseListener, that checks if
the distance of the click from then nearest circle, here's the code:

canvas.addMouseListener(new MouseListener()

   {

   public void mouseClicked(MouseEvent me)
   {

    Point2D pt2d = (Point2D) me.getPoint();
    double dist = 800;
    Point2D clst = null;
    Iterator i = spots.iterator();

    while (i.hasNext())
    {

      Point2D spot = (Point2D) i.next();
      if (pt2d.distance(pt2d.getX(),pt2d.getY(),spot.getX(),spot.getY()) <
dist)
      {

        clst = spot;
        dist =
pt2d.distance(pt2d.getX(),pt2d.getY(),spot.getX(),spot.getY());
      }

    }
    int numb = accNum;
    if (clst != null && dist < 11)
    {
      canvas.getOverlays().add(new DrawCircle(clst,Color.YELLOW,numb));
      canvas.repaint();
        accNum++;
    }
   }

Not sure if it is tbe best solution but it does work - my next problem I
wantto tackle is how to turn on/off certain features of the svg map, such
as to turn of 'rivers', 'build-up areas' etc... any ideas?

Many thx

yasmin



> Hi Yasmin,
>
> [EMAIL PROTECTED] wrote on 03/01/2006 04:51:22 PM:
>
>> Just to let you know that I've solved my problem, rather than adding to
>> the svg document or to the dom I decided to draw on the Overlay method
> and
>> it works!
>
>    Don't you still have the 'picking' problem (knowing what circle was
> clicked on)?
>
>> here's the code:
>>
>> class Draw implements Overlay
>>                         {
>>                           public void paint (Graphics g)
>>                           {
>>                             Graphics2D g2d = (Graphics2D) g;
>>                             AffineTransform oldAT = g2d.getTransform();
>> g2d.transform(canvas.getRenderingTransform());
>>
>> g2d.fillOval((int)pt2d.getX(),(int)pt2d.getY(),22,22);
>
>> Only now I have the problem when I zoomIn the red-cirlcs also get
> bigger,
>> I dnt want them to change the size ...I was thinking of removing them
> when
>> zoomIn and then redraw them, I dnt know how code-efficient that is?
> Also,
>> how would I remove the circles?
>
>    You don't have any circles to 'remove' you simply need to change the
> size
> of the circles as the renderingTransform changes.  In short you will need
> to
> scale '22' by the scale factor of the renderingTransform:
>
>         double r =
> 22/Math.sqrt(canvas.getRenderingTransform().getDeterminant());
>         // ...
>         double x = pt2d.getX();
>         double y = pt2d.getX();
>         // Use 'shape 2D' since fillOval only takes integer coords and
>         // as you zoom in this will become increasingly inaccurate.
>         Ellipse2D e = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
>         g2d.fill(e);
>
>> > [EMAIL PROTECTED] wrote on 02/28/2006 05:00:41 PM:
>> >
>> >> Sorry, should had given the complete code, so you can see:
>> >>
>> >> canvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
>> >>
>> >> canvas.getUpdateManager().getUpdateRunnableQueue().invokeLater(new
>> >> Runnable() {
>> >
>> >    The UpdateManager and RunnableQueue only become available after the
>> > first
>> > rendering completes.  Alternately you could put your code to create
> the
>> > Circles in the SVG onload event callback.  There are good examples in
> the
>> > JavaScripting and JSVGCanvas sections of the Batik web site that
> covers
>> > how
>> > to do this (I would tend towards registering an 'onload' event
> handler).
>> >
>> > http://xmlgraphics.apache.org/batik/javaScripting.html
>> > http://xmlgraphics.apache.org/batik/svgcanvas.html
>> >
>> >>
>> >>                       public void run()
>> >>                       {
>> >>                         SVGDocument doc = canvas.getSVGDocument();
>> >>                         String SVGNS = "http://www.w3.org/2000/svg";;
>> >>                         Iterator i = spots.iterator();
>> >>                         Element g; // =
>> > doc.getElementById("circleGroup");
>> >>                         g = doc.createElementNS(SVGNS,"g");
>> >>                 g.setAttributeNS(null, "id", "circleGroup");
>> >>                 doc.getRootElement().appendChild(g);
>> >>
>> >>                         while (i.hasNext())
>> >>                         {
>> >>                           Point2D pt2d = (Point2D) i.next();
>> >>                           Element e =
>> > doc.createElementNS(SVGNS,"circle");
>> >>                           e.setAttributeNS(null,"cx",""+pt2d.getX());
>> >>                           e.setAttributeNS(null,"cy",""+pt2d.getY());
>> >>                           e.setAttributeNS(null,"r","8");
>> >>                           e.setAttributeNS(null,"fill","Red");
>> >>                           g.appendChild(e);
>> >>                         }
>> >>                       }
>> >>                       });
>> >>
>> >> Many thx
>> >>
>> >> yasmin
>> >>
>> >>
>> >> > Hi Yasmin,
>> >> >
>> >> > [EMAIL PROTECTED] wrote on 02/28/2006 03:37:12 PM:
>> >> >
>> >> >> ...yes you are right I haven't created a group with attribute 'id'
>> > set
>> >> > to
>> >> >> 'circlegroup', not sure how to :( I'm a complete novice to SVG and
>> > have
>> >> >> embarked on this ambitiouse project I'm finding it very difficult
> and
>> >> > time
>> >> >> is running out, pls ELP!
>> >> >
>> >> >   I would add something like:
>> >> >
>> >> >         if (g == null) {
>> >> >                 g = doc.createElementNS(SVGNS,"g");
>> >> >                 g.setAttributeNS(null, "id", "circleGroup");
>> >> >                 doc.getRootElement().appendChild(g);
>> >> >         }
>> >> >
>> >> >   Right after you call getElementById.
>> >> >   In your case you could probably also just skip the
>> >> > 'getElementById' and 'if' and just create the element in all cases
>> >> > (but you should avoid creating it twice - it's bad to have two
>> >> > elements with the same 'id' in one document).
>> >> >
>> >> >   BTW If at some point you want to remove the group (and
>> >> > consiquently all it's children) you can use:
>> >> >         g.getParentNode().removeChild(g);
>> >> >
>> >> >   Good luck!
>> >> >
>> >> >
> ---------------------------------------------------------------------
>> >> > To unsubscribe, e-mail:
> [EMAIL PROTECTED]
>> >> > For additional commands, e-mail:
>> > [EMAIL PROTECTED]
>> >> >
>> >> >
>> >> >
>> >>
>> >>
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail:
> [EMAIL PROTECTED]
>> >> For additional commands, e-mail:
> [EMAIL PROTECTED]
>> >>
>> >
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: [EMAIL PROTECTED]
>> > For additional commands, e-mail:
> [EMAIL PROTECTED]
>> >
>> >
>> >
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to