Posted by
cznlzq on
Mar 23, 2012; 8:23pm
URL: https://forum.jogamp.org/question-about-the-GLCanvas-and-GLJPanel-tp3844025p3852684.html
Thank you for the suggestion.
It is very interesting. Indeed we have to call the repaint in the display for the GLCanvas.
However, I found that for the GLJPanel we don't have to call any function.
Just override the paintComponent in GLJPanel then it will take care of everything.
And I recode the test codes to test these two
=========================================================
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.awt.GLJPanel;
import com.jogamp.opengl.util.FPSAnimator;
public class SimpleScene implements GLEventListener {
private double theta = 0;
private double s = 0;
private double c = 0;
private static GLCanvas canvas = null;
private static GLJPanel panel = null;
private static int step = 1;
public static void main(String[] args) {
GLProfile glp = GLProfile.getDefault();
GLCapabilities caps = new GLCapabilities(glp);
caps.setDoubleBuffered(true);
canvas = new GLCanvas(caps){
@Override
public void paint(Graphics arg0) {
//System.out.println("paint");
// TODO Auto-generated method stub
super.paint(arg0);
Graphics2D g2 = (Graphics2D)arg0;
Font font = new Font("Times", Font.BOLD, 32);
g2.setColor(Color.BLUE);
g2.setFont(font);
g2.drawString("2DRender", 250, 50);
}
};
panel = new GLJPanel(){
@Override
protected void paintComponent(Graphics arg0) {
//System.out.println("paintComponent");
// TODO Auto-generated method stub
super.paintComponent(arg0);
Graphics2D g2 = (Graphics2D)arg0;
Font font = new Font("Times", Font.BOLD, 32);
g2.setColor(Color.GREEN);
g2.setFont(font);
g2.drawString("2DRender", 250, 50);
}
};
Frame frame = new Frame("AWT Window Test");
frame.setSize(600, 600);
frame.add(canvas);
//frame.add(panel);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
canvas.setAutoSwapBufferMode(true);
canvas.addGLEventListener(new SimpleScene());
FPSAnimator animator = new FPSAnimator(canvas, 60);
animator.add(canvas);
// panel.setAutoSwapBufferMode(true);
// panel.addGLEventListener(new SimpleScene());
// FPSAnimator animator = new FPSAnimator(panel, 60);
// animator.add(panel);
animator.start();
}
@Override
public void display(GLAutoDrawable drawable) {
update();
render(drawable);
canvas.repaint();
}
@Override
public void dispose(GLAutoDrawable drawable) {
}
@Override
public void init(GLAutoDrawable drawable) {
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
}
private void update() {
theta += 0.01;
s = Math.sin(theta);
c = Math.cos(theta);
}
private void render(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
// draw a triangle filling the window
gl.glBegin(GL.GL_TRIANGLES);
gl.glColor3f(1, 0, 0);
gl.glVertex2d(-c, -c);
gl.glColor3f(0, 1, 0);
gl.glVertex2d(0, c);
gl.glColor3f(0, 0, 1);
gl.glVertex2d(s, -s);
gl.glEnd();
}
}

I can see the drawString things, however, its background doesn't look good and it's flashing.
Did I make something wrong?