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 Peppe <pe...@gmail.com> on 2006/12/12 22:53:36 UTC

insert polygon

Hi, i want to add polygon to my svgCanvas...i wrote this code but there is a
problem: when i click button "polygon" this activate 3 listener,
initializing flag = true, create = true and elem = 6. When i click on
svgCanvas point clicked is saved into a string "points", then when i reclick
the point is saved (concat) into "points", when i click 2 times string
"points" is add to attribute "points" and polygon is created. But when i
reclick button "polygon" and then when i click on svgCanvas the points is
saved 2 times into string "points"...
for example:
insert first polygon

points = 192,168  340,430 ...

when i reclick button polygon

points = 156,402  156,402  304,170  304,170 ....

why?

private class OnDownAction implements EventListener {
	        public void handleEvent(Event evt) {
	        	Element svgRoot = svgCanvas.getSVGDocument().getRootElement();
		    	if(create == true){
	        		DOMMouseEvent elEvt = (DOMMouseEvent)evt;  
		            actionNode = elEvt.getTarget();
		            action = DRAG;
		            Node n = ((Element)elEvt.getTarget());
		            startPt = localPt((Element)n, elEvt.getClientX(),
elEvt.getClientY());
	        		if(elem == 6){
		            
		        	  if(flag = true){

		            	points = points+startPt.getX()+","+startPt.getY()+" ";		            		
		            	System.out.println(points);

		        		if(elEvt.getDetail() == 2){   
		        			flag = false;
		        			polygon = svgCanvas.getSVGDocument().createElementNS(svgNS,
"polygon");
			            	polygon.setAttributeNS(null, "id", "bgpolygon");
			            		
			            	polygon.setAttributeNS(null, "points", points);
			            	polygon.setAttributeNS(null, "style", "fill:rgb("+
newColor.getRed()+ "," + newColor.getGreen()+","+newColor.getBlue()+");
stroke:none; stroke-width:0");
		            	
			            	svgRoot.appendChild(polygon);
			            	create = false;
			            	actionNode = null;
			            	elem = 0;
			            	points = "";
		        		}
		        	   } 
	                }				
	        	}
	        }
	    }
		
		 private class OnUpAction implements EventListener {
	        public void handleEvent(Event evt) {
				if(create == true && elem != 6){
		    		
		                Element svgRoot =
svgCanvas.getSVGDocument().getRootElement();
		                if(elem == 5){
			            
			            	Element text ....
							....
		                }	
		               			
						if(elem != 6){
						create = false;
		                actionNode = null;
		                elem = 0;
						}
		    	}
	        }
	    }
		
		 private class OnMoveAction implements EventListener {
	        public void handleEvent(Event evt) {
		    	if(create == true && elem != 6){
		    		Element svgRoot = svgCanvas.getSVGDocument().getRootElement();
		    		doc = svgCanvas.getSVGDocument();
		            if (actionNode == null)return;
		            DOMMouseEvent elEvt = (DOMMouseEvent)evt;
		            if (action == DRAG) {
		                Element ee = (Element)actionNode;           
		                pt = localPt((Element)ee.getParentNode(),
elEvt.getClientX(), elEvt.getClientY());
		                dx = pt.getX() - startPt.getX();
		                dy = pt.getY() - startPt.getY();
		                switch(elem){
			            case 1: 
			            	rect...
			 	        	break;
			            case 2:
			            	circle...
			 	        	break;
			            case 3:
			             	ellipse...
			 	        	break;
			            case 4: 	            	
			            	line...
			            	break;
			            
		                }
		            }
		    	}
	        }
	    }

-- 
View this message in context: http://www.nabble.com/insert-polygon-tf2810417.html#a7842554
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: insert polygon

Posted by Peppe <pe...@gmail.com>.


thomas.deweese wrote:
> 
> Hi Peppe,
> 
>     You need to pass the same object instance to remove as you
> passed to addEventListener you are passing a new instance to
> remove:
> 
> Peppe <pe...@gmail.com> wrote on 12/13/2006 09:10:51 AM:
> 
>>      t.addEventListener("mousedown", new OnDownAction(), false);
> 
> 
>>      t.removeEventListener("mousedown", new OnDownAction(), false);
> 
>    You need to remember the listener you added:
>         EventListner downListener;  // class member...
> 
>         [...]
>         downListener  = new OnDownAction();
>         t.addEventListener("mousedown", downListener, false);
> 
> 
>         [....]
>       t.removeEventListener("mousedown", downListener, false);
>         downListener = null;
> 
> 


ok...you have reason, now it works
-- 
View this message in context: http://www.nabble.com/insert-polygon-tf2810417.html#a7904523
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: insert polygon

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

    You need to pass the same object instance to remove as you
passed to addEventListener you are passing a new instance to
remove:

Peppe <pe...@gmail.com> wrote on 12/13/2006 09:10:51 AM:

>      t.addEventListener("mousedown", new OnDownAction(), false);


>      t.removeEventListener("mousedown", new OnDownAction(), false);

   You need to remember the listener you added:
        EventListner downListener;  // class member...

        [...]
        downListener  = new OnDownAction();
        t.addEventListener("mousedown", downListener, false);


        [....]
      t.removeEventListener("mousedown", downListener, false);
        downListener = null;

Re: insert polygon

Posted by Peppe <pe...@gmail.com>.


thomas.deweese wrote:
> 
> 
>     I suspect you need to call 'EventTarget.removeEventListener'
> 

I delete the code of "onUpAction" and "onMoveAction" in this way the code
become simpler...then, how you say, i remove Event listener but the the
problem is not solved:

private JButton getPolygonButton() {
			if (polygonButton == null) {
				polygonButton = new JButton();
				polygonButton.setText("polygon");
				polygonButton.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {	
						move = false;					
						elem = 6;
						create = true;
		            	                flag = true;
		            	                points = "";
		               
svgCanvas.getUpdateManager().getUpdateRunnableQueue().invokeLater(new
Runnable() {
							public void run() {
									SVGElement svgRoot = svgCanvas.getSVGDocument().getRootElement();
									svgRoot.setAttributeNS(null, "cursor", "crosshair");
									EventTarget t = (EventTarget)svgRoot;
									//t.addEventListener("mousemove", new OnMoveAction(), false);
									t.addEventListener("mousedown", new OnDownAction(), false);
									//t.addEventListener("mouseup", new OnUpAction(), false);
							}
		                });
					}

				});
			}
			return polygonButton;
		}


	    private class OnDownAction implements EventListener {
	        public void handleEvent(Event evt) {
	        	Element svgRoot = svgCanvas.getSVGDocument().getRootElement();
		    	if(move == true){
	            DOMMouseEvent elEvt = (DOMMouseEvent)evt;  
	            actionNode = elEvt.getTarget();
	            action = DRAG;
	            Node n = ((Element)elEvt.getTarget());
	            startPt = localPt((Element)n, elEvt.getClientX(),
elEvt.getClientY());
	        	}else if(create == true){
	        		DOMMouseEvent elEvt = (DOMMouseEvent)evt;  
		            actionNode = elEvt.getTarget();
		            action = DRAG;
		            Node n = ((Element)elEvt.getTarget());
		            startPt = localPt((Element)n, elEvt.getClientX(),
elEvt.getClientY());
	        		switch(elem){
		            case 1: 
		 	        	...
		            case 2:
		                        ...
		            case 3:
		            	      ...
		            case 4: 
		            	     ....
		           case 6:
		        	  if(flag = true){

		            	points = points+startPt.getX()+","+startPt.getY()+" ";		            		
		            	System.out.println(points);

		        		if(elEvt.getDetail() == 2){   
		        			flag = false;
		        			polygon = svgCanvas.getSVGDocument().createElementNS(svgNS,
"polygon");
			            	polygon.setAttributeNS(null, "id", "bgpolygon");
			            		
			            	polygon.setAttributeNS(null, "points", points);
			            	polygon.setAttributeNS(null, "style", "fill:rgb("+
newColor.getRed()+ "," + newColor.getGreen()+","+newColor.getBlue()+");
stroke:none; stroke-width:0");
		            	
			            	svgRoot.appendChild(polygon);
			            	create = false;
			            	actionNode = null;
			            	elem = 0;
			            	points = "";
			            	
							
				    		EventTarget t = (EventTarget)svgRoot;
							//t.removeEventListener("mousemove", new OnMoveAction(), false);
						t.removeEventListener("mousedown", new OnDownAction(), false);
							//t.removeEventListener("mouseup", new OnUpAction(), false);
		        		}
		        		
		        	   } 					
		            	break;
	                }				
	        	}
	        }
	    }

-- 
View this message in context: http://www.nabble.com/insert-polygon-tf2810417.html#a7853464
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: insert polygon

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

Peppe <pe...@gmail.com> wrote on 12/12/2006 04:53:36 PM:

> when i click button "polygon" this activate 3 listener,
> initializing flag = true, create = true and elem = 6. 

   When you say 'activate' I suspect this means that you
add the listeners to the canvas.

> [everything works for first polygon]

>But when i reclick button "polygon" and then when i click 
> on svgCanvas the points is saved 2 times into string "points"...
> for example:
> insert first polygon
>     points = 192,168  340,430 ...
> when i reclick button polygon
>     points = 156,402  156,402  304,170  304,170 ....
> why?

    I think you end up registering a new instance of the
listeners when the user clicks 'polygon'.  You either need
to only register them once (and have a way to disable them
when they shouldn't do anything), or remove them after the 
polygon is created.

> private class OnDownAction implements EventListener {
>            public void handleEvent(Event evt) {
  [...]
> 
>                      points = points+startPt.getX()+","+startPt.getY()+" 
"; 
>                      System.out.println(points);

    Does this get printed twice for each point, like:
        156,402  156,402  304,170
        156,402  156,402  304,170  304,170

    I suspect you need to call 'EventTarget.removeEventListener'