Search code examples
javaopengljogl

How to move 3D Models in Java 3D by Mouse or KeyBoard


i have a project Jug3D in java with jogl, and i want to move it with mouse or keyboard. i tried to search and write the code . how i can add mouseAdapter or keyboardAdapter to move the shape?

My Sourcecode

(need to add JOGL Library):

JOGL Library
JOGL Library

===========================================================

output
output

===========================================================

package Jug3D;

import com.jogamp.newt.Window;
import com.jogamp.newt.event.awt.*;
import com.jogamp.opengl.GL;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLJPanel;
import com.jogamp.opengl.util.gl2.GLUT;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javafx.animation.*;

public class Jug3D extends JPanel implements GLEventListener {

public static void main(String[] args) {
    JFrame window = new JFrame("JUG 3D");
    window.setContentPane(new Jug3D());
    window.pack();
    window.setLocation(50,50);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
}

private GLJPanel display;
private float rotateX, rotateY;   // rotation amounts about axes, controlled by keyboard


public Jug3D() {
    GLCapabilities caps = new GLCapabilities(null);
    display = new GLJPanel(caps);
    display.setPreferredSize( new Dimension(600,600) );  // TODO: set display size here
    display.addGLEventListener(this);
    setLayout(new BorderLayout());
    add(display,BorderLayout.CENTER);
    // TODO:  Other components could be added to the main panel.

    rotateX = 15;  // initialize some variables used in the drawing.
    rotateY = 15;

    // TODO:  Uncomment the next two lines to enable keyboard event handling
    //requestFocusInWindow();
    //addKeyListener(this);

    // TODO:  Uncomment the next one or two lines to enable mouse event handling
    //display.addMouseListener(this);
    //display.addMouseMotionListener(this);


}

// ---------------  Methods of the GLEventListener interface -----------

private GLUT glut = new GLUT();  // for drawing the teapot

/**
 * This method is called when the OpenGL display needs to be redrawn.
 */
public void display(GLAutoDrawable drawable) {  
        // called when the panel needs to be drawn

    GL2 gl = drawable.getGL().getGL2();
    gl.glClearColor(0,0,0,0); // BG Color *
    gl.glClear( GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT );

    gl.glMatrixMode(GL2.GL_PROJECTION);  // same like texture *
    gl.glLoadIdentity();
    gl.glOrtho(-1,1,-1,1,-2,2); // position of jug
    gl.glMatrixMode(GL2.GL_MODELVIEW); // same like texture *

    gl.glLoadIdentity();             // Set up modelview transform. 
    gl.glRotatef(rotateY,0,1,0);
    gl.glRotatef(rotateX,1,0,0);

    // TODO: add drawing code!!  As an example, draw a GLUT teapot
    glut.glutSolidTeapot(0.5); // size of glut



}

/**
 * This is called when the GLJPanel is first created.  It can be used to initialize
 * the OpenGL drawing context.
 */
public void init(GLAutoDrawable drawable) {
        // called when the panel is created
    GL2 gl = drawable.getGL().getGL2();
    gl.glClearColor(0.8F, 0.8F, 0.8F, 1.0F);
    gl.glEnable(GL.GL_DEPTH_TEST);
    gl.glEnable(GL2.GL_LIGHTING);
    gl.glEnable(GL2.GL_LIGHT0);
    gl.glEnable(GL2.GL_COLOR_MATERIAL);

               gl.glEnable(GL2.GL_NORMALIZE);

}


public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
}


public void dispose(GLAutoDrawable drawable) {
}

}

Solution

  • Here is the full source code (using JOGL 2.3)

    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;
    
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    import com.jogamp.opengl.GL;
    import com.jogamp.opengl.GL2;
    import com.jogamp.opengl.GLAutoDrawable;
    import com.jogamp.opengl.GLCapabilities;
    import com.jogamp.opengl.GLEventListener;
    import com.jogamp.opengl.GLProfile;
    import com.jogamp.opengl.awt.GLCanvas;
    import com.jogamp.opengl.util.Animator;
    import com.jogamp.opengl.util.gl2.GLUT;
    
    public class G1 extends JFrame implements GLEventListener, MouseMotionListener {
    
        private static final long serialVersionUID = 7376825297884956163L;
    
        private float rotateX, rotateY;
        private int lastX, lastY;
    
        public G1() {
            super("G1");
            setSize(800,600);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            GLProfile glProfile = GLProfile.getDefault();
            GLCapabilities glCapabilities = new GLCapabilities(glProfile);
            glCapabilities.setDoubleBuffered(true);
            GLCanvas glCanvas = new GLCanvas(glCapabilities);
            glCanvas.addGLEventListener(this);
            glCanvas.addMouseMotionListener(this);
            add(glCanvas);
            addMouseMotionListener(this);
            rotateX = 0f; rotateY = 0f;
            Animator a = new Animator(glCanvas);
            a.start();
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    G1 g1 = new G1();
                    g1.setVisible(true);
                }
            });
        }
    
        @Override
        public void display(GLAutoDrawable drawable) {
            GL2 gl = drawable.getGL().getGL2();
            gl.glClearColor(0,0,0,0);
            gl.glClear( GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT );
            gl.glMatrixMode(GL2.GL_PROJECTION);
            gl.glLoadIdentity();
            gl.glOrtho(-1,1,-1,1,-2,2);
            gl.glMatrixMode(GL2.GL_MODELVIEW);
            gl.glLoadIdentity();
            gl.glRotatef(rotateX,0,1,0);
            gl.glRotatef(rotateY,1,0,0);
            (new GLUT()).glutSolidTeapot(0.5);
        }
    
        @Override
        public void init(GLAutoDrawable drawable) {
            GL2 gl = drawable.getGL().getGL2();
            gl.glClearColor(0.8F, 0.8F, 0.8F, 1.0F);
            gl.glEnable(GL.GL_DEPTH_TEST);
            gl.glEnable(GL2.GL_LIGHTING);
            gl.glEnable(GL2.GL_LIGHT0);
            gl.glEnable(GL2.GL_COLOR_MATERIAL);
            gl.glEnable(GL2.GL_NORMALIZE);
        }
    
        @Override
        public void reshape(
                GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) {
        }
    
        @Override
        public void dispose(GLAutoDrawable arg0) {
        }
    
        @Override
        public void mouseMoved(MouseEvent e) {
            lastX = e.getX();
            lastY = e.getY();
        }
    
        @Override
        public void mouseDragged(MouseEvent e) {
            rotateX += e.getX() - lastX;
            rotateY += e.getY() - lastY;
            lastX = e.getX();
            lastY = e.getY();
        }
    
    }