Programme java opengl simple de manipulation des objets 3d

Soyez le premier à donner votre avis sur cette source.

Vue 6 850 fois - Téléchargée 1 461 fois

Description

J'ai codé ce petit programme dans le cadre d'un mini-projet au cour de mes études à l'institut supérieur d'informatique et de multimédia de Sfax (Tunisie)
J'ai utilisé un seul fichier "Cube.java" pour les deux classes Cube et SwingWindow.

Profitez en!
:)

Source / Exemple :

//Développé par Mohamed Ali Ben Fredj         benfredjmohamedali@gmail.com 
//LinkedIn: http://www.linkedin.com/pub/mohamed-ali-ben-fredj/27/29a/a31
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.*;
import java.awt.event.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.sun.opengl.util.*;
import java.nio.*;

public class Cube implements GLEventListener {

	float light1_position[] = { -1.0f, 1.0f, 1.0f, 1.0f };// posiotion lumiere
	private double anglex;
	private double angley;
	private double anglez;
	
	private double transx;
	private double transy;
	private double transz;
	private GLU cam;
	private GLUT glut;
	public static String obj = "";

	public void setobj(String objt) {
		this.obj = objt;
	}

	/*
	 * Set up the background color, and clear the modelview matrix.
	 */
	public void init(GLAutoDrawable d) {
		GL gl = d.getGL();
	    glut=new GLUT();
		gl.glClear(GL.GL_COLOR_BUFFER_BIT);
		gl.glClearColor(0, 0, 0, 1);
		gl.glMatrixMode(GL.GL_MODELVIEW);
		gl.glLoadIdentity();
		anglex = 0.0;
		angley= 0.0;
		anglez= 0.0;
		transx = 0.0;
		transy= 0.0;
		transz= 0.0;
		cam = new GLU();
		float light1_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
		float light1_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
		float light1_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
		float spot_direction[] = { 1.0f, -1.0f, 0.0f };
		gl.glLightfv(GL.GL_LIGHT1, GL.GL_AMBIENT, light1_ambient, 0);
		gl.glLightfv(GL.GL_LIGHT1, GL.GL_DIFFUSE, light1_diffuse, 0);
		gl.glLightfv(GL.GL_LIGHT1, GL.GL_SPECULAR, light1_specular, 0);
		gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, light1_position, 0);
		gl.glEnable(GL.GL_LIGHT1);
		// gl.glEnable(GL.GL_LIGHT2);
		// gl.glEnable(GL.GL_LIGHT3);
		gl.glEnable(GL.GL_LIGHTING);

		gl.glEnable(GL.GL_NORMALIZE);// normal % chaque face de l objet %lumiere
		

	}

	/*
	 * Set up a simple orthographic projection, with a square window.
	 */
	public void reshape(GLAutoDrawable d, int x, int y, int width, int height) {
		GL gl = d.getGL();
		gl.glMatrixMode(GL.GL_PROJECTION);
		cam.gluPerspective(60.0, (double) width / height, -5.0, 100.0);

		gl.glLoadIdentity();
		// gl.glOrtho( -1.0, 1.0, -1.0, 1.0, -0.1, 0.1 ); elimination si 3d
		gl.glMatrixMode(GL.GL_MODELVIEW);

	}

	/*
	 * Draw a circle and a triangle rotated by angle.
	 */
	public void display( GLAutoDrawable d ) {
	// glut=new GLUT();
		GL gl = d.getGL();
		envInstall(gl);

		if(this.obj=="cube")
		{
			drawCube(gl);
			}
	//	if(this.obj=="")
		//	{}
		if(this.obj=="cylindre")
			{
				
 			 drawcylindre(gl);
				}
				if(this.obj =="cone")
				{
				drawcone(gl);
		
		}
					
					if(this.obj=="sphere")
					{
				drawsphere(gl);
						}
					if(this.obj=="teapot")
					{
				drawSolidTeapot(gl);
						}
			
			
			
			
	}
	
	public void envInstall(GL gl)
	{
		System.out.println("display thread: " + Thread.currentThread().toString());
		gl.glMatrixMode( GL.GL_PROJECTION );
		gl.glLoadIdentity();
		cam.gluLookAt(0.0,0.5,1.0,0.0,0.0,0.0,0.0,1.0,0.0);//positoin caméra+cooredoné cible+axe de rotaion
	//	cam.gluLookAt(10.0,15.0,10.0,3.0,5.0,-2.0,0.0,1.0,0.0);
		gl.glMatrixMode( GL.GL_MODELVIEW );
		gl.glClear(GL.GL_COLOR_BUFFER_BIT);
		gl.glLoadIdentity();
		gl.glEnable(GL.GL_LIGHT1);
		gl.glEnable(GL.GL_LIGHTING);		
		gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, light1_position,0);
		}
		
	
	public void drawCube(GL gl)
	{
			gl.glRotated(anglex, 1, 0, 0);
			gl.glRotated(angley, 0, 1, 0);
			gl.glRotated(anglez, 0, 0, 1);
		
			gl.glTranslated(transx,0,0);
			gl.glTranslated(0,transy,0);
			gl.glTranslated(0,0,transz);
			

	
        	gl.glPushMatrix();
	        gl.glTranslated(0,0,0.4);
			gl.glRotated(45,0,1,0); // rotate by 45 degrees about y (up) axis
		    glut.glutSolidCube(0.4f);
	        gl.glPopMatrix();
        
		
		}
		
		public void drawSolidTeapot(GL gl)
	{
			gl.glRotated(anglex, 1, 0, 0);
			gl.glRotated(angley, 0, 1, 0);
			gl.glRotated(anglez, 0, 0, 1);
		
			gl.glTranslated(transx,0,0);
			gl.glTranslated(0,transy,0);
			gl.glTranslated(0,0,transz);
			
			gl.glPushMatrix();
			gl.glTranslated(0,0,0.4);
			gl.glRotated(45,0,1,0); // rotate by 45 degrees about y (up) axis
			glut.glutSolidTeapot(0.3);
	  		gl.glPopMatrix();
        
		
		}
		
		public void drawcylindre(GL gl)
		{
				gl.glRotated(anglex, 1, 0, 0);
				gl.glRotated(angley, 0, 1, 0);
				gl.glRotated(anglez, 0, 0, 1);
				gl.glTranslated(transx,0,0);
				gl.glTranslated(0,transy,0);
				gl.glTranslated(0,0,transz);
			
			gl.glTranslated(0,0.0,0.4);
			 double angcyl;
   int h=50;
   float r1=0.1f;
   for(int i=0;i<h;i++)
   	
   {  
   	
   		gl.glTranslated(0,0.01,0);
   	  	gl.glPushMatrix(); 
   	  	gl.glBegin(GL.GL_LINE_LOOP);
   	  for(int j=0;j<100; j++)
   	  {
   	  
   	  	angcyl=j*(2*Math.PI)/100;
  
   	  		  	gl.glVertex3f(((float)Math.cos(angcyl))*r1,0,((float)Math.sin(angcyl))*r1);
   	  }
   	  
		gl.glEnd();
 		 gl.glPopMatrix();
 		}
		}
		
		public void drawcone(GL gl)
		{
				gl.glRotated(anglex, 1, 0, 0);
				gl.glRotated(angley, 0, 1, 0);
				gl.glRotated(anglez, 0, 0, 1);
				gl.glTranslated(transx,0,0);
				gl.glTranslated(0,transy,0);
				gl.glTranslated(0,0,transz);
			
			
		 double ang,ang1,a,b ;
		 int h=1;
		 float r;
	  	float rmax=0.2f;
	   float x=0;
	   
		   gl.glTranslated(0.0,0.0,1.0);
	   gl.glBegin(GL.GL_TRIANGLES);
	   for(float i=1;i<=h;i++)
	   		   {
		   x=x+0.01f;
		   gl.glPushMatrix();
		   gl.glTranslated(0,0,i*0.01);
	   	  r=i*(rmax/h);
	   	 
	   	  for(int j=0;j<=100; j++)
	   	  {
	   	  	ang=j*(2*Math.PI)/100;
	   	    ang1=(j+1)*(2*Math.PI)/100;
	   	    gl.glNormal3f(((float)Math.cos(ang))*r,((float)Math.sin(ang))*r,0);
	   	  	gl.glVertex3f(((float)Math.cos(ang))*r,((float)Math.sin(ang))*r,0);
	   	  	gl.glNormal3f(((float)Math.cos(ang1))*r,((float)Math.sin(ang1))*r,0);
	   	  	gl.glVertex3f(((float)Math.cos(ang1))*r,((float)Math.sin(ang1))*r,0);
             a=((Math.cos(ang1)*r)-(Math.cos(ang)*r))/2;
             b=((Math.sin(ang1)*r)-(Math.sin(ang)*r))/2;
             gl.glNormal3f(0, (float) h,0);
	  		  	gl.glVertex3f(0, (float) h,0);
	   	  }
	  	gl.glPopMatrix();
	   		   }
	  	gl.glEnd();
		
			
		}
		
		public void drawsphere(GL gl)
		{
				gl.glRotated(anglex, 1, 0, 0);
				gl.glRotated(angley, 0, 1, 0);
				gl.glRotated(anglez, 0, 0, 1);
				
				gl.glTranslated(transx,0,0);
				gl.glTranslated(0,transy,0);
				gl.glTranslated(0,0,transz);
			
			
			
		float tStep = (float)(Math.PI) / 30.00f;
				float sStep = (float)(Math.PI) / 30.00f;
				double fx=0.3;
				double fy=0.3;
				double fz=0.3;

				gl.glBegin( GL.GL_TRIANGLES );
			for(float t = -(float)(Math.PI/2); t <= (float)(Math.PI/2)+.0001; t += tStep)
	{
	//	gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE);
		gl.glBegin(GL.GL_TRIANGLE_STRIP);
		gl.glPushMatrix();
		for(float s = -(float)(Math.PI); s <= (float)(Math.PI)+.0001; s += sStep)
		{
		gl.glNormal3d(fx * Math.cos(t) * Math.cos(s), fy * Math.cos(t) * Math.sin(s), fz * Math.sin(t));
        gl.glVertex3d(fx * Math.cos(t) * Math.cos(s), fy * Math.cos(t) * Math.sin(s), fz * Math.sin(t));
        gl.glColor3f(0.6f,0.6f,0.6f);
		gl.glNormal3d((fx * Math.cos(t+tStep) * Math.cos(s)),fy * Math.cos(t+tStep) * Math.sin(s), fz * Math.sin(t+tStep));
        gl.glVertex3d((fx * Math.cos(t+tStep) * Math.cos(s)),fy * Math.cos(t+tStep) * Math.sin(s), fz * Math.sin(t+tStep));}
		gl.glPopMatrix();
		gl.glEnd();
	}
		}

	/*
	 * Rotate the triangle by changing the rotation angle, the triangle will
	 * visually change on the next call to display.
	 */
	public void rotatex(double ang) {
		anglex += ang;
		if (anglex > 180)
			anglex -= 360;
		if (anglex < -180)
			anglex += 360;
	}
	public void rotatey(double ang) {
		angley += ang;
		if (angley > 180)
			angley -= 360;
		if (angley < -180)
			angley += 360;
	}
	public void rotatez(double ang) {
		anglez += ang;
		if (anglez > 180)
			anglez -= 360;
		if (anglez < -180)
			anglez += 360;
	}

	public void translatex(double pas) {
		transx += pas;
	}
	public void translatey(double pas) {
		transy += pas;
	}

	public void translatez(double pas) {
		transz += pas;
	}

	public void moveLight1_xplus(float val, int x, int y, int z) {
		if (x == 1)
			light1_position[0] = light1_position[0] + val;
		if (y == 1)
			light1_position[1] = light1_position[1] + val;
		if (z == 1)
			light1_position[2] = light1_position[2] + val;
	}


	/*
	 * Create an instance of SwingWindow.
	 */

	/*
	 * Unused method
	 */
	public void displayChanged(GLAutoDrawable d, boolean modeChanged,
			boolean deviceChanged) {}

	public static void main(String[] args) {
		new SwingWindow();
	}
	}

class SwingWindow extends JFrame implements ActionListener {
	public Cube formInst;
	// draw buttons
	private JButton cubeButton, coneButton, sphereButton, cylindreButton,teapotButton;
	
	// Rotation buttons
	public JButton 		rotation_x= new JButton(),
				   		rotation_y= new JButton(),
			 	   		rotation_z= new JButton(),
				   		rotationy= new JButton(),
				   		rotationz= new JButton(),
				   		rotationx= new JButton();
	// Déplacement buttons
	public JButton 		translationx= new JButton(), 
					   	translationy= new JButton(),
						translationz= new JButton(),
						translation_x= new JButton(),
			    		translation_y= new JButton(),
			 			translation_z= new JButton();
			 			
	// Déplacement lumière buttons
	public JButton 		translationx_lum= new JButton(), 
					   	translationy_lum= new JButton(),
						translationz_lum= new JButton(),
						translation_x_lum= new JButton(),
			    		translation_y_lum= new JButton(),
			 			translation_z_lum= new JButton();

	// Rotation menu items
	private JMenuItem rotationPlus, rotationMoins, transplusxButton,
			transmoinsxButton;



	//  GLCanvas pannel

	private JPanel canvpanel = new JPanel();

	// The GLCanvas
	GLJPanel canvasCube;

	/*
	 * Sets up the JFrame.
	 */
	public SwingWindow() {

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			//JFrame
	setTitle("ProjetCube Mohamed Ali Ben Fredj");

		Container c = getContentPane();
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(1,2));
		buttonPanel.setSize(new Dimension(800,250));
		buttonPanel.setBackground(Color.WHITE);
		//buttonPanel.setBorder(BorderFactory.createEtchedBorder());

		JPanel formes = new JPanel();
		formes.setLayout(new GridLayout(1,12));
		formes.setBackground(Color.WHITE);
		formes.setBorder(BorderFactory.createTitledBorder("Objets"));

		canvpanel.setPreferredSize(new Dimension(800, 600));
		canvpanel.setBackground(Color.DARK_GRAY);
		canvpanel.setBorder(BorderFactory.createTitledBorder("Espace de visualisation"));
		

		JPanel espaceModif = new JPanel();
		espaceModif.setPreferredSize(new Dimension(200, 100));
		espaceModif.setBackground(Color.white);
		espaceModif.setBorder(BorderFactory.createEtchedBorder());
		espaceModif.setLayout(new GridLayout(4,1));

	//	JPanel cubePanel = new JPanel();
	//	cubePanel.setPreferredSize(new Dimension(100, 400));
	//	cubePanel.setBackground(Color.WHITE);
		
			//rotation_x.setText("-x");
			rotation_x.setBackground(Color.white);
	    	rotation_x.setBorderPainted(true);
	    	ImageIcon icon_rotation_x = new ImageIcon("./images/haut.gif");
	    	rotation_x.setIcon(icon_rotation_x);
	    
	    	//rotation_y.setText("-y");
			rotation_y.setBackground(Color.white);
	    	rotation_y.setBorderPainted(true);
	    	ImageIcon icon_rotation_y = new ImageIcon("./images/gauche.gif");
	    	rotation_y.setIcon(icon_rotation_y);
	    		
		//	rotation_z.setText("-z");
			rotation_z.setBackground(Color.white);
	    	rotation_z.setBorderPainted(true);
	    	ImageIcon icon_rotation_z = new ImageIcon("./images/z_moins.gif");
	    	rotation_z.setIcon(icon_rotation_z);
	    	
	    	
		//	rotationx.setText("+x");
			rotationx.setBackground(Color.white);
	    	rotationx.setBorderPainted(true);
	    	ImageIcon icon_rotationx = new ImageIcon("./images/bas.gif");
	    	rotationx.setIcon(icon_rotationx);
	    	
		//	rotationy.setText("+y");
			rotationy.setBackground(Color.white);
	    	rotationy.setBorderPainted(true);
	    	ImageIcon icon_rotationy = new ImageIcon("./images/droite.gif");
	    	rotationy.setIcon(icon_rotationy);
			
		//	rotationz.setText("+z");
			rotationz.setBackground(Color.white);
	    	rotationz.setBorderPainted(true);
	    	ImageIcon icon_rotationz = new ImageIcon("./images/z_plus.gif");
	    	rotationz.setIcon(icon_rotationz);
	    	
		//////////////////boutons déplacement///////////////////////////
		  //	translation_x.setText("-x");	
			translation_x.setBackground(Color.white);
	    	translation_x.setBorderPainted(true);
	    	ImageIcon icon_translation_x = new ImageIcon("./images/gauche.gif");
	    	translation_x.setIcon(icon_translation_x);
		 
		//	translation_y.setText("-y");	
			translation_y.setBackground(Color.white);
	    	translation_y.setBorderPainted(true);
	    	ImageIcon icon_translation_y = new ImageIcon("./images/bas.gif");
	    	translation_y.setIcon(icon_translation_y);
		
		//	translation_z.setText("-z");	
			translation_z.setBackground(Color.white);
	    	translation_z.setBorderPainted(true);
	    	ImageIcon icon_translation_z = new ImageIcon("./images/z_plus.gif");
	    	translation_z.setIcon(icon_translation_z);
		 
		 //	translationx.setText("x");
		 	translationx.setBackground(Color.white);
	    	translationx.setBorderPainted(true);
	    	ImageIcon icon_translationx = new ImageIcon("./images/droite.gif");
	    	translationx.setIcon(icon_translationx);
		 
		 //	translationy.setText("y");
		 	translationy.setBackground(Color.white);
	    	translationy.setBorderPainted(true);
	    	ImageIcon icon_translationy = new ImageIcon("./images/haut.gif");
	    	translationy.setIcon(icon_translationy);
		 
		 //	translationz.setText("z");
		 	translationz.setBackground(Color.white);
	    	translationz.setBorderPainted(true);
	    	ImageIcon icon_translationz = new ImageIcon("./images/z_moins.gif");
	    	translationz.setIcon(icon_translationz);
		
		//////////////////boutons lumière///////////////////////////
		
		  //	translation_x.setText("-x");	
			translation_x_lum.setBackground(Color.white);
	    	translation_x_lum.setBorderPainted(true);
//	    	ImageIcon icon_translation_x = new ImageIcon("./images/gauche.gif");
	    	translation_x_lum.setIcon(icon_translation_x);
		 
		//	translation_y.setText("-y");	
			translation_y_lum.setBackground(Color.white);
	    	translation_y_lum.setBorderPainted(true);
//	    	ImageIcon icon_translation_y = new ImageIcon("./images/bas.gif");
	    	translation_y_lum.setIcon(icon_translation_y);
		
		//	translation_z.setText("-z");	
			translation_z_lum.setBackground(Color.white);
	    	translation_z_lum.setBorderPainted(true);
//	    	ImageIcon icon_translation_z = new ImageIcon("./images/z_plus.gif");
	    	translation_z_lum.setIcon(icon_translation_z);
		 
		 //	translationx.setText("x");
		 	translationx_lum.setBackground(Color.white);
	    	translationx_lum.setBorderPainted(true);
//	    	ImageIcon icon_translationx = new ImageIcon("./images/droite.gif");
	    	translationx_lum.setIcon(icon_translationx);
		 
		 //	translationy.setText("y");
		 	translationy_lum.setBackground(Color.white);
	    	translationy_lum.setBorderPainted(true);
//	    	ImageIcon icon_translationy = new ImageIcon("./images/haut.gif");
	    	translationy_lum.setIcon(icon_translationy);
		 
		 //	translationz.setText("z");
		 	translationz_lum.setBackground(Color.white);
	    	translationz_lum.setBorderPainted(true);
//	    	ImageIcon icon_translationz = new ImageIcon("./images/z_moins.gif");
	    	translationz_lum.setIcon(icon_translationz);
		//////////////////////JPanel de panneau de rotation des objets////////////////////
				JPanel rotationControl = new JPanel();
				rotationControl.setLayout(new GridLayout(3,3));
				rotationControl.setSize(new Dimension(200,250));
				rotationControl.setBackground(Color.WHITE);
				rotationControl.setBorder(BorderFactory.createTitledBorder("Rotation de l'objet"));
				
				rotationControl.add(new JLabel("") );
				rotationControl.add(rotation_x);
				rotationControl.add(rotation_z);
				rotationControl.add(rotation_y);
				rotationControl.add(new JLabel("") );
				rotationControl.add(rotationy);
				rotationControl.add(rotationz);
				rotationControl.add(rotationx);
				rotationControl.add(new JLabel("") );

	////////////////////////JPanel de panneau de déplacement de lumière////////////////////
					JPanel depControl_lum = new JPanel();
				depControl_lum.setLayout(new GridLayout(3,3));
				depControl_lum.setSize(new Dimension(200,200));
				depControl_lum.setBackground(Color.WHITE);
				depControl_lum.setBorder(BorderFactory.createTitledBorder("Déplacement de la lumière"));
				
				depControl_lum.add(new JLabel("") );
				depControl_lum.add(translationy_lum);
				depControl_lum.add(translationz_lum);
				depControl_lum.add(translation_x_lum);
				depControl_lum.add(new JLabel("") );
				depControl_lum.add(translationx_lum);
				depControl_lum.add(translation_z_lum);
				depControl_lum.add(translation_y_lum);
				depControl_lum.add(new JLabel("") );
				///////////////////////////JPanel de panneau de déplacement des objets////////////////////
				
				JPanel depControl = new JPanel();
				depControl.setLayout(new GridLayout(3,3));
				depControl.setSize(new Dimension(200,200));
				depControl.setBackground(Color.WHITE);
				depControl.setBorder(BorderFactory.createTitledBorder("Déplacement de l'objet"));
				
				depControl.add(new JLabel("") );
				depControl.add(translationy);
				depControl.add(translationz);
				depControl.add(translation_x);
				depControl.add(new JLabel("") );
				depControl.add(translationx);
				depControl.add(translation_z);
				depControl.add(translation_y);
				depControl.add(new JLabel("") );
				


		
		ImageIcon icon_cube = new ImageIcon("./images/cube.gif");
		cubeButton = new JButton(icon_cube);
		cubeButton.setBackground(Color.white);
		cubeButton.setBorderPainted(true);

		ImageIcon icon_cone = new ImageIcon("./images/cone.gif");
		coneButton = new JButton(icon_cone);
		coneButton.setBackground(Color.white);
		coneButton.setBorderPainted(true);

		ImageIcon icon_sphere = new ImageIcon("./images/sphere.gif");
		sphereButton = new JButton(icon_sphere);
		sphereButton.setBackground(Color.white);
		sphereButton.setBorderPainted(true);

		ImageIcon icon_cylindre = new ImageIcon("./images/cylindre.gif");
		cylindreButton = new JButton(icon_cylindre);
		cylindreButton.setBackground(Color.white);
		cylindreButton.setBorderPainted(true);
		
		
		ImageIcon icon_teapot = new ImageIcon("./images/teapot.jpg");
		teapotButton = new JButton(icon_teapot);
		teapotButton.setBackground(Color.white);
		teapotButton.setBorderPainted(true);

 		JLabel aboutProject= new JLabel("benfredjmohamedali@gmail.com");

		formes.add(cubeButton);
		formes.add(coneButton);
		formes.add(sphereButton);
		formes.add(cylindreButton);
		formes.add(teapotButton);
		formes.add(new JLabel(""));
		formes.add(new JLabel(""));
		formes.add(new JLabel(""));
		formes.add(new JLabel(""));
		formes.add(new JLabel(""));
		formes.add(new JLabel(""));
		formes.add(new JLabel(""));

		cubeButton.addActionListener(this);
		coneButton.addActionListener(this);
		sphereButton.addActionListener(this);
		cylindreButton.addActionListener(this);
		teapotButton.addActionListener(this);
		rotationx.addActionListener(this);
		rotationy.addActionListener(this);
		rotationz.addActionListener(this);
		rotation_x.addActionListener(this);
		rotation_y.addActionListener(this);
		rotation_z.addActionListener(this);
		translationx.addActionListener(this);
		translationy.addActionListener(this);
		translationz.addActionListener(this);
		translation_x.addActionListener(this);
		translation_y.addActionListener(this);
		translation_z.addActionListener(this);
		translationx_lum.addActionListener(this);
		translationy_lum.addActionListener(this);
		translationz_lum.addActionListener(this);
		translation_x_lum.addActionListener(this);
		translation_y_lum.addActionListener(this);
		translation_z_lum.addActionListener(this);
		
		


	
		
		espaceModif.add(rotationControl);
		espaceModif.add(depControl);
		espaceModif.add(depControl_lum);
		espaceModif.add(aboutProject);
		
		
		buttonPanel.add(formes, BorderLayout.WEST);
		c.add(buttonPanel, BorderLayout.NORTH);
		c.add(canvpanel, BorderLayout.CENTER);
		c.add(espaceModif, BorderLayout.WEST);

		canvasCube = new GLJPanel();
		canvasCube.setPreferredSize(new Dimension(800, 600));
		canvasCube.setBounds(0,0,800,800);
		canvasCube.setBorder(BorderFactory.createLineBorder(Color.RED,1));
		
		
		formInst = new Cube();
		canvasCube.addGLEventListener(formInst);
		canvasCube.setVisible(false);

		pack();
		setVisible(true);

	}



	/*
	 * Handles events for the JButtons and JMenuItems. A rotation is caused by
	 * updating the angle with a call to rotate in the SwingAndJOGL object. The
	 * screen is then updated by calling display from the GLCanvas.
	 * 
	 * OpenGL methods cannot be called here (or in methods called from here)
	 * because the OpenGL context is not current. Hence, we update the angle and
	 * then request a re-display of the GLCanvas.
	 */

	public void actionPerformed(ActionEvent e) {

		Object source = e.getSource();
		System.out.println("bouton clique: "
				+ Thread.currentThread().toString());

		// cubelistener.rotate(-6.0);
		// cubelistener.moveLight1_xplus(0.1f,1,1,1);

		if (source == cubeButton) {
			canvasCube.setVisible(false);
			formInst.setobj("cube");

			canvasCube.display();
			canvasCube.setVisible(true);
			canvpanel.add(canvasCube, BorderLayout.CENTER);
			canvpanel.setBackground(Color.BLACK);

			setVisible(true);
			canvasCube.repaint();

			System.out.println("After display()");

		} else if (source == sphereButton) {
			canvasCube.setVisible(false);
			formInst.setobj("sphere");

			canvasCube.display();
			canvasCube.setVisible(true);
			canvpanel.add(canvasCube, BorderLayout.CENTER);
			canvpanel.setBackground(Color.BLACK);

			setVisible(true);
			canvasCube.repaint();

			System.out.println("After display()");
		} else if (source == cylindreButton) {
			canvasCube.setVisible(false);
			formInst.setobj("cylindre");

			canvasCube.display();
			canvasCube.setVisible(true);
			canvpanel.add(canvasCube, BorderLayout.CENTER);
			canvpanel.setBackground(Color.BLACK);

			setVisible(true);
			canvasCube.repaint();

			System.out.println("After display()");
		}

		else if (source == coneButton) {
			canvasCube.setVisible(false);
			formInst.setobj("cone");

			canvasCube.display();
			canvasCube.setVisible(true);
			canvpanel.add(canvasCube, BorderLayout.CENTER);
			canvpanel.setBackground(Color.BLACK);

			setVisible(true);
			canvasCube.repaint();

			System.out.println("After display()");
		}	else if (source == teapotButton) {
			canvasCube.setVisible(false);
			formInst.setobj("teapot");

			canvasCube.display();
			canvasCube.setVisible(true);
			canvpanel.add(canvasCube, BorderLayout.CENTER);
			canvpanel.setBackground(Color.BLACK);

			setVisible(true);
			canvasCube.repaint();

			System.out.println("After display()");
		} else if (source == translation_x ) {
			formInst.translatex(-0.1);

		} else if (source == translationx) {
			formInst.translatex(0.1);

		}else if (source == translationy) {
			formInst.translatey(0.1);
		}else if (source == translation_y) {
			formInst.translatey(-0.1);
		}else if (source == translationz) {
			formInst.translatez(0.1);
		}else if (source == translation_z) {
			formInst.translatez(-0.1);
		}else

		if (source == translation_x_lum ) {
			formInst.moveLight1_xplus(-5.0f,1,0,0);

		} else if (source == translationx_lum) {
			formInst.moveLight1_xplus(5.0f,1,0,0);

		}else if (source == translationy_lum) {
				formInst.moveLight1_xplus(5.0f,0,1,0);
		}else if (source == translation_y_lum) {
				formInst.moveLight1_xplus(-5.0f,0,1,0);
		}else if (source == translationz_lum) {
				formInst.moveLight1_xplus(5.0f,0,0,1);
		}else if (source == translation_z_lum) {
				formInst.moveLight1_xplus(-5.0f,1,0,0);
		}

		else if (source == rotation_x) {
			formInst.rotatex(-6.0);

		} else if (source ==rotationx) {
			formInst.rotatex(6.0);

		}

		else if (source == rotation_y) {
			formInst.rotatey(-6.0);

		} else if (source ==rotationy) {
			formInst.rotatey(6.0);

		}

		else if (source == rotation_z) {
			formInst.rotatez(-6.0);

		} else if (source ==rotationz) {
			formInst.rotatez(6.0);

		}
		
		canvasCube.setVisible(false);
		canvasCube.display();
		canvasCube.setVisible(true);
	}

}

Conclusion :

Pour activer Opengl vous devez copier les fichiers gluegen-rt.jar, jogl.jar et jogl-natives-win32 dans le répertoire "C:\Program Files\Java\jdk1.7.0\jre\lib\ext" et les fichiers gluegen-rt.dll,jogl.dll, jogl_awt.dll et jogl_cg.dll dans le répertoire C:\Program Files\Java\jdk1.7.0\jre\bin

Contactez moi sur benfredjmohamedali@gmail.com
LinkedIn: http://www.linkedin.com/pub/mohamed-ali-ben-fredj/27/29a/a31

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.