Could someone explain why the code below does not print in console?
canvasName=canvas1 canvasName=canvas2 But is printing: canvasName=canvas2 That´s the code: public class JOGL2Setup_GLCanvas extends GLCanvas implements GLEventListener { private static String TITLE = "JOGL 2.0 Setup (GLCanvas)"; private static final int CANVAS_WIDTH = 640; private static final int CANVAS_HEIGHT = 480; private static final int FPS = 60; private String canvasName; public JOGL2Setup_GLCanvas(String canvasName) { this(); this.canvasName = canvasName; } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { // Create my two OpenGL Canvas GLCanvas upperCanvas = new JOGL2Setup_GLCanvas("upperCanvas"); GLCanvas lowerCanvas = new JOGL2Setup_GLCanvas("lowerCanvas"); upperCanvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT)); lowerCanvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT)); final FPSAnimator upperCanvasAnimator = new FPSAnimator(upperCanvas, FPS, true); final FPSAnimator lowerCanvasAnimator = new FPSAnimator(lowerCanvas, FPS, true); final JFrame frame = new JFrame(); // Adding the canvases. Will be drawn one above the other because I just want to print messages in console. frame.getContentPane().add(upperCanvas); frame.getContentPane().add(lowerCanvas); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { new Thread() { @Override public void run() { // stop the world if (upperCanvasAnimator.isStarted() || lowerCanvasAnimator.isStarted()){ upperCanvasAnimator.stop(); lowerCanvasAnimator.stop(); } System.exit(0); } }.start(); } }); frame.setTitle(TITLE); frame.pack(); frame.setVisible(true); // Should start my two canvas upperCanvasAnimator.start();// Should call init method for upperCanvas, am I right? lowerCanvasAnimator.start();// Should call init method for lowerCanvas, am I right? } }); } private GLU glu; // for the GL Utility public JOGL2Setup_GLCanvas() { this.addGLEventListener(this); } public void init(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); glu = new GLU(); gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glClearDepth(1.0f); gl.glEnable(GL_DEPTH_TEST); gl.glDepthFunc(GL_LEQUAL); gl.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); gl.glShadeModel(GL_SMOOTH); // .... } // Should be called twice, am I right? One for upperCanvas and one for lowerCanvas? But is called once =( public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { GL2 gl = drawable.getGL().getGL2(); if (height == 0) height = 1; float aspect = (float)width / height; gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL_PROJECTION); gl.glLoadIdentity(); glu.gluPerspective(45.0, aspect, 0.1, 100.0); gl.glMatrixMode(GL_MODELVIEW); gl.glLoadIdentity(); System.out.println(drawable); } public void display(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); gl.glTranslatef(0.0f, 0.0f, -6.0f); gl.glBegin(GL_TRIANGLES); gl.glVertex3f(0.0f, 1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 0.0f); gl.glVertex3f(1.0f, -1.0f, 0.0f); gl.glEnd(); } public void dispose(GLAutoDrawable drawable) { } @Override public String toString() { return "canvasName="+this.canvasName; } } |
Administrator
|
Hi
Your code is poorly written, you mix up everything, you create a class that uselessly extends GLCanvas and that creates 2 canvases. System.exit(0) should be necessary only if you use Java Webstart. In my humble opinion, you should put the GLEventListener into a separate class and read some tutorials about Swing as your JFrame uses a BorderLayout by default then you should rather write: frame.getContentPane().add(upperCanvas, BorderLayout.NORTH); frame.getContentPane().add(lowerCanvas, BorderLayout.SOUTH); Replace glu = new GLU(); by glu = GLU.createGLU(gl), otherwise your instance of GLU will support a tiny subset of GLU and throw unsupported operation exceptions whereas it is easy to avoid. I wrote a simple example in which I put everything into the same class but I did it that way to keep it simple, it doesn't mean that it should be done in real programs: https://gist.github.com/gouessej/3420e2b6f632efdddf98 There are some modern examples here: http://jogamp.org/git/?p=jogl-demos.git;a=blob;f=src/demos/es2/RawGL2ES2demo.java;hb=HEAD https://github.com/elect86/jogl-samples/blob/master/jogl-samples/src/helloTriangle/HelloTriangle.java
Julien Gouesse | Personal blog | Website
|
Hi Julien
Thanx for your reply. I had extracted this code from a former tutorial and I tried to put the code the simplest I could to avoid a tiresome reading. I´ll take a look at Swing and Layouts. There´s too many years since I used Swing apps for the last time. I´ll check your example too. Thanx again. |
Set the BorderLayout give me the desired results.
Thanx |
Administrator
|
You're welcome. Don't forget to call GLU.createGLU(gl) instead of new GLU().
Julien Gouesse | Personal blog | Website
|
Free forum by Nabble | Edit this page |