Java 7 Released… (watch the video)

19 07 2011

Good and very Important News for all. That is Java 7 had released on 7th July 2011. Ceremony speech was given by Adam Messinger, vice president of development Fusion Middleware. According to dream team of Java , in the new version of Java has a big different comparing than the other versions of Java. And this is the first release of Java with Oracle company.And they has made it as MOVING JAVA FORWARD!

With Oracle, Java’s future  has goes with  the  new technology, the community, the Java Community Process (JCP), and the entire ecosystem focused on moving Java forward.

And they has categorised new Technical Breakout of Java 7

  1. Making Heads and Tails of Project Coin, Small Language Changes in JDK 7
  2. Divide and Conquer Parallelism with the Fork/Join Framework
  3. The New File System API in JDK 7
  4. A Renaissance VM: One Platform, Many Languages

Java 7 Released… from Gihan on Vimeo.

Just watch it. It’s worth to watch…

Thanks

Gihan Malan De Silva

Advertisements




Rotate an image with java Buffered image class..

14 07 2011

Hi, today I’m going to show you another image enhancing technique with Java’s Buffered image class. That is rotating an image. To rotate an image with Java we use classes called AffineTransform and AffineTransformOp. And let’s see how to rotate an image. It is very easy as previous things.

In this post I’m not going to describe basics like creating an buffered image, display a buffered image on a JLabel..  etc .. but to describe methods I wrote in my program.

1). public void rescale() {}

2). private void rorateImg() {}

Let’s consider one by one..

public void rescale() {

        tx = new AffineTransform();
        tx.rotate(angel,w / 2, h / 2);//(radian,arbit_X,arbit_Y)

        op = new AffineTransformOp(tx,AffineTransformOp.TYPE_BILINEAR);
        bufferedImage=op.filter(bufferedImage,null);//(sourse,destination)

       }//end rescale()

Before rotating the image...

In rescale() method I’ve created an AffineTransform object then called the rotate() method.

tx = new AffineTransform();

double theta, double anchorx, double anchory parameters should be passed into the rotate() method. This method rotates an image around an anchor point which coordinates equals to anchorx,anchory . And the theta should be give in radians. And this procedure equal to these three operations.
translate(anchorx, anchory);      // final translation
rotate(theta);                                 // rotate around anchor
translate(-anchorx, -anchory);   // translate anchor to origin

double angel=Math.PI/2;

This Math.PI/2 equals to 90 degrees.

   int w;// width of buffered image
   int h;//height of buffered image

then Buffered Image’s getWidth() and getHeight() methods assigns image’s width and height into w and h.

  w= bufferedImage.getWidth();
  h=bufferedImage.getHeight();

tx.rotate(angel,w / 2, h / 2);

By assigning w/2 and h/2 into anchorx and anchory I made the image to rotate around it’s center point. Then AffineTransformOp object is created. When creating the object an AffineTransform object and Bilinear interpolation type are given as parameters.

op = new AffineTransformOp(tx,AffineTransformOp.TYPE_BILINEAR);

Then the filter() method is called with AffineTransformOp object. This public final BufferedImage filter(BufferedImage src, BufferedImage dst) method is more likely the filter() in RescaleOp which we discussed. And this method compares source and destination images then returns the filtered buffered image. 

bufferedImage=op.filter(bufferedImage,null);

After rotating the image...

private void rorateImg(){

        w= bufferedImage.getWidth();
        h=bufferedImage.getHeight();

        rescale();             

        icon = new ImageIcon(bufferedImage);          
        picLabel.setIcon(icon);

    }//end rotateImg()

In here I earlier discussed what happens with w= bufferedImage.getWidth(); and h=bufferedImage.getHeight(); . Then rescale() method is called and the other codes describes the displaying the buffered image on a JLabel. 😀 😀 ok that all.

And here is related Java docs.

1) AffineTransform

2) AffineTransformOp

Here is the source code of it and if you want you can DOWNLOAD my Netbeans project. And it has two classes Rotate.java and Main.java which is included the main method.

Main.java

/*
 * Main.java
 */
package rotate;

/**
 *
 * @author gihan
 */
public class Main {
     public static void main(String[] args) {
        Rotate obj=new Rotate();
    }

}

Rotate.java

/*
 * Rotate.java
 */
package rotate;

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;

/**
 *
 * @author gihan
 */
public class Rotate extends JFrame{

    // globel variables

    BufferedImage bufferedImage;
    RescaleOp rescale;
    ImageIcon icon;
    AffineTransformOp op;
    AffineTransform tx;
    int w;// width of buffered image
    int h;//height of buffered image
    double angel=Math.PI/2; /** angle should be given in radian and
                                        * this program rotates the image in 90 degrees */
    String path=”/home/gihan/Pictures/nfs_bmw.jpg”;// image file path  
    JLabel picLabel=new JLabel();

    public Rotate(){

         JFrame jf=new JFrame();
         JPanel jp=new JPanel();        

         jf.add(jp);
         jp.add(picLabel);

            jf.setVisible(true);
            jf.setSize(487, 640);
            jf.setLocation(200,200);
            jf.setTitle(“Gihan’s Image Processing Test Area.. “);
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            File file = new File(path);

        try {           
            bufferedImage = ImageIO.read(file); // create a buffered image
        } catch (IOException ex) {
            Logger.getLogger(Rotate.class.getName()).log(Level.SEVERE, null, ex);
        }
            rorateImg();

         icon = new ImageIcon(bufferedImage);          
         picLabel.setIcon(icon);
    }

    private void rorateImg(){//rotates 90 degrees

        w= bufferedImage.getWidth();
        h=bufferedImage.getHeight();

        rescale();             

        icon = new ImageIcon(bufferedImage);          
        picLabel.setIcon(icon);

    }

    public void rescale() {

        tx = new AffineTransform();
        tx.rotate(angel,w / 2, h / 2);//(radian,arbit_X,arbit_Y)

        op = new AffineTransformOp(tx,AffineTransformOp.TYPE_BILINEAR);
        bufferedImage=op.filter(bufferedImage,null);//(sourse,destination)

       }

}//end class Rotate

 

If you have any doubt, feel free to ask. :D
Thank you

Gihan Malan De Silva @ gihansblog.wordpress.com





Contrast control with java Buffered image class..

11 07 2011

Hi, in this post I’m going to tell you how to change the contrast of an buffered image. And it will be very easy to you if you read my previous post on Brightness control with java Buffered image class... Here I’m going to explain only the key methods and others will be same as Brightness control.

In this program also here are two methods I’ve written.

1). public void rescale() {}

2). private void contrastChange() {}

Let’s consider them, one by one

An Image before change the Contrast...

        public void rescale() {

        rescale = new RescaleOp(scaleFactor,20.0f, null);
bufferedImage=rescale.filter(bufferedImage,null);//(sourse,destination)

       }//end rescale
In here, I’ve created RescaleOp object, and should pass parameters as in order sacleFactors, offsets,  hints .

By changing offsets value we can adjust the brightness of an image, and by changing sacleFactors value we can adjust contrast of an image and hints for the specified RenderingHints or can be kept as null. In my program scaleFactor is set to 1.0 in float for default image and for enhanced image it is set to 3.6 in float.

An Image after increasing the Contrast...

private void contrastChange(){

         rescale();
         icon = new ImageIcon(bufferedImage);          
         picLabel.setIcon(icon);
         System.out.println(“scaleFactor : “+scaleFactor  );

    }//end  contrastChange()

In the filter method filter(sourse_bufferedImage, destination_bufferedImage); should be given as like this, and it returns filtered buffered image. Then in the contrastChange() the rescale() method is called and as I mentioned in my previous post, the buffered image is printed on a JLabel. :D

Here is the source code of it and if you want you can DOWNLOAD my Netbeans project. And it has two classes Contrast.java and Main.java which is included the main method.

Main.java

/*

* Main.java

*/
package contrast;

/**
 *
 * @author gihan
 */
public class Main {
     public static void main(String[] args) {
        Contrast obj=new Contrast();
    }//end main()

}//end class Main

Contrast.java

/*
 * Contrast.java
 */
package contrast;

import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author gihan
 */
public class Contrast extends JFrame{
       //globel variables
        BufferedImage bufferedImage;
        String path=”/home/gihan/Pictures/nfs_bmw.jpg”;// image file path     
        float scaleFactor=3.6f;//change scaleFactor to change contrast        
        /*keep the value scaleFactor = 1.0f; as for a normal image*/
        RescaleOp rescale;
        ImageIcon icon;
        JLabel picLabel=new JLabel();

     public Contrast() {

         JFrame jf=new JFrame();
         JPanel jp=new JPanel();        

         jf.add(jp);
         jp.add(picLabel);

            jf.setVisible(true);
            jf.setSize(740, 487);
            jf.setLocation(200,100);
            jf.setTitle(“Gihan’s Image Processing Test Area.. “);
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            File file = new File(path);

        try {           
            bufferedImage = ImageIO.read(file); // create a buffered image
        } catch (IOException ex) {
            Logger.getLogger(Contrast.class.getName()).log(Level.SEVERE, null, ex);
        }

         contrastChange();   
         icon = new ImageIcon(bufferedImage);          
         picLabel.setIcon(icon);       

    }

     private void contrastChange(){

         rescale();
         icon = new ImageIcon(bufferedImage);          
         picLabel.setIcon(icon);
         System.out.println(“scaleFactor : “+scaleFactor  );

    }//contrastChange()

     public void rescale() {

        rescale = new RescaleOp(scaleFactor,20.0f, null);
        bufferedImage=rescale.filter(bufferedImage,null);//(sourse,destination)

       }//end rescale

}//end class contrast

If you have any doubt, feel free to ask. :D
Thank you

Gihan Malan De Silva @ gihansblog.wordpress.com





Brightness control with java Buffered image class..

10 07 2011

Hi, today I’m going to tell you how to change brightness of an image with Java’s Buffered image class. .. And consider this will be a series of posts regarding image processing/enhancement with Java  🙂 .

First we need to create an String variable to store the path of image file.

String path=”filepath_of_ your_image”;

for a example I used : String path=”/home/gihan/Pictures/nfs_bmw.jpg”;

Then create a file type object. And give our path as parameter.

File file = new File(path);

Then create a  buffered image.

BufferedImage bufferedImage = ImageIO.read(file);

If you want to, now you can preview your image using :

ImageIcon icon;
JLabel picLabel=new JLabel();

 icon = new ImageIcon(bufferedImage);          
 picLabel.setIcon(icon);

An Image before change the Brightness...

But we are going to process that image so let’s move to next point. There are two methods I’ve written in my programm.

1). public void rescale() {}

2). private void brighten() {}

Let’s consider them, one by one
     public void rescale() {

        rescale = new RescaleOp(1.0f,offset, null);
        bufferedImage=rescale.filter(bufferedImage,null);//(sourse,destination)

       }//end rescale
In here, I’ve created RescaleOp object, and should pass parameters as in order sacleFactors, offsetshints .

By changing offsets value we can adjust the brightness of an image, and by changing sacleFactors value we can adjust contrast of an image and hints for the specified RenderingHints or can be kept as null. In my program offset is set to 20 in float for default image and for enhanced image it is set to 170 in float.

An Image after change the Brightness...

Here you can look at the related java docs.

In the filter method filter(sourse_bufferedImage, destination_bufferedImage); should be given as like this, and it returns filtered buffered image.

Then in the private void brighten()  method I think there is nothing to describe. 😀 :D. I called the rescale() method and as I mentioned earlier, the buffered image is printed on a JLabel. 😀
private void brighten(){

         rescale();
         icon = new ImageIcon(bufferedImage);          
         picLabel.setIcon(icon);

    }//end  brighten()

Here is the source code of it and if you want you can DOWNLOAD my Netbeans project. And it has two classes Bright.java and Main.java which is included the main method.

Main.java
/*
 * Main.java
 */
package bright;

/**
 *
 * @author gihan
 */
public class Main {

    public static void main(String[] args) {
        Bright obj=new Bright();
    }//end main()

}//end class Main

Bright.java

/*
 * Bright.java
 */
package bright;

import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author gihan
 */
public class Bright extends JFrame{
       //globel variables
        BufferedImage bufferedImage;
        String path=”/home/gihan/Pictures/nfs_bmw.jpg”;// image file path         
        float offset = 170.0f;//change offset to brighten
        /*keep the value offset = 170.0f; as for a normal image*/
        RescaleOp rescale;
        ImageIcon icon;
        JLabel picLabel=new JLabel();

     public Bright() {

         JFrame jf=new JFrame();
         JPanel jp=new JPanel();        

         jf.add(jp);
         jp.add(picLabel);

            jf.setVisible(true);
            jf.setSize(550, 550);
            jf.setLocation(200,100);
            jf.setTitle(“Gihan’s Image Processing Test Area.. “);
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            File file = new File(path);

        try {           
            bufferedImage = ImageIO.read(file); // create a buffered image
        } catch (IOException ex) {
            Logger.getLogger(Bright.class.getName()).log(Level.SEVERE, null, ex);
        }

         brighten();   
         icon = new ImageIcon(bufferedImage);          
         picLabel.setIcon(icon);       

    }

     private void brighten(){

         rescale();
         icon = new ImageIcon(bufferedImage);          
         picLabel.setIcon(icon);
         System.out.println(“offset : “+offset  );

    }//end  brighten()

     public void rescale() {

        rescale = new RescaleOp(1.0f,offset, null);
        bufferedImage=rescale.filter(bufferedImage,null);//(sourse,destination)

       }//end rescale

}//end class Bright

If you have any doubt, feel free to ask. 😀
Thank you

Gihan Malan De Silva @ gihansblog.wordpress.com





How to change the theme of a java GUI Application (Java look and feel)

18 06 2011

Hi, today I’m going to show you how to change the appearance of a Java GUI application. In technically we call it as changing the look and feel of a Java GUI application. Swing components are responsible for changing the look and feel. In here “Look” refers to appearance of the GUI component and “Feel” refers to behave of the GUI component.

Java

Sun’s (now it’s Oracle) default JDK has provided these basic  LookAndFeel types..

  1. javax.swing.plaf.metal.MetalLookAndFeel
  2. com.sun.java.swing.plaf.windows.WindowsLookAndFeel
  3. com.sun.java.swing.plaf.motif.MotifLookAndFeel
  4. com.sun.java.swing.plaf.gtk.GTKLookAndFeel

When we developing  an GUI application using Java , it normally gives normal look and feel smiler to Linux OS (especially Knoppix-Linux  look and feel). Because by default it sets to Sun’s CrossPlatformLookAndFeel and also called “Metal look and feel“.And it’s part of the Java API (javax.swing.plaf.metal).

This code should be included in main method..

try {
                    UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());                

                    } catch (Exception e) {
                      System.err.println(“Look and feel not set.”);
                    }

for example..

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class gui {

    public static void main(String args[]){

                //———–Look and Feel————-
                try {
                    UIManager.setLookAndFeel(“com.sun.java.swing.plaf.motif.MotifLookAndFeel”);             

                    } catch (Exception e) {
                      System.err.println(“Look and feel not set.”);
                    }
                //———–Look and Feel————-
         gui myGui = new gui();
            myGui.launchFrame();
    }//end main()

But if you choose SystemLookAndFeel the application gives a look and feel like the running Operating system. Simply I say if you cshoose this, the look and feel will be depended on the Operating system.. To do that add following codes into your GUI application’s main method of the main class.

try {
                      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

                    } catch (Exception e) {
                      System.err.println(“Look and feel not set.”);
                    }
But if you choose WindowsLookAndFeel, the application gives a look and feel like the Windows Operating system.(But this will work only on Windows)

try {
                     UIManager.setLookAndFeel(“com.sun.java.swing.plaf.windows.WindowsLookAndFeel”);

                    } catch (Exception e) {
                      System.err.println(“Look and feel not set.”);
                    }

If you want to get Sun’s Motif Look and feel use this….

try {
                      UIManager.setLookAndFeel(“com.sun.java.swing.plaf.motif.MotifLookAndFeel”);

                    } catch (Exception e) {
                      System.err.println(“Look and feel not set.”);
                    } 

But in addition to that you may need an advanced look and feel.. for that you can use any commercial look and feels like Synthetica look and feel or Jtatoo.

with Syanthatica look and feel it look like this…

import de.javasoft.plaf.synthetica.SyntheticaBlackEyeLookAndFeel;

try {
UIManager.setLookAndFeel(new SyntheticaBlackEyeLookAndFeel());

} catch (Exception e) {
System.err.println(“Look and feel not set.”);
}

here I used sysnthatica’s Black eye look and feel. Other than the default look feela of Java, here you need to download and set the build path for some .jar file they provide.

To do that first you need to download Syanthatica’s synthatica.jar from this URL :http://www.jyloo.com/synthetica/download/

then, you need to download a theme for that :http://www.jyloo.com/synthetica/themes/

If you are using  Netbeans for java application developing put that both two .jar file into Libraries folder. If you are using Eclipse, put that two .jar files into JRE system libraries by right clicking and selecting “build path”–> “configure build path”.

Then Jtatoo is also a good commercial look and feel set. If you want to to try that, you need to download Jtatoo look and feel at:http://www.jtattoo.net/Download.html

There will be a file call Jtatoo.jar , so as mentioned above you need to put that into appropriate folder, then you can use it like this..

try {
                     UIManager.setLookAndFeel(“com.jtattoo.plaf.smart.SmartLookAndFeel”);

                    } catch (Exception e) {
                      System.err.println(“Look and feel not set.”);
                    }

In addition if you need an GUI application to test this you can use mine.

My java file:gui.java

My Eclipse project:My Code.zip

If you have any doubt, feel free to comment them.

Thanks

Gihan Malan De Silva