gpt4 book ai didi

java - 已经为此实例调用了 setRenderer

转载 作者:行者123 更新时间:2023-11-30 02:57:25 24 4
gpt4 key购买 nike

我知道这条错误消息已经有了答案,但这些解决方案都没有解决我的问题。我正在尝试做一个 Sierpinski Triangle 并且我有一个 MySurfaceView 类扩展了 GLSurfaceView 和一个 MyRenderer 类扩展 Activity 并实现 Renderer。以下是类(class):

 import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;

public class MySurfaceView extends GLSurfaceView {

private MyRenderer renderer;
private float prevX;
private float prevY;
private float ROTATION_FACTOR = 1.0f;


public MySurfaceView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

public MySurfaceView(Context context, MyRenderer renderer) {
super(context);
this.renderer = renderer;
this.setRenderer(renderer);
this.setRenderMode(RENDERMODE_WHEN_DIRTY);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_MOVE:
float x = event.getX();
float y = event.getY();
float dx = x-prevX;
float dy = y-prevY;
prevX = x;
prevY = y;
float distance = (float) Math.sqrt((double) (dx*dx+dy*dy));
float angle = distance*ROTATION_FACTOR;
if(dy>0){
renderer.increaseRotationAngle(angle);
}else{
renderer.increaseRotationAngle(-angle);
}
this.requestRender();
}
return true;
}

}


import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.opengl.GLSurfaceView.Renderer;

public class MyRenderer extends Activity implements Renderer {

private Triangle mTriangle;
private float rotationAngle = 0;
private int level;
private int mFill;

public MyRenderer(int level, int mFill)
{
this.level = level;
this.mFill = mFill;
mTriangle = new Triangle(5, "WireFrame");
}

@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glRotatef(rotationAngle, 0, 0, 1);
mTriangle.onDrawFrame(gl);
// TODO Auto-generated method stub
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {

int delta;
if(width<height){
delta=(height-width)/2;
gl.glViewport(0, delta, width, width);
}else{
delta=(width-height)/2;
gl.glViewport(delta, 0, height, height);
}
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, -0.1f, 0.1f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
// TODO Auto-generated method stub
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {

gl.glClearColor(0.8f, 0.8f, 0.8f, 1);
// TODO Auto-generated method stub
}

public void increaseRotationAngle(float delta)
{
rotationAngle+=delta;
System.out.println("Rotating by "+rotationAngle);
}
}


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

private MySurfaceView view;
private MyRenderer renderer;
private String levelCount;
public String skew;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);

renderer = new MyRenderer(3, 1);
view = new MySurfaceView(this, renderer);
view.setRenderer(renderer);
setContentView(view);

}

@Override
public void onPause()
{
view.onPause();
super.onPause();
}

@Override
public void onResume()
{
view.onResume();
super.onResume();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

最佳答案

ActivityonCreate() 的下一行中

view = new MySurfaceView(this, renderer);

您创建了一个 MySurfaceView 类的实例。它的构造函数(MySurfaceView(Context context, MyRenderer renderer))在行中设置了一个renderer

this.setRenderer(renderer);

因此,当您尝试在 ActivityonCreate()

的下一行中正确设置渲染器时
view.setRenderer(renderer);

你得到了错误。

该行是多余的。

关于java - 已经为此实例调用了 setRenderer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23048888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com