# JOGL 3D Cube

In this section, we will learn how to create a cube. So for that firstly create six quadrilaterals of same size and positioned them in such a way that by connecting them we will get a proper cube. We will also rotate this cube to see all its sides.

To create a rotating cube, go through the below example: -

J3DCube.java

1. package com.tpoint.jogl;
2.
3.
4. import javax.media.opengl.GL2;
5. import javax.media.opengl.*;
6. import javax.media.opengl.awt.GLCanvas;
7. import javax.media.opengl.glu.GLU;
8.
9. import javax.swing.JFrame;
10.
11. import com.jogamp.opengl.util.FPSAnimator;
12.
13. public class J3DCube implements GLEventListener {
14.
15.
16. private GLU glu = new GLU();
17. private float rotation = 0.0f;
18.
19.    @Override
20. public void display( GLAutoDrawable drawable ) {
21.
22. final GL2 gl = drawable.getGL().getGL2();
23. gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT );
25. gl.glTranslatef( 0f, 0f, -2.0f );
26.
27. gl.glRotatef(rotation, 1.0f, 1.0f, 1.0f);
28.
29.
31. gl.glColor3f(0f,0f,1f); //Blue color
33. gl.glVertex3f(0.5f, 0.5f, -0.5f); //Upper Right
34. gl.glVertex3f( -0.5f, 0.5f, -0.5f); // Upper Left
35. gl.glVertex3f( -0.5f, 0.5f, 0.5f ); // Bottom Left
36. gl.glVertex3f( 0.5f, 0.5f, 0.5f ); // Bottom Right
38. gl.glColor3f( 1f,0f,0f ); //Red color
39. gl.glVertex3f( 0.5f, -0.5f, 0.5f ); // Upper Right
40. gl.glVertex3f( -0.5f, -0.5f, 0.5f ); // Upper Left
41. gl.glVertex3f( -0.5f, -0.5f, -0.5f ); // Bottom Left
42. gl.glVertex3f( 0.5f, -0.5f, -0.5f ); // Bottom Right
44. gl.glColor3f( 0f,1f,0f ); //Green color
45. gl.glVertex3f( 0.5f, 0.5f, 0.5f ); // Upper Right
46. gl.glVertex3f( -0.5f, 0.5f, 0.5f ); // Upper Left
47. gl.glVertex3f( -0.5f, -0.5f, 0.5f ); // Bottom Left
48. gl.glVertex3f( 0.5f, -0.5f, 0.5f ); // Bottom Right
50. gl.glColor3f( 1f,1f,0f ); //Yellow
51. gl.glVertex3f( 0.5f, -0.5f, -0.5f ); // Bottom Left
52. gl.glVertex3f( -0.5f, -0.5f, -0.5f ); // Bottom Right
53. gl.glVertex3f( -0.5f, 0.5f, -0.5f ); // Upper Right
54. gl.glVertex3f( 0.5f, 0.5f, -0.5f ); // Upper Left
56. gl.glColor3f( 1f,0f,1f ); //Purple
57. gl.glVertex3f( -0.5f, 0.5f, 0.5f ); // Upper Right
58. gl.glVertex3f( -0.5f, 0.5f, -0.5f ); // Upper Left
59. gl.glVertex3f( -0.5f, -0.5f, -0.5f ); // Bottom Left
60. gl.glVertex3f( -0.5f, -0.5f, 0.5f ); // Bottom Right
62. gl.glColor3f( 0f,1f, 1f ); //Cyan
63. gl.glVertex3f( 0.5f, 0.5f, -0.5f ); // Upper Right
64. gl.glVertex3f( 0.5f, 0.5f, 0.5f ); // Upper Left
65. gl.glVertex3f( 0.5f, -0.5f, 0.5f ); // Bottom Left
66. gl.glVertex3f( 0.5f, -0.5f, -0.5f ); // Bottom Right
67. gl.glEnd();
68. gl.glFlush();
69.
70. rotation += 0.6f;
71.    }
72.
73.    @Override
74. public void dispose( GLAutoDrawable drawable ) {
75.
76.    }
77.
78.    @Override
79. public void init( GLAutoDrawable drawable ) {
80.
81.    }
82.
83.    @Override
84. public void reshape( GLAutoDrawable drawable, int x, int y, int width, int height ) {
85.
86. final GL2 gl = drawable.getGL().getGL2();
87. if( height <= 0 )
88. height = 1;
89.
90. final float h = ( float ) width / ( float ) height;
91. gl.glViewport( 00, width, height );
92. gl.glMatrixMode( GL2.GL_PROJECTION );
94.
95. glu.gluPerspective( 45.0f, h, 1.020.0 );
96. gl.glMatrixMode( GL2.GL_MODELVIEW );
98.    }
99.
100. public static void main( String[] args ) {
101.
102. final GLProfile gp = GLProfile.get( GLProfile.GL2 );
103. GLCapabilities cap = new GLCapabilities(gp );
104.
105.
106. final GLCanvas gc = new GLCanvas(cap);
107.       J3DCube cube = new J3DCube();
108.