Friday, September 9, 2016

#JOGL Simple Program to draw a Triangle and rotate it about z-axis

Although the use of JPanel and calling repaint method on it is a good way to draw in Java Bindings for OpenGL I have followed the other method of creating a GLCanvas object and adding it to the JFrame object. In this program I have created a basic frame which sets the background color to red and clears the canvas and then draws a triangle. Each time the display is called by the FPSAnimator object the triangle is drawn at a new position after rotation by an angle. Angle is incremented in the display function itself.

Snapshot:

Here is the source in case you are interested to know.

Friday, September 2, 2016

Translate a Circle by pressing Swing Class JButton in #JOGL

This program translates the circle on pressing the translate button.


Here is the source:(online Viewing)


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


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.glu.GLU;
import com.jogamp.opengl.awt.GLJPanel;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

class CircleTranslate extends GLJPanel implements GLEventListener,ActionListener {

/**
 * Interface to the GLU library.
 */
private GLU glu;
int x,y;
static JButton okButton;
/**
 * Take care of initialization here.
 */


CircleTranslate()
{
// super(g);
super( new GLCapabilities(null) ); // Makes a panel with default OpenGL "capabilities".
GLJPanel drawable = new GLJPanel();               // new GLJPanel inside GLJPanel
drawable.setPreferredSize(new Dimension(200,100));
setLayout(new BorderLayout());
add(drawable, BorderLayout.CENTER);
drawable.addGLEventListener(this); // Set up events for OpenGL drawing!
// drawable.addMouseListener(this);
//drawable.addMouseMotionListener(this);
//drawable.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {
// System.exit(0);
x++;
repaint();
}
public void init(GLAutoDrawable gld) {
    GL2 gl = gld.getGL().getGL2();
    glu = new GLU();

    gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
//    gl.glViewport(-250, -150, 250, 150);
    gl.glMatrixMode(GL2.GL_PROJECTION);
    gl.glLoadIdentity();
    glu.gluOrtho2D(-250.0, 250.0, -150.0, 150.0);
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    x=10;y=30;
    repaint();
}

/**
 * Take care of drawing here.
 */
public void display(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();
    gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
    /*
     * put your code here
     */
   
    drawCircle(gl, x, y, 50);
}

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


private void drawCircle(GL2 gl, int x1, int y1, int r) {
    gl.glPointSize(1.0f);
    gl.glBegin(GL2.GL_POINTS);
   
    int x=0,y=r,p=1-r;
    while(x<y)
    {
   
    if(p<0)
    p+=2*x+3;
    else {
    p+=2*(x-y)+5;
    y--;
    }
    x++;
    circleSymmetry(gl,x1,y1,x,y);

    }
    gl.glEnd();//end drawing of points

}
public void dispose(GLAutoDrawable arg0)
{
}
public void circleSymmetry(GL2 gl,int xc,int yc,int x,int y)
{
gl.glVertex2i(xc+x , yc+y );
gl.glVertex2i(xc-x , yc+y );
gl.glVertex2i(xc+x , yc-y );
gl.glVertex2i(xc-x , yc-y );
gl.glVertex2i(xc+y , yc+x );
gl.glVertex2i(xc+y , yc-x );
gl.glVertex2i(xc-y , yc+x );
gl.glVertex2i(xc-y , yc-x );
}
public static void main(String args[])
{
JFrame window = new JFrame("Circle Translate");
    // The canvas
    CircleTranslate panel = new CircleTranslate();
    panel.setPreferredSize(new Dimension(1000,1000));      
    okButton = new JButton("Translate");
    JPanel content = new JPanel();

content.setLayout(new BorderLayout());
content.add(panel, BorderLayout.CENTER);
content.add(okButton, BorderLayout.SOUTH);
    okButton.addActionListener(panel);
   
    window.setContentPane(content);
    window.pack();
    window.setLocation(0,0);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
    panel.requestFocusInWindow();
}
}
Thanks!!!

Friday, July 22, 2016

Simple program to create JFrame and add a JPanel to it and draw using Graphics class in Java

This program is self explanatory...

I have done this to demonstrate simple way of creating A window(using Java Swing) and display something inside a panel in it.

/* Program to display a rectangle, filled rectangle and a line all with different colors
 * and a text string
 */
import java.awt.Graphics;
import java.awt.Color;
//import java.awt.
import java.awt.event.*;
import javax.swing.JPanel;
import javax.swing.JFrame;
//import javax.swing.Timer;
public class FirstJavaGraphics extends JPanel
{
   
public static void main(String[] args)
{
    FirstJavaGraphics fjg=new FirstJavaGraphics();
    JFrame window=new JFrame("Hello Java Graphics");
    window.setSize(640, 480);
    window.add(fjg);
    window.setVisible(true);
}
public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    setBackground(Color.RED);
    g.drawRect(100, 20, 30, 30);
    g.setColor(Color.GREEN);
    g.fillRect(200, 320, 40, 40);
    g.drawLine(100, 100, 200, 200);
    g.setColor(Color.BLUE);
    g.drawString("Hello WOrld", 100, 250);
}
FirstJavaGraphics()
{
}
}

Snapshot:

The next program will be to animate the scene using a Timer in the swing package.

Monday, July 11, 2016

#JOGL-Events- Mouse and Keyboard handling using (immediate mode) animator class

These two programs I consider basic in handling mouse and keyboard events. I looked up some programming sites and had to collate info from variety of sources. If you are new to JOGL then I suggest you this site where you can download a PDF of e-book on computer graphics by David J Eck.

The first program i made was to handle keyboard events:
Here it is:
import java.awt.*;
import java.awt.event.*;

import com.jogamp.opengl.util.Animator;
import com.jogamp.opengl.util.gl2.*;
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.glu.GLU;

import javax.swing.JFrame;

class mykeyboardeventshandler implements KeyListener,GLEventListener
{
String msg="";
int x=10,y=20;
private GLU glu;
public void keyPressed(KeyEvent ke)
{
//showStatus("Key down");
}
public void keyReleased(KeyEvent ke)
{
//showStatus("Key released");
}
public void keyTyped(KeyEvent ke)
{
msg+=ke.getKeyChar();
//repaint();
}
/**
 * Take care of initialization here.
 */
public void init(GLAutoDrawable gld) {
    GL2 gl = gld.getGL().getGL2();
    glu = new GLU();

    gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    gl.glViewport(-250, -150, 250, 150);
    gl.glMatrixMode(GL2.GL_PROJECTION);
    gl.glLoadIdentity();
    glu.gluOrtho2D(-250.0, 250.0, -150.0, 150.0);
}

/**
 * Take care of drawing here.
 */
public void display(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();
    gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
    /*
     * put your code here
     */
//    drawLine(gl, 0, 0, 100, 50);
    gl.glPushMatrix();
    GLUT glut = new GLUT();
    gl.glRasterPos2i(0,10);
  //  gl.glTranslatef(0, 0, 0);
    gl.glColor3f(1, 0, 0);
    glut.glutBitmapString(GLUT.BITMAP_HELVETICA_18, msg);
    gl.glPopMatrix();
    gl.glFlush();
}

public void reshape(GLAutoDrawable drawable, int x, int y, int width,
        int height) {
}
public void dispose(GLAutoDrawable arg0)
{
}
}
public class keyboardeventsdemo
{
    public static void main(String[] args)
    {
    //getting the capabilities object of GL2 profile
    final GLProfile profile=GLProfile.get(GLProfile.GL2);
    GLCapabilities capabilities=new GLCapabilities(profile);
    // The canvas
    final GLCanvas glcanvas=new GLCanvas(capabilities);
    final Animator animator = new Animator(glcanvas);
    mykeyboardeventshandler b=new mykeyboardeventshandler();
    glcanvas.addGLEventListener(b);
    glcanvas.addKeyListener(b);
    glcanvas.setSize(400, 400);
    //creating frame
    final JFrame frame=new JFrame("Basic frame");
    //adding canvas to frame
    frame.add(glcanvas);
    frame.setSize(640,480);
    frame.setResizable(false);
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            animator.stop();
            frame.dispose();
            System.exit(0);
        }
});
    animator.start();
    frame.setVisible(true);
    glcanvas.requestFocus();
    }
}



Snapshot:
 
The second program i made is on handling mouse events. I made a separate class for handling mouse events and GLEvents called MyMouseEventsHandler. I instantiate it in the MouseEventsDemo class and them add event listeners to the frame using this object.

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.glu.GLU;
import com.jogamp.opengl.util.gl2.GLUT;
import com.jogamp.opengl.util.Animator;
import java.awt.*;
import java.awt.event.*;

import javax.swing.JFrame;

class MyMouseEventsHandler implements MouseMotionListener, MouseListener,GLEventListener
{
String msg="hello";
int mouseX=0,mouseY=0;
private GLU glu;
public void init()
{
}
public void mouseClicked(MouseEvent me)
{
mouseX=0;
mouseY=10;
msg="mouse clicked";
//repaint();
}
public void mouseEntered(MouseEvent me)
{
mouseX=0;
mouseY=10;
msg="mouse Entered";
//repaint();
}
public void mouseExited(MouseEvent me)
{
mouseX=0;
mouseY=10;
msg="mouse Exited";
//repaint();
}
public void mousePressed(MouseEvent me)
{
mouseX=0;
mouseY=10;
msg="mouse Pressed";
//repaint();
}
public void mouseReleased(MouseEvent me)
{
mouseX=me.getX();
mouseY=me.getY();
msg="Mouse Released";
//repaint();
}
public void mouseMoved(MouseEvent me)
{
//showStatus("Moving mouse at "+me.getX()+" , "+me.getY());
}
public void mouseDragged(MouseEvent me)
{
mouseX=me.getX();
mouseY=me.getY();
msg="mouse dragged";
//repaint();
}
public void init(GLAutoDrawable gld)
{
    GL2 gl = gld.getGL().getGL2();
    glu = new GLU();

    gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    gl.glViewport(-250, -150, 250, 150);
    gl.glMatrixMode(GL2.GL_PROJECTION);
    gl.glLoadIdentity();
    glu.gluOrtho2D(-250.0, 250.0, -150.0, 150.0);
}
public void display(GLAutoDrawable gld)
{
    GL2 gl = gld.getGL().getGL2();
    gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
    /*
     * put your code here
     */
//    drawLine(gl, 0, 0, 100, 50);
    gl.glPushMatrix();
    GLUT glut = new GLUT();
    gl.glRasterPos2i(0,10);
  //  gl.glTranslatef(0, 0, 0);
    gl.glColor3f(1, 0, 0);
    glut.glutBitmapString(GLUT.BITMAP_HELVETICA_18, msg);
    gl.glPopMatrix();
    gl.glFlush();
//    gld.swapBuffers();
}
public void reshape(GLAutoDrawable gld,int x,int y,int width,int height)
{
   
}
public void dispose(GLAutoDrawable gld)
{
   
}
}
public class MouseEventsDemo extends JFrame
{
    public static void main(String[] args)
    {
        final GLProfile profile=GLProfile.get(GLProfile.GL2);
        GLCapabilities capabilities=new GLCapabilities(profile);
        // The canvas
        final GLCanvas glcanvas=new GLCanvas(capabilities);
        MyMouseEventsHandler mmeh=new MyMouseEventsHandler();
        final Animator animator = new Animator(glcanvas);
//        glcanvas.addGLEventListener(mmeh);
        glcanvas.setSize(400, 400);
        //creating frame
        final JFrame frame=new JFrame("Basic frame");
        //adding canvas to frame
        frame.add(glcanvas);
        frame.setSize(640,480);
      
        frame.setResizable(false);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                animator.stop();
                frame.dispose();
                System.exit(0);
            }
});
         glcanvas.addGLEventListener(mmeh);
        glcanvas.addMouseMotionListener(mmeh);
        glcanvas.addMouseListener(mmeh);
        animator.start();
        frame.setVisible(true);
      
        glcanvas.requestFocus();
      
    }
}

 Snapshot:

 

Saturday, June 25, 2016

#JOGL DDA, Bresenhams, Mid-Point Circle and Mid-Point Ellipse Algorithms

I have implemented the above mentioned algorithms using Java Bindings for OpenGL.

Snapshots and Code:
Bresenhams: Code
Mid-Point Circle: Code
 DDA:Code
Ellipse: Code

Monday, May 30, 2016

#JOGL My First OpenGL program in Java

I recently got an interview call for a requirement for high end graphics(CUDA etc), with strong skills in java, hadoop and knowledge of spark were added advantages. It is very clear that java, python and more modern languages are the future of graphics.
I learn't that Java is gaining popularity in writing games in OpenGL. I am now forced to work on Java now that my college has made a switch to OpenGL using Java. So Bye-Bye-C. The ultimate goal of shifting to java is obviously to develop android games. OpenGL ES which stands for OpenGL for Embedded Systems (or GLES) is a subset of the OpenGL computer graphics rendering application programming interface (API) for rendering 2D and 3D computer graphics such as those used by video games, typically hardware-accelerated using a graphics processing unit (GPU).
My first program using JOGL draws a red polygon and a green line.

Here is a snapshot:

Here is the source.

Sunday, May 8, 2016

Rain Water Harvesting

RWH:-
In this project rain water is collected from roof tops and is stored in under ground tanks after purification and is pumped out for domestic and garden use.

Snapshot:






Here is the source.