2012-12-04 26 views
1

我有一個自己的vector.class和polygon.class Vector.class如何列出自己的矢量元素?

package org.onvif.ver10.schema; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlType; 

@XmlAccessorType(XmlAccessType.FIELD) 

@XmlType(name = "Vector") 

public class Vector { 

    @XmlAttribute(name = "x") 
    protected Float x; 
    @XmlAttribute(name = "y") 
    protected Float y; 

    /** 
    * Gets the value of the x property. 
    * 
    * @return 
    *  possible object is 
    *  {@link Float } 
    *  
    */ 
    public Float getX() { 
     return x; 
    } 

    /** 
    * Sets the value of the x property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link Float } 
    *  
    */ 
    public void setX(Float value) { 
     this.x = value; 
    } 

    /** 
    * Gets the value of the y property. 
    * 
    * @return 
    *  possible object is 
    *  {@link Float } 
    *  
    */ 
    public Float getY() { 
     return y; 
    } 

    /** 
    * Sets the value of the y property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link Float } 
    *  
    */ 
    public void setY(Float value) { 
     this.y = value; 
    } 
    } 

和多邊形類

public class Polygon { 

@XmlElement(name = "Point", required = true) 
protected List<Vector> point; 

/** 
* Gets the value of the point property. 
* 
* <p> 
* This accessor method returns a reference to the live list, 
* not a snapshot. Therefore any modification you make to the 
* returned list will be present inside the JAXB object. 
* This is why there is not a <CODE>set</CODE> method for the point property. 
* 
* <p> 
* For example, to add a new item, do as follows: 
* <pre> 
* getPoint().add(newItem); 
* </pre> 
* 
* 
* <p> 
* Objects of the following type(s) are allowed in the list 
* {@link Vector } 
* 
* 
*/ 
public List<Vector> getPoint() { 
    if (point == null) { 
     point = new ArrayList<Vector>(); 
    } 
    return this.point; 
} 

} 

所以,我給向量元素:

org.onvif.ver10.schema.Vector MyVector = new Vector(); // létrehozzuk az 
                   // onvif féle 
                   // vector-t 
     org.onvif.ver10.schema.Polygon op = new org.onvif.ver10.schema.Polygon(); 

     for (int i = 1; i <= p.npoints; i++) { 
//   IJ.log("X: "+ i); 
//   MyVector.setX((float) p.xpoints[i]); // hozzáadjuk az elemet 
//   MyVector.setY((float) p.ypoints[i]); 

      MyVector.setX((float)p.xpoints[i-1]); 
      op.getPoint().add(MyVector); 
      IJ.log("Vector X Elements "+i+" :"+ MyVector.getX()); 

     } 
     IJ.log("Op size " + op.getPoint().size()); 

我的問題是我如何得到op(onvif多邊形)元素? 因爲我不管我如何嘗試,我只是得到了最後一個元素10次。

+0

你可以使用'javax.vecmath.Vector2f'類,而不是用2個漂浮...... –

回答

2

你的錯誤是,您要添加的相同Vector實例的多邊形10倍,一遍又一遍地對一個實例設置點。

您的代碼應改爲移動new Vector()循環:

org.onvif.ver10.schema.Polygon op = new org.onvif.ver10.schema.Polygon(); 

    for (int i = 1; i <= p.npoints; i++) { 
     org.onvif.ver10.schema.Vector MyVector = new Vector(); 
     MyVector.setX((float)p.xpoints[i-1]); 
     op.getPoint().add(MyVector); 
     IJ.log("Vector X Elements "+i+" :"+ MyVector.getX()); 

    } 

與您的代碼風格的問題包括,但不限於:

  • 變量名應該開始以小寫字母,所以myVectorMyVector
  • 從不創建與JDK類的代名詞類的名稱(如Vector
+0

THX創建一個Vector類,我會盡力回答。我沒有將它命名爲vector.class,它是一個onvif.xsd,它使這些calsses。 如何打印op元素外觀循環? – david

+0

@ user1620554您可以用'爲(矢量V:op.getPoint()){IJ.log( 「矢量X元素:」 + v.getX());}' –

+0

THX它的工作:) 一個問題:我管理它的第一個循環從1開始,並且p.xpoints以i-1結束......你能告訴我爲什麼嗎? – david