Sunday, December 20, 2015

Using SIFT/SURF for Object Recognition in OpenCV Java

Hi All,

Today my post is on, how you can use SIFT/SURF algorithms for Object Recognition with OpenCV Java. I have shared this post on SURF feature detector previously. This is fully based on that post and therefore I'm just trying to show you how you can implement the same logic in OpenCV Java. It's important that you have to download previous OpenCV versions, so that you have SURF feature detector in your library. Because in the newer versions they have removed these Non-Free modules from the java wrapper. You can check this out.

I believe that you have some basic knowledge in working with OpenCV Java. If you want some beginner help you can refer the following links.
Now let's move on to our development. I'm using Eclipse for developing this. I'm going to use OpencCV 2.4.11. You can download it here

First create the user library for OpenCV as described in the previous link and add it to the build path. Then we can start developing the code for object recognition. Following is my eclipse project. I have added the OpenCV 2.4.11 library as a user library and added it to the build path. 

I'm using following images for object recognition. 

1. Object we are going to recognize. 

2. Scene that we are going to recognize the object from. 

Now following is the Java implementation of our SURF feature detector. 



Following are our outputs. 

1. Identified key-points of the object.



2. Matching the object keypoints with the scene. 


3. Object recognized image. 


You can do the same with SIFT feature detector by just changing the Feature Detector and Descriptor Extractor name to SIFT. 

So that's it. 

Hope that helps. 

Thank You. :-) 



Sunday, December 13, 2015

Develop OpenCV Cross Platform Applications with Java

Hi All,

Today I'm going to show you how you can start developing OpenCV applications with Java that can work independently from the underlying OS. This might not be the best implementation, but what I'm going to share works fine.

I assume you have some basic knowledge in working with OpenCV in Java. You can easily download OpenCV for Windos/Linux/Mac from OpenCV downloads. Here I'm going to use OpenCV 2.4.11. You can download your preferred version.

I'm going to use the following simple code to demonstrate that OpenCV is working.
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;

public class Main
{
 public static void main(String[] args)
 {
  System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  Mat mat = Mat.eye(3, 3, CvType.CV_8UC1);
                System.out.println("mat = " + mat.dump());
 }
}

Our purpose here is to provide the above code to work in any platform. ( Here I'm going to show only for Windows and Linux. You can use similar method for Mac)

Therefore first we have to build necessary .dll and .so files so that we can use them in our application. For Windows it's very easy because, when you download OpenCV for windows and extract it to a particular location you can find the .dll files in \build\java\x64 and in \build\java\x86.

Next we have to build the .so files.

In your Linux machine that you are going to build .so files, make sure you have installed the following programs before you follow the next set of instructions.

1. g++
2. cmake
3. ant

You can install the above programs by just using 'sudo apt-get install ' .
Make sure you have set the Java path in your terminal before you execute the commands. You can use 'export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64' command to do this.

Once you have set up your environment you can use the following instructions to build the .jar and .so files for opencv.

1. Download OpenCV for Linux.
2. Extract the .zip file to a location.
3. CD into that location.
4. Execute the following commands.

mkdir release
cd release
cmake -DBUILD_SHARED_LIBS=OFF ..
make -j8
When you run the cmake -DBUILD_SHARED_LIBS=OFF .. command make sure you get the following output. Otherwise you won't get the .so and .jar files created.


Inside the release folder you can find the newly created files. Inside 'bin' folder you can find the 'opencv-2411.jar' and inside 'lib' folder you can find the 'libopencv_java2411.so' file.

You can build for both Linux 32 and 64 bit environments.

Now we are ready to develop our OpenCV program for both Windows and Linux environments along with their respective bitness (64 or 32).

I am using Eclipse to develop my application. First add the 'opencv-2411.jar' to your project and add it to the Build Path.

Then copy the .dll and .so files for both 32 and 64bit versions to a folder inside the project. Now with the following code, you can start loading the libraries for the respective OS.
if (os.toUpperCase().contains("WINDOWS"))
  {
   if (bitness.endsWith("64"))
   {
    lib = new File("native/64x" + System.mapLibraryName("opencv_java2411"));
   }
   else
   {
    lib = new File("native/86x" + System.mapLibraryName("opencv_java2411"));
   }
  }
  else if (os.toUpperCase().contains("LINUX"))
  {
   if (bitness.endsWith("64"))
   {
    lib = new File("native/64x" + System.mapLibraryName("opencv_java2411"));
   }
   else
   {
    lib = new File("native/86x" + System.mapLibraryName("opencv_java2411"));
   }
  }
  
  System.out.println(lib.getAbsolutePath());
  System.load(lib.getAbsolutePath());

Insert the above code block inside your class. Then you can run the previous demo code without using the 'System.loadLibrary(Core.NATIVE_LIBRARY_NAME);' line, because we have already loaded the library from the above block.

Following is the complete code. I have loaded the library inside the main method. In your applications you can use it inside a Static code block according to your application.
import java.io.File;

import org.opencv.core.CvType;
import org.opencv.core.Mat;

public class Main
{
 public static void main(String[] args)
 {
  File lib = null;
  String os = System.getProperty("os.name");
  String bitness = System.getProperty("sun.arch.data.model");
  
  if (os.toUpperCase().contains("WINDOWS"))
  {
   if (bitness.endsWith("64"))
   {
    lib = new File("lib/opencv/64x" + System.mapLibraryName("opencv_java2411"));
   }
   else
   {
    lib = new File("lib/opencv/86x" + System.mapLibraryName("opencv_java2411"));
   }
  }
  else if (os.toUpperCase().contains("LINUX"))
  {
   if (bitness.endsWith("64"))
   {
    lib = new File("lib/opencv/64x" + System.mapLibraryName("opencv_java2411"));
   }
   else
   {
    lib = new File("lib/opencv/86x" + System.mapLibraryName("opencv_java2411"));
   }
  }
  
  System.out.println(lib.getAbsolutePath());
  System.load(lib.getAbsolutePath());
  
  Mat mat = Mat.eye(3, 3, CvType.CV_8UC1);
        System.out.println("mat = " + mat.dump());
 }
}


Here my native .dll and .so files are inside a folder named 'lib/opencv/'.


You will get the following output. 

Hope this helps.

Thank you..

:-)

Sunday, June 14, 2015

New Features in Java SE 8: A Developer's Guide

JOptionPane with Multiple Inputs

Assume that you want to provide a JOptionPane, Input Message Dialog with multiple input fields. There are several ways that you can do this. Among them this is one simple solution that you can try out.

Following is the Input Dialog that we are going to develop.


Following is the main method that you have to use in order to develop the JOptionPane with multiple inputs.
private void showMultipleInputMessageDialog() {

        textArea.setText("");
        JTextField textField1 = new JTextField();
        final JTextField textField2 = new JTextField();
        final JCheckBox checkBox = new JCheckBox();
        textField2.setEnabled(false);

        checkBox.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                if (e.getSource() == checkBox) {
                    if (checkBox.isSelected()) {
                        textField2.setEnabled(true);
                    } else {
                        textField2.setEnabled(false);
                    }
                }
            }
        });

        Object[] inputFields = {"Enter Text 01", textField1,
                "Enable TextField 02", checkBox,
                "Enter Text 02", textField2};

        int option = JOptionPane.showConfirmDialog(this, inputFields, "Multiple Inputs", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);

        if (option == JOptionPane.OK_OPTION) {
            String text = textField1.getText() + "\n" + (checkBox.isSelected() ? "Checked" : "Unchecked") + "\n" + textField2.getText() + "\n";
            textArea.setText(text);
        }
    }

Following is the complete JFrame with the demo application.

package com.dummy.learning;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by Kinath.
 */
public class ApplicationFrame extends JFrame implements ActionListener {

    private static ApplicationFrame instance;

    JPanel jPanel = new JPanel();
    JButton jButton = new JButton("Click");
    JTextArea textArea = new JTextArea(5, 100);
    GridBagLayout gridBagLayout1 = new GridBagLayout();
    GridBagLayout gridBagLayout2 = new GridBagLayout();

    public static ApplicationFrame getInstance() {
        if (instance == null) {
            instance = new ApplicationFrame();
        }
        return instance;
    }

    public ApplicationFrame() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(new Dimension(300, 250));
        this.setTitle("JOptionPane Demo");
        this.setResizable(false);
        setGui();
    }

    private void setGui() {
        this.setLayout(gridBagLayout1);
        this.getContentPane().add(jPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
        jPanel.setLayout(gridBagLayout2);
        jPanel.setBorder(BorderFactory.createEtchedBorder());
        jButton.setMinimumSize(new Dimension(100, 25));
        jButton.setMaximumSize(new Dimension(100, 25));
        jButton.setPreferredSize(new Dimension(100, 25));
        jButton.addActionListener(this);
        jPanel.add(jButton, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        jPanel.add(textArea, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jButton) {
            showMultipleInputMessageDialog();
        }
    }

    private void showMultipleInputMessageDialog() {

        textArea.setText("");
        JTextField textField1 = new JTextField();
        final JTextField textField2 = new JTextField();
        final JCheckBox checkBox = new JCheckBox();
        textField2.setEnabled(false);

        checkBox.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                if (e.getSource() == checkBox) {
                    if (checkBox.isSelected()) {
                        textField2.setEnabled(true);
                    } else {
                        textField2.setEnabled(false);
                    }
                }
            }
        });

        Object[] inputFields = {"Enter Text 01", textField1,
                "Enable TextField 02", checkBox,
                "Enter Text 02", textField2};

        int option = JOptionPane.showConfirmDialog(this, inputFields, "Multiple Inputs", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);

        if (option == JOptionPane.OK_OPTION) {
            String text = textField1.getText() + "\n" + (checkBox.isSelected() ? "Checked" : "Unchecked") + "\n" + textField2.getText() + "\n";
            textArea.setText(text);
        }
    }
}

Hope that helps.
Thank You. :-)

Sunday, May 24, 2015

Java Grid Bag Layout Tutorial

Hi All,

Today we are going to learn how we can use Java GridBagLayout to develop you GUI for your application. GridBagLayout is a flexible layout manager that comes with java.

According to oracle documentation,

A GridBagLayout places components in a grid of rows and columns, allowing specified components to span multiple rows or columns. Not all rows necessarily have the same height. Similarly, not all columns necessarily have the same width. Essentially, GridBagLayout places components in rectangles (cells) in a grid, and then uses the components' preferred sizes to determine how big the cells should be.

A component is placed and positioned inside a container by specifying the ‘GridBagConstraints’ for each component. When you are adding the component inside the container, you can provide GridBagConstraints object as a parameter.

It’s important to understand each parameter of the GridBagConstraints constructor. Therefore please go through the following document to get a good understanding about them.

Let’s go through a demo GUI application and learn how to do it.
Following is the GUI that we are going to develop. This sketch was developed using Pencil (http://pencil.evolus.vn/).


Before developing the GUI, we should first understand the Grid we are positioning our components. Following is how I have divided the main frame as a grid, so that the main panels get positioned in the given space.


I am using IntelliJ Idea 14 to develop this Demo Application.

Following are the initial Classes developed to setup the main panel.

Left-Top-Panel
This panel contains a JTabbedPane and a JTable which is positioned in NORTH and SOUTH cells inside the panel. For the ‘fill’ parameter in GridBagConstraints I have used BOTH in order to make the component fill its display area entirely.

package com.dummyscodes.learning;

import javax.swing.*;
import java.awt.*;

/**
 * Created by Kinath on 05/04/2015.
 */
public class LeftTopPanel extends JPanel {

    private JTabbedPane jTabbedPane;
    private JPanel panel01;
    private JPanel panel02;
    private JPanel panel03;
    private JTable jTable;
    private GridBagLayout gridBagLayoutLeftTop;
    private JScrollPane jScrollPane;

    public LeftTopPanel() {
        setupGui();
    }

    private void setupGui() {
        jTabbedPane = new JTabbedPane();
        panel01 = new JPanel();
        panel02 = new JPanel();
        panel03 = new JPanel();
        jTable = new JTable();

        gridBagLayoutLeftTop = new GridBagLayout();

        this.setBorder(BorderFactory.createTitledBorder("Left-Top-Panel"));
        this.setLayout(gridBagLayoutLeftTop);

        jTabbedPane.add("Banana", panel01);
        jTabbedPane.add("Pummelo", panel02);
        jTabbedPane.add("Mango", panel03);

        // Create columns names
        String columnNames[] = {"Column 1", "Column 2", "Column 3", "Column 4"};

        // Create some data
        String dataValues[][] =
                {
                        {"12", "234", "67", "52"},
                        {"-123", "43", "853", "234"},
                        {"93", "89.2", "109", "23"},
                        {"279", "9033", "3092", "2342"},
                        {"12", "234", "67", "52"},
                        {"-123", "43", "853", "234"},
                        {"93", "89.2", "109", "23"},
                        {"279", "9033", "3092", "2342"},
                        {"12", "234", "67", "52"},
                        {"-123", "43", "853", "234"},
                        {"93", "89.2", "109", "23"},
                        {"279", "9033", "3092", "2342"},
                        {"12", "234", "67", "52"},
                        {"-123", "43", "853", "234"},
                        {"93", "89.2", "109", "23"},
                        {"279", "9033", "3092", "2342"}
                };

        jTable = new JTable(dataValues, columnNames);
        jScrollPane = new JScrollPane(jTable);

        this.add(jTabbedPane, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
        this.add(jScrollPane, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
    }
}
Right-Top-Panel 
This panel also uses simple GridBagConstraints. Here I have divided the right-top-panel into 3 vertical cells and positioned the components inside them. TextField and JButton are allowed to fill the area in HORIZONTAL way. JScrollpane is allowed to fill the entire area.
package com.dummyscodes.learning;

import javax.swing.*;
import java.awt.*;

/**
 * Created by Kinath on 05/04/2015.
 */
public class RightTopPanel extends JPanel {

    JTextField textField = new JTextField();
    JButton addButton = new JButton();
    JList jList;
    JScrollPane jScrollPane;
    GridBagLayout gridBagLayout = new GridBagLayout();

    public RightTopPanel() {
        setupGui();
    }

    private void setupGui() {
        this.setBorder(BorderFactory.createTitledBorder("Right-Top-Panel"));
        this.setLayout(gridBagLayout);

        String[] items = {"Item 01", "Item 02", "Item 03", "Item 04", "Item 05", "Item 06", "Item 07", "Item 08", "Item 09", "Item 10"};
        jList = new JList(items);
        jScrollPane = new JScrollPane(jList);

        int width = this.getWidth();
        Dimension dimension = new Dimension(width, 25);
        addButton.setPreferredSize(dimension);
        addButton.setMinimumSize(dimension);
        addButton.setMaximumSize(dimension);
        addButton.setText("Add");

        textField.setPreferredSize(dimension);
        textField.setMinimumSize(dimension);
        textField.setMaximumSize(dimension);

        this.add(textField, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));
        this.add(addButton, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));
        this.add(jScrollPane, new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0));

    }
}
Right-Bottom-Panel
Same as the above layouts.
package com.dummyscodes.learning;

import javax.swing.*;
import java.awt.*;

/**
 * Created by Kinath on 05/04/2015.
 */
public class RightBottomPanel extends JPanel {

    JLabel label01 = new JLabel("Text Label Text");
    JLabel label02 = new JLabel("Text Label Text");
    JRadioButton radio01 = new JRadioButton("Radio Button 01");
    JRadioButton radio02 = new JRadioButton("Radio Button 02");
    JRadioButton radio03 = new JRadioButton("Radio Button 03");
    JSpinner spinner = new JSpinner();
    GridBagLayout gridBagLayout = new GridBagLayout();

    public RightBottomPanel() {
        setupGui();
    }

    private void setupGui()
    {
        this.setBorder(BorderFactory.createTitledBorder("Right-Bottom-Panel"));
        this.setLayout(gridBagLayout);

        this.add(label01 ,  new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));
        this.add(radio01 ,  new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));
        this.add(radio02 ,  new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));
        this.add(radio03 ,  new GridBagConstraints(0, 3, 1, 1, 1.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));
        this.add(label02 ,  new GridBagConstraints(0, 4, 1, 1, 1.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));
        this.add(spinner ,  new GridBagConstraints(0, 5, 1, 1, 1.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));
    }
}

Bottom-Panel 
This panel consists of horizontally placed set of buttons on a horizontal grid cells. Here the buttons are not allowed to resize. They keep their original size.
package com.dummyscodes.learning;

import javax.swing.*;
import java.awt.*;

/**
 * Created by Kinath on 05/04/2015.
 */
public class BottomPanel extends JPanel {

    private JButton btn01 = new JButton();
    private JButton btn02 = new JButton();
    private JButton btn03 = new JButton();
    private JButton btn04 = new JButton();
    private JCheckBox checkBox01 = new JCheckBox();
    private JCheckBox checkBox02 = new JCheckBox();
    private GridBagLayout gridBagLayout = new GridBagLayout();

    public BottomPanel() {
        setupGUI();
    }

    private void setupGUI() {
        int width = this.getWidth();
        this.setBorder(BorderFactory.createTitledBorder("Bottom-Panel"));
        this.setPreferredSize(new Dimension(width,80));
        this.setMinimumSize(new Dimension(width,80));
        this.setMaximumSize(new Dimension(width,80));
        this.setLayout(gridBagLayout);

        Dimension btnDimension = new Dimension(100,25);
        btn01.setMaximumSize(btnDimension);
        btn02.setMaximumSize(btnDimension);
        btn03.setMaximumSize(btnDimension);
        btn04.setMaximumSize(btnDimension);

        btn01.setMinimumSize(btnDimension);
        btn02.setMinimumSize(btnDimension);
        btn03.setMinimumSize(btnDimension);
        btn04.setMinimumSize(btnDimension);

        btn01.setPreferredSize(btnDimension);
        btn02.setPreferredSize(btnDimension);
        btn03.setPreferredSize(btnDimension);
        btn04.setPreferredSize(btnDimension);

        btn01.setText("Button 01");
        btn02.setText("Button 02");
        btn03.setText("Button 03");
        btn04.setText("Button 04");

        checkBox01.setText("Checkbox 01");
        checkBox02.setText("Checkbox 02");

        this.add(btn01, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        this.add(btn02, new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        this.add(btn03, new GridBagConstraints(2, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        this.add(btn04, new GridBagConstraints(3, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));

        this.add(checkBox01, new GridBagConstraints(4, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        this.add(checkBox02, new GridBagConstraints(5, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
    }

}

MainPanel
MainPanel.java uses the GridBagLayout to position the panels according to the specified GridBagConstraints.
package com.dummyscodes.learning;

import javax.swing.*;
import java.awt.*;

/**
 * Created by Kinath on 05/04/2015.
 */
public class MainPanel extends JPanel {

    private LeftTopPanel leftTopPanel;
    private RightTopPanel rightTopPanel;
    private RightBottomPanel rightBottomPanel;
    private BottomPanel bottomPanel;
    private GridBagLayout gridBagLayoutMain;
    private JPanel topMainPanel;

    public MainPanel() {
        setupGui();
    }

    private void setupGui() {

        topMainPanel = new JPanel();
        gridBagLayoutMain = new GridBagLayout();
        leftTopPanel = new LeftTopPanel();
        rightBottomPanel = new RightBottomPanel();
        rightTopPanel = new RightTopPanel();
        bottomPanel = new BottomPanel();

        this.setBorder(BorderFactory.createTitledBorder("Main-Panel"));
        this.setLayout(gridBagLayoutMain);
        this.add(leftTopPanel, new GridBagConstraints(0, 0, 1, 2, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
        this.add(rightTopPanel, new GridBagConstraints(1, 0, 1, 1, 1, 1, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
        this.add(rightBottomPanel, new GridBagConstraints(1, 1, 1, 1, 1, 1, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
        this.add(bottomPanel, new GridBagConstraints(0, 2, 2, 1, 1, 0, GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    }

}


package com.dummyscodes.learning;

import javax.swing.*;
import java.awt.*;

/**
 * Created by Kinath on 02/04/2015.
 */
public class ApplicationFrame extends JFrame
{
    private static ApplicationFrame instance = null;
    private MainPanel mainPanel;
    private GridBagLayout gridBagLayoutAppFrame;

    private ApplicationFrame()
    {
        setupGUI();
    }

    public static ApplicationFrame getInsance()
    {
        if(instance == null)
        {
            instance = new ApplicationFrame();
        }
        return  instance;
    }

    public void setupGUI()
    {
        this.setTitle("Demo Application");
        this.setMinimumSize(new Dimension(800,600));
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        gridBagLayoutAppFrame = new GridBagLayout();
        mainPanel = new MainPanel();
        this.setLayout(gridBagLayoutAppFrame);
        this.getContentPane().add(mainPanel,new GridBagConstraints(0,0,1,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(10,10,10,10),0,0));
    }
}


package com.dummyscodes.learning;

/**
 * Created by Kinath on 02/04/2015.
 */
public class Main {

    public static void main(String[] args) {

        ApplicationFrame app = ApplicationFrame.getInsance();
        app.setVisible(true);
    }

}


That’s it.
Hope that helps.
Thanks.

Monday, April 6, 2015

A simple introduction to AOP

all and sundry: A simple introduction to AOP - Session 1: Why use AOP, a simple way to answer this question is to show an implementation of a cross cutting concern without using AOP. Consider a s...

Thursday, March 26, 2015

When to Use String/StringBuilder/String Buffer during Concatenation

When you want the String concatenation to be done at compile time, then you can use the normal '+' operator, otherwise String.concat() to do it. This is efficient at the compile time.

But when you are using the concatenation at the run time, you have to do it using StringBuilder or String Buffer. 

We have to identify the difference between these two. StringBuilder is NOT Synchronized and StringBuffer is Synchronized. Therefore when you want to work in a thread safe environment you have to use StringBuffer. Otherwise you have to use the StringBuilder. That would make the concatenation faster at runtime. 

References :