我对JOGL很新,所以请原谅这个问题的noobness。
我正在尝试编写一段玩具代码,使用GLCanvas输出单个方块,GLCanvas随机放置在画布上。但是,显示功能似乎被调用两次(因此出现两个方块,而不是一个)。这是代码。我哪里出错了?
RandomPoint类:
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
public class RandomPoints extends Frame implements GLEventListener{
/**
*
*/
private static final long serialVersionUID = 1L;
public int HEIGHT;
public int WIDTH;
private GLCanvas canvas;
//private Animator animator;
public RandomPoints(){
HEIGHT = 300;
WIDTH = 300;
GLProfile profile = GLProfile.getDefault();
GLCapabilities cap = new GLCapabilities(profile);
canvas = new GLCanvas(cap);
add(canvas, BorderLayout.CENTER);
canvas.addGLEventListener(this);
System.out.println("Probe");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
//animator.stop();
System.exit(0);
}
});
}
@Override
public void display(GLAutoDrawable arg0) {
//TODO Auto-generated method stub
GL2 gl = arg0.getGL().getGL2();
float red = (float) Math.random();
float blue = (float) Math.random();
float green = (float) Math.random();
int x = (int)(Math.random()*WIDTH);
int y = (int)(Math.random()*HEIGHT);
gl.glColor3f(red, green, blue);
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex2i(x, y);
gl.glVertex2i(x+10, y);
gl.glVertex2i(x+10, y+10);
gl.glVertex2i(x, y+10);
gl.glEnd();
System.out.println(x+" "+y+" "+red+" "+green+" "+blue);
}
@Override
public void dispose(GLAutoDrawable arg0) {
// TODO Auto-generated method stub
}
@Override
public void init(GLAutoDrawable arg0) {
// TODO Auto-generated method stub
GL2 gl = arg0.getGL().getGL2();
gl.glColor3f(0.0f, 0.0f, 0.0f);
gl.glClearColor(1, 1, 1, 0);
gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
//animator = new Animator(canvas);
//animator.start();
}
@Override
public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) {
// TODO Auto-generated method stub
WIDTH = arg3; HEIGHT = arg4;
GL2 gl = arg0.getGL().getGL2();
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(0, WIDTH, 0, HEIGHT, -1, 1);
}
}
GLTest类(驱动程序)
public class GLTest {
public static void main(String args[]){
RandomPoints point = new RandomPoints();
point.setSize(300,300);
point.setVisible(true);
}
}
提前致谢!
答案 0 :(得分:0)
我刚刚测试了你的代码,我只看到一个正方形。但是,最好还清除display()方法中的颜色缓冲区。你应该看一下我的例子here。如果你真的想要为你的绘画设置动画,你应该使用动画师。如果你想获得更准确和更快的回复,请在official forum上发布关于JOGL的问题,因为只有极少数的维护者来到这里。