Wednesday, December 22, 2010

Rotate and translate images with AffineTransform

Staying on the same subject as my previous posts, the next example will help you rotate and translate any BufferedImage.

public BufferedImage rotateImage(BufferedImage rotateImage) {
AffineTransformOp op = null;

try {

   AffineTransform tx = new AffineTransform();

   //Rotate 90ยบ
   tx.rotate(Math.toRadians(90), rotateImage.getWidth()
              / 2.0, rotateImage.getHeight() / 2.0);

   AffineTransform translationTransform;
   translationTransform = findTranslation(tx, rotateImage);

   tx.preConcatenate(translationTransform);

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

   } catch (Exception e) {
      //Do something here
   }

   return op.filter(rotateImage, null);
}

Thursday, December 16, 2010

How to convert images

javax.imageio.ImageIO let's you convert and save images from one format into another.It works using plug-in modules that handle various formats, including gif, png, bmp, jpg and others. We can use getWriterFormatNames() to find out all supported types.

String[] types = ImageIO.getWriterFormatNames();

  for (String name: types){
    System.out.println(name);
  }


Next, let's load our image into a BufferedImage which is a subclass of Image class:
File inputFile = new File("1.bmp");

  BufferedImage image = ImageIO.read(inputFile);

Finally convert the image:
File outputFile = new File("1.jpg");

  ImageIO.write(image, "jpg", outputFile);

In this example, the image file bmp was converted into a jpg image file.

Wednesday, December 15, 2010

How to resize Buffered Images

When we need to resize or scale images in Java, the Java2D Team has been encouraging developers to move away from Image.getScaledInstance() to more updated APIs. This is exactly what I tried to do. So, if you are in need of a fairly good function that resizes images, look no further :)

private BufferedImage resizedImage(BufferedImage oriImage) {

    int type = oriImage.getType() == 0
            ? BufferedImage.TYPE_INT_ARGB : oriImage.getType();

    BufferedImage scaledImage = new BufferedImage((int) scaleWidth,
            (int) scaleHeight, type);

    // Paint scaled version of image to new image

    Graphics2D graphics2D = scaledImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BILINEAR);

    graphics2D.drawImage(oriImage, 0, 0, (int) scaleWidth,
            (int) scaleHeight, null);

    // Clean up
    graphics2D.dispose();

//Return scaled image
 return scaledImage;

}


Aside note: Some of my co-workers asked me about AffineTransform.scale() when performing scaled functions. Well, there is nothing wrong in using AffineTransform.Here's an example:
 AffineTransform xform = AffineTransform.getScaleInstance(2, 2);
 graphics2D.drawImage(img, xform, null);

But in a majority of cases, there is a better approach. Simply use the scaling variant of Graphics.drawImage()

 graphics2D.drawImage(oriImage, 0, 0, oriImage.getWidth()*2,
            oriImage.getHeight()*2, null);

Friday, December 10, 2010

Reading a file into a Byte Array

To read a file (image file) into a Byte Array all we need to is copy the next function into your source code.

I tweaked it to fit my needs, however it is not entirely mine. I found it by goggling a while ago (don't remember when) and most of the credits will have to go to the creator (sorry... don't know who you are either, because the page where I found this function didn't have anything regarding the creator).

Tuesday, December 7, 2010

JComboBox Text Alignment

Today someone asked me how can we make the text in a JComboBox align to the right instead of the left? Here's your answer :)

DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
 //DefaultListCellRenderer.LEFT;
 //DefaultListCellRenderer.CENTER;
 //DefaultListCellRenderer.RIGHT;
 dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
 jComboBox1.setRenderer(dlcr);

Import this,
import javax.swing.DefaultListCellRenderer;
and your done.


Monday, December 6, 2010

How to set an Application Icon in Swing

To set an application icon simply paste this code in you application:

InputStream icon = this.getClass().getResourceAsStream("MyIcon.gif");
if (icon != null) {
   BufferedImage image = ImageIO.read(icon);
   setIconImage(image);
}

Swing components, such as labels and buttons can be decorated with an icon by using the Icon interface: IconImage.

Thursday, December 2, 2010

Oracle and Apple Announce OpenJDK Project for Mac OS X

This may be old news, but apparently Oracle and Apple announced the OpenJDK project for Mac OS X.
“We are excited to welcome Apple as a significant contributor in the growing OpenJDK community,” said Hasan Rizvi, Oracle’s senior vice president of Development. Personally I'm more eager to see how Oracle, Apple and the JAVA community will handle the issue of Apple's use of private API in their JAVA development.

Let's just hope that Apple's implementation doesn't end up like Microsoft's :(

For more information on JAVA and on the subject, visit: