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 次。

4

1 回答 1

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());

    }

您的代码的样式问题包括但不限于:

  • 变量名应该以小写字母开头,所以myVector不要MyVector
  • 永远不要创建与 JDK 类同义的类名(如Vector
于 2012-12-04T12:08:45.850 回答