JOGL Basic Templates
- JOGL - GLJPanel Class
- JOGL - Canvas with Swing
- JOGL - Canvas with AWT
- JOGL - API for Basic Templates
JOGL Graphical Shapes
JOGL Effects & Transformation
JOGL 3D Graphics
JOGL Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
JOGL - Coloring
This chapter teaches you how to apply colours to the objects using JOGL. To apply colour to an object, use the method glColor() of GL2. Below given is the syntax for using glColor method.
Syntax
gl.glColorXY(1f,0f,0f);
where,
X denotes the number of colours used, 3 (red, green, blue) or 4(red, green, blue, alpha). To get various colour combinations, the values of these colours are passed as parameters. The sequence of the colour parameters must be maintained in that order.
Example
If you pass colour values as (1, 0, 0), then you get red colour. Similarly, (1, 1, 0) gives you yellow colour.
Y denotes the data type which accepts parameters such as byte(b), double(d), float(f), int(i), short(s), ubyte(ub), uint(ui), and ushort(us).
gl.glColor3f(1f,0f,0f); //gives us red gl.glColor3f(0f,1f,0f); //gives us green gl.glColor3f(0f,0f,1f); //gives us blue
In case of triangle, you can apply different colors for each vertex.
Let us go through the program to apply colors to a triangle −
import javax.media.opengl.GL2; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCapabipties; import javax.media.opengl.GLEventListener; import javax.media.opengl.GLProfile; import javax.media.opengl.awt.GLCanvas; import javax.swing.JFrame; pubpc class TriangleColor implements GLEventListener { @Override pubpc void display( GLAutoDrawable drawable ) { final GL2 gl = drawable.getGL().getGL2(); gl.glBegin( GL2.GL_TRIANGLES ); // Drawing Using Triangles gl.glColor3f( 1.0f, 0.0f, 0.0f ); // Red gl.glVertex3f( 0.5f,0.7f,0.0f ); // Top gl.glColor3f( 0.0f,1.0f,0.0f ); // green gl.glVertex3f( -0.2f,-0.50f,0.0f ); // Bottom Left gl.glColor3f( 0.0f,0.0f,1.0f ); // blue gl.glVertex3f( 0.5f,-0.5f,0.0f ); // Bottom Right gl.glEnd(); } @Override pubpc void dispose( GLAutoDrawable arg0 ) { //method body } @Override pubpc void init( GLAutoDrawable arg0 ) { // method body } @Override pubpc void reshape( GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4 ) { // method body } pubpc static void main( String[] args ) { //getting the capabipties object of GL2 profile final GLProfile profile = GLProfile.get( GLProfile.GL2 ); GLCapabipties capabipties = new GLCapabipties(profile); // The canvas final GLCanvas glcanvas = new GLCanvas( capabipties ); TriangleColor triangle = new TriangleColor(); glcanvas.addGLEventListener( triangle ); glcanvas.setSize( 400, 400 ); //creating frame final JFrame frame = new JFrame (" Colored Triangle"); //adding canvas to it frame.getContentPane().add( glcanvas ); frame.setSize( frame.getContentPane().getPreferredSize()); frame.setVisible( true ); } //end of main } //end of class
When you compile and execute the above program, you get the following colored triangle −
Applying Color to a Polygon
Let us go through the program to apply colors to a polygon −
import javax.media.opengl.GL2; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCapabipties; import javax.media.opengl.GLEventListener; import javax.media.opengl.GLProfile; import javax.media.opengl.awt.GLCanvas; import javax.swing.JFrame; pubpc class PolygonColor implements GLEventListener { @Override pubpc void display( GLAutoDrawable drawable ) { final GL2 gl = drawable.getGL().getGL2(); gl.glColor3f( 1f,0f,0f ); //applying red gl.glBegin( GL2.GL_POLYGON ); gl.glVertex3f( 0f,0.5f,0f ); gl.glVertex3f( -0.5f,0.2f,0f ); gl.glVertex3f( -0.5f,-0.2f,0f ); gl.glVertex3f( 0f,-0.5f,0f ); gl.glVertex3f( 0f,0.5f,0f ); gl.glVertex3f( 0.5f,0.2f,0f ); gl.glVertex3f( 0.5f,-0.2f,0f ); gl.glVertex3f( 0f,-0.5f,0f ); gl.glEnd(); } @Override pubpc void dispose( GLAutoDrawable arg0 ) { //method body } @Override pubpc void init( GLAutoDrawable arg0 ) { // method body } @Override pubpc void reshape( GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4 ) { // method body } pubpc static void main( String[] args ) { //getting the capabipties object of GL2 profile final GLProfile profile = GLProfile.get( GLProfile.GL2 ); GLCapabipties capabipties = new GLCapabipties(profile); // The canvas final GLCanvas glcanvas = new GLCanvas( capabipties ); PolygonColor polygon = new PolygonColor(); glcanvas.addGLEventListener( polygon ); glcanvas.setSize( 400, 400 ); //creating frame final JFrame frame = new JFrame ( "Colored Polygon" ); //adding canvas to frame frame.getContentPane().add( glcanvas ); frame.setSize(frame.getContentPane().getPreferredSize() ); frame.setVisible( true ); } //end of main } //end of class
When you compile and execute the above program, you get the following coloured Polygon −
Advertisements