-1

elcipes 告诉我我的 vertBuff.put(vertices); --- 类型 FloatBuffer 中的此方法 .put(float f) 不适用于参数 (Float[])。我该如何解决这个错误?这是具有 vertBuff.put() 的代码

      `public class Object {

private float rgbaVals[] = { 1, 1, 0, .5f, .25f, 0, .85f, 1, 0, 1, 1, 1 };

private FloatBuffer colorBuff;

private FloatBuffer vertBuff;

public short[] pIndex = { 0, 1, 2 };// this is the array for the drawing
                                    // order

private ShortBuffer pBuff;

    public void Line(Float vertex, Short Index){
        Float vertices = vertex;
        Short pIndex = Index;   
    }
public Object(Float[] vertices) {

    ByteBuffer bBuff = ByteBuffer.allocateDirect(Float.SIZE * 4);
    bBuff.order(ByteOrder.nativeOrder());
    vertBuff = bBuff.asFloatBuffer();
    vertBuff.put(vertices);        <--------------this is where the problem is at.        
    vertBuff.position(0);

    ByteBuffer pbBuff = ByteBuffer.allocateDirect(Short.SIZE * 2);
    pbBuff.order(ByteOrder.nativeOrder());
    pBuff = pbBuff.asShortBuffer();
    pBuff.put(pIndex);
    pBuff.position(0);

    ByteBuffer cBuff = ByteBuffer.allocateDirect(rgbaVals.length * 4);
    cBuff.order(ByteOrder.nativeOrder());
    colorBuff = cBuff.asFloatBuffer();
    colorBuff.put(rgbaVals);
    colorBuff.position(0);
}

public void draw(GL10 gl) {
    // TODO Auto-generated method stub
    gl.glFrontFace(GL10.GL_CW);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
    gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertBuff);
    gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuff);
    gl.glDrawElements(GL10.GL_TRIANGLES, pIndex.length,
            GL10.GL_UNSIGNED_SHORT, pBuff);
    gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}

}`

这是 MainActivity 类

public class MainActivity extends Activity implements OnClickListener {
public ArrayList<Short> indexP = new ArrayList<Short>();
public ArrayList<Float> linep = new ArrayList<Float>();
public Float coords = (float) 0;
public short p = 0;
public int l = 0;
GLSurfaceView ourSurface;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cad);
    ourSurface = new GLSurfaceView(this);
    FrameLayout v = (FrameLayout) findViewById(R.id.display);
    v.addView(ourSurface);
    ourSurface.setRenderer(new GLRenderer());

    Button line = (Button) findViewById(R.id.line);
    final Button enter = (Button) findViewById(R.id.enter);
    EditText cl = (EditText) findViewById(R.id.cl);
    final String value = cl.getText().toString();

    try {
        coords = Float.parseFloat(value);
    } catch (NumberFormatException e) {
    }
    ;

    line.setOnClickListener(this);
    enter.setOnClickListener(this);

}

public void onClick(View v) {
    Short Index[];
    Float vertex[];
    TextView info = (TextView) findViewById(R.id.info);
    switch (v.getId()) {
    case R.id.line:
        info.setText("Input X");
    case R.id.enter:
        switch (l) {
        case (0):
            linep.add(coords);
            l++;
            info.setText("Input Y");
            break;
        case (1):
            linep.add(coords);
            indexP.add(p);
            l = 0;
            p++;
            vertex = linep.toArray(new Float[linep.size()]);
            Index = indexP.toArray(new Short[indexP.size()]);
            break;
        }
    }

}

public void Setvertex() {

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
}

}

4

1 回答 1

1

Floatfloat与;不是一回事 前者是一种自动装箱类型——它是一个包装原始类型的 Object,因此它可以作为 Object 传递并用作 Generic 类型。

声明/传入你的数组Float[] vertices意味着你有一个Float对象数组。您不能将它用作float[]原始数组float- 自动装箱不会扩展到数组对象。

FloatBuffer有方法put(float[])。您需要使用float[]而不是Float[]为您的vertices.

于 2013-06-08T20:18:53.490 回答