@Cakey + Ihkbob
Thanks for your quick. Yesterday I played around the whole day with your suggestions. Everything works fine.
@ Ihkbob
I even wrote some lines with an animator. To rise my learning curve even a bit more!

→ See below. Thanks for mention it.
Again, thanks you two. It helped me very much.
Jaquelin
In case someone is interested in the animator solution Ihkbob mentioned see this code. Take a look at moveToLeftAccess() and how the other two classes use this method.
My “Main-Class”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent;
import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLJPanel; import javax.swing.JFrame; import javax.swing.JPanel;
import com.sun.opengl.util.FPSAnimator;
@SuppressWarnings("serial") public class TriangleGL extends JFrame {
private static final int JPANEL_WIDTH = 512; private static final int JPANEL_HEIGHT = 512; private TriangleGLListener triangleGLListener; private GLJPanel jCanvas;
private static ValueChanger valueChanger; private FPSAnimator animator; private static int DEFAULT_FPS = 40; private static float moveToLeft = 0; public static final int SET = 1; public static final int GET = 2; public TriangleGL() { super("Frame for my frist triangle"); Container container = this.getContentPane(); container.setLayout(new BorderLayout()); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { animator.stop(); valueChanger.interrupt(); System.exit(0); } }); JPanel renderJPanel = makeRenderPanel(); container.add(renderJPanel, BorderLayout.CENTER); pack(); setVisible(true); animator.start(); } public static void main(String[] args) { valueChanger = new ValueChanger(); valueChanger.start(); new TriangleGL(); }
private JPanel makeRenderPanel() { JPanel renderJPanel = new JPanel(); renderJPanel.setLayout(new BorderLayout()); renderJPanel.setPreferredSize(new Dimension(JPANEL_WIDTH, JPANEL_HEIGHT)); GLCapabilities glCapabilities = new GLCapabilities(); glCapabilities.setAlphaBits(8); jCanvas = new GLJPanel(glCapabilities); triangleGLListener = new TriangleGLListener(this); jCanvas.addGLEventListener(triangleGLListener); animator = new FPSAnimator(jCanvas, DEFAULT_FPS, true); renderJPanel.add(jCanvas, BorderLayout.CENTER); return renderJPanel; } public static synchronized float moveToLeftAccess(int mode, float newValue) { switch(mode) { case (SET): moveToLeft = newValue; break; case (GET): break; } return moveToLeft; } } |
My GLListener implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| import javax.media.opengl.GL; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLEventListener; import javax.media.opengl.glu.GLU;
public class TriangleGLListener implements GLEventListener {
private GLU glu;
public TriangleGLListener(TriangleGL triangle) {
}
public void display(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(TriangleGL.moveToLeftAccess(TriangleGL.GET, 0.0f), 0.0f, -6.0f);
gl.glBegin(GL.GL_TRIANGLES); gl.glVertex3f(0.0f, 1.5f, 0.0f); gl.glVertex3f(-1.0f, 0.0f, 0.0f); gl.glVertex3f(1.0f, 0.0f, 0.0f); gl.glEnd();
gl.glFlush();
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL gl = drawable.getGL();
if (height == 0) height = 1;
gl.glViewport(x, y, width, height);
gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity();
glu.gluPerspective(45.0f, (float) width / (float) height, 0.1f, 100.0f);
gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity();
}
public void init(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
glu = new GLU();
gl.glShadeModel(GL.GL_SMOOTH);
gl.glEnable(GL.GL_DEPTH_TEST);
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
} |
This class simulates my class which actually calculates some stuff on which changeToLeft depends.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| public class ValueChanger extends Thread {
private float moveToLeftInternal; public ValueChanger() { moveToLeftInternal = 0; } public void run() { while(true) { if(isInterrupted()) { break; } changeMoveToLeft(); } } public void changeMoveToLeft() { if(moveToLeftInternal > -2.0f) { moveToLeftInternal = moveToLeftInternal - 0.1f; } else { moveToLeftInternal = 0; } TriangleGL.moveToLeftAccess(TriangleGL.SET, moveToLeftInternal); try { Thread.sleep(500); } catch (InterruptedException ie) { interrupt(); } }
} |