Hello. I am trying to figure out how to move a plane smoothly. I have turned on double buffering for the canvas but it still seems to flicker. Any suggestions?
Here is some code that does what I’m talking about
================= DISPLAY ============
public void display(GLDrawable drawable) {
float clear[] = { 1.0f, 1.0f, 1.0f, 1.0f };
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glPushMatrix();
gl.glTranslatef(x,y,.z);
gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT_AND_DIFFUSE, clear);
gl.glBegin (gl.GL_QUADS);
gl.glVertex3fv(bounds[0]);
gl.glVertex3fv(bounds[1]);
gl.glVertex3fv(bounds[2]);
gl.glVertex3fv(bounds[3]);
gl.glEnd ();
gl.glPopMatrix();
}
======== Updates the rectangle ===============
public void move(float x, float y){
this.x = x;
this.y = y;
========= Bounds for the rectangle ===========
float bounds[][];
bounds = new float[4][3];
bounds[0][0] = 0.0f; bounds[0][1] = 0.0f; bounds[0][2] = 0.01f;
bounds[1][0] = 1.0f; bounds[1][1] = 0.0f; bounds[1][2] = 0.01f;
bounds[2][0] = 1.0f; bounds[2][1] = 1.0f; bounds[2][2] = 0.01f;
bounds[3][0] = 0.0f; bounds[3][1] = 1.0f; bounds[2][2] = 0.01f;
=========== A Thread for handing the movement of the box ==========
while(reading){
if(moving_up){
y += speed;
}
else if(moving_down){
y -= speed;
}
if(moving_right){
x += speed;
}
else if(moving_left){
x -= speed;
}
if(moving_right || moving_left || moving_down || moving_up){
move(x,y);
}
try{
thread.sleep(10); //in mill
}
catch(InterruptedException e){}
}