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.

Thursday, April 28, 2016

Calendar

This project is about a calendar which displays the various dates from 1 to 31 and the weekdays(Sun to Sat).
The project can be modified to select a particular month or year of the calendar on mouse input. This is left as an exercise.

Snapshot:


Here is the source.

Galaxy and cylinder

Two projects in this post. The first one is on Galaxy where the points(stars) are moving towards the viewer or looks like the space ship is moving in an infinite star system.
Snapshot:


Here is the source.

The other project is about a group of points in a cylinder rotating about the axis of the cylinder...

Snapshot:


Here is the source.