Search Projects

Tuesday, November 24, 2015

Scenery - Java OpenGL Graphics Programming

In this post we are going to learn about the Java OpenGL Graphics Programming or Java OGL Programming. Like C++ OpenGL API Library are also used in Java programming. According to Wikipedia - Java OpenGL (JOGL) is a wrapper library that allows OpenGL to be used in the Java programming language.

Opengl programming in java is compare to C++ is little bit tricky but no hard as you might think. C/ C++ are considered tougher than java, yeah they truly are. In this post we are going to learn Java OpenGL Graphics Programming. For that First we need to understand our requirements.

Prerequisite

Tools installed in your computer - 
  • Java JDK with proper path setup - Download from Oracle.
  • Java Binding for OpenGL - Downlaod it here.
  • IDE like Eclipse with OGL configured (Optional).
For learning Opengl programming in java you must have above tool properly configured. Since Eclipse is most preferred for Working in Java you might like to use it or go for other IDE. You must have the JDK configured and also the check Java OpenGL properly configured. Two folder jar and lib are most important, descriptions are given below. 

jar/ - contains all of the Java bytecode (.class files) for JOGL grouped into .JAR files.
lib/ - contains the native libraries (.so for Linux / OS X, .dll for Windows) for your platform.

About Scenery Opengl programming in java

First we need to know about what we are going to program here. We will program a simple scenery in Java Opengl. The scenery will contain the sun, sky, mountain, tree, road, cars and flag post. You can view the image below.
opengl programming in java

OpenGL in java give you power for graphics programming in java. You can make powerful Java OpenGL Graphics Programming with the use of API. 

First Step :  Create Package

First create a package name it sceneryjogl. Define the class in the package as well. 

package sceneryjogl

public class sceneryjogl{

Initialize the basic codes

Each of the Java Graphics program we will have some basic code that are required to get stated with. First you need to extend the class to proper Applet. Call the init functions and the graphics component functions as well.

Import the Java Packages

Note As you start coding you need to import java AWT and Applet and other packages, import them. Most important don't forget to import the OGL packages. Here is all the imports -

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.applet.*;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;

Extend the  Package Class

Here we will extend the main class to JApplet to our program. This help access the necessary files for graphical user interface. Also define the init function and call the paint.

public class sceneryjogl extends JApplet{

public static void main (String [] args){

}
public void init () {  

}
public void paintComponent (Graphics g) {
super.paintComponent (g);
}
}

Next Move is to fill the code. Yeah! Call the Jframe, the container give it title and other attributes to it. Initialize the component and add it to the container. "Edi Haryanto | 8011171 | Absen : 28" can be changed as you want the text to appear. The Program will exit on closing the window.

public static void main (String [] args){ 
JFrame frame= new JFrame ();
frame.setTitle("Edi Haryanto | 8011171 | Absen : 28");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet= new sceneryjogl ();
applet.init();
frame.getContentPane().add(applet);
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
}
public void init () {  
JPanel panel4 = new Panel2D ();
getContentPane().add(panel4);
}

Now we have to draw the objects. First choose the color you want to add and then fill the object with proper coordinates. Below is the code for green bushes, place the code in paintComponent. Rest of the code can be download with full source code.

//Gambar Tanaman
g.setColor(Color.green);
g.fillOval(0, 500, 30,30);
g.fillOval(25, 500, 30,30);
g.fillOval(50, 500, 30,30);
g.fillOval(15, 480, 30,30);
g.fillOval(32, 480, 30,30);
g.fillOval(90, 500, 30,30);
g.fillOval(115, 500, 30,30);
g.fillOval(140, 500, 30,30);
g.fillOval(105, 480, 30,30);
g.fillOval(122, 480, 30,30);
g.fillOval(180, 500, 30,30);
g.fillOval(205, 500, 30,30);
g.fillOval(230, 500, 30,30);
g.fillOval(195, 480, 30,30);
g.fillOval(212, 480, 30,30);
g.fillOval(270, 500, 30,30);
g.fillOval(295, 500, 30,30);
g.fillOval(320, 500, 30,30);
g.fillOval(285, 480, 30,30);
g.fillOval(302, 480, 30,30);
g.fillOval(510, 500, 30,30);
g.fillOval(535, 500, 30,30);
g.fillOval(560, 500, 30,30);
g.fillOval(525, 480, 30,30);
g.fillOval(542, 480, 30,30);
g.fillOval(600, 500, 30,30);
g.fillOval(625, 500, 30,30);
g.fillOval(650, 500, 30,30);
g.fillOval(615, 480, 30,30);
g.fillOval(632, 480, 30,30);
g.fillOval(690, 500, 30,30);
g.fillOval(715, 500, 30,30);
g.fillOval(740, 500, 30,30);
g.fillOval(705, 480, 30,30);
g.fillOval(722, 480, 30,30);
g.fillOval(780, 500, 30,30);
g.fillOval(805, 500, 30,30);
g.fillOval(830, 500, 30,30);
g.fillOval(795, 480, 30,30);
g.fillOval(812, 480, 30,30);
g.fillOval(870, 500, 30,30);
g.fillOval(895, 500, 30,30);
g.fillOval(920, 500, 30,30);
g.fillOval(885, 480, 30,30);
g.fillOval(902, 480, 30,30);
g.fillOval(960, 500, 30,30);
g.fillOval(985, 500, 30,30);
g.fillOval(1010, 500, 30,30);
g.fillOval(975, 480, 30,30);
g.fillOval(992, 480, 30,30); 

Similarly other objected can be painted using the paint. Download the whole source code for this Java OpenGL Graphics Programming and share you views. If you have any doubt don't hesitate to put your comment and ask the same. Keep learning!

Download the Source file -  Name it as java file and run on your computer.