Login  Register

Re: jogl 2

Posted by sindroide on May 24, 2012; 8:56am
URL: https://forum.jogamp.org/jogl-2-tp4013001p4013143.html

public class GUI extends javax.swing.JFrame implements GLEventListener {
private Animator animator;
private double z=10, rot=0;
private javax.swing.JButton jButton1;
private javax.media.opengl.awt.GLJPanel gLJPanel2;
public GUI() {
jButton1 = new javax.swing.JButton();
jButton1.setText("Rotate");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                for(int i=1; i<361; i++) {
                     rot+=1;
                }
            }
        });
gLJPanel2 = new javax.media.opengl.awt.GLJPanel();
//adding the gLJPanel2 to the frame
javax.swing.GroupLayout gLJPanel2Layout = new javax.swing.GroupLayout(gLJPanel2);
        gLJPanel2.setLayout(gLJPanel2Layout);
        gLJPanel2Layout.setHorizontalGroup(
            gLJPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 281, Short.MAX_VALUE)
        );
        gLJPanel2Layout.setVerticalGroup(
            gLJPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
//adding the button to the frame
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(gLJPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

gLJPanel2.addGLEventListener(this);
animator = new Animator(gLJPanel2);
animator.start();
} //end of constructor

public void init(GLAutoDrawable drawable) {
        // Use debug pipeline
        // drawable.setGL(new DebugGL(drawable.getGL()));

        GL2 gl = drawable.getGL().getGL2();
        System.err.println("INIT GL IS: " + gl.getClass().getName());

        // Enable VSync
        gl.setSwapInterval(0);

        // Setup the drawing area and shading mode
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glShadeModel(GL2.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.
    }

    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
        GL2 gl = drawable.getGL().getGL2();                
        if (height <= 0) { // avoid a divide by zero error!        
            height = 1;
        }
        final float h = (float) width / (float) height;
       
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
   
        GLU glu = new GLU();        
        glu.gluPerspective(45.0f, h, 1.0, 1000.0);        
        glu.gluLookAt(x, y, -10, 0, 0, 0, 0, 1, 0);
        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

    public void display(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();
        // Clear the drawing area
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        // Reset the current matrix to the "identity"
        gl.glLoadIdentity();
        gl.glPushMatrix();
       
        gl.glTranslated(0.0f, 0.0f, z);
        gl.glRotated(rot, 1.0f, 0.0f, 0.0f);
        gl.glPointSize(5);
     
       gl.glBegin(GL.GL_TRIANGLES);
 
        gl.glColor3f(0.0f, 1.0f, 1.0f); gl.glVertex3f(0.0f, 1.0f, 0.0f);
        gl.glColor3f(0.0f, 0.0f, 1.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f);
        gl.glColor3f(0.0f, 0.0f, 0.0f); gl.glVertex3f(1.0f, -1.0f, 1.0f);
        gl.glPopMatrix();
       
        gl.glEnd();
        // Flush all drawing operations to the graphics card
        gl.glFlush();
        // rot+=.2%360; adding this line I have the desited effect but this has to be done when the user clicks the button
    }

    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
    }
   
    @Override
    public void dispose(GLAutoDrawable drawable) {
                // Hardly used.
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new GUI().setVisible(true);
            }
        });
    }
}

I really hope you got the idea... and really apreciate your help Sven