Java Tutorial – Create Icon Reflection
Posted by Haqqi in Programming Wednesday, 3 February 2010 20:51 View Comments
Just a simple Swing make over
Recently, I just wrote posts about my own world. For now, I will write about another Java Tutorial. Still in the progress of HQLibrary that is suspended, this is another simple class in my library. From the image you can see beside, you will understand that I will show you how to make a label that automatically create a reflection if we add an icon. More over, this class can be used in NetBeans GUI form, so you can add an icon using the parameter panel.
Before we go to create an extended class of JLabel, first we need to know how to automatically create a reflection of an image. Using Java 2D API, we can easily finish that problem. Those useful class packaged in java.awt package. We need to do simple 2D manipulation to create a new image that has reflection. So, I created some methods in another class (not in extended JLabel), with static modifier, so that it can be used by another class. I wrapped that methods in a class named HQIconUtil.
Here is the source code:
/**
* DO NOT REMOVE THIS LICENSE
*
* This source code is created by Muhammad Fauzil Haqqi.
* You can use and modify this source code freely but
* you are forbidden to change or remove this license.
*
* Nick : Haqqi
* YM : xp_guitarist
* Email : fauzil.haqqi@gmail.com
* Blog : http://www.fauzilhaqqi.net
*/
package hq.utility;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
/**
*
* @author Haqqi
*/
public final class HQIconUtil {
// *******variables area****** //
// *****constructors area***** //
private HQIconUtil() {}
// ********methods area******* //
public static BufferedImage convertToBufferedImage(Image image) {
// get image size
int width = image.getWidth(null);
int height = image.getHeight(null);
// create new buffered image
BufferedImage result = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = result.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
// return the result
return result;
}
public static BufferedImage
createReflectionBufferedImage(BufferedImage image) {
// create result image
BufferedImage result = new BufferedImage(image.getWidth(),
image.getHeight() * 6 / 4, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = result.createGraphics();
// paints original image
g.drawImage(image, 0, 0, null);
// paints mirrored image
g.scale(1.0, -1.0);
g.drawImage(image, 0, -image.getHeight() * 2, null);
g.scale(1.0, -1.0);
// move to mirror's origin
g.translate(0, image.getHeight());
// create gradien mask
GradientPaint mask = new GradientPaint(0, 0, new Color(1f, 1f, 1f, 0.5f),
0, image.getHeight() / 2, new Color(1f, 1f, 1f, 0f));
g.setPaint(mask);
// set alpha composite
g.setComposite(AlphaComposite.DstIn);
// paint the mask
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.dispose();
return result;
}
public static BufferedImage
createReflectionBufferedImage(Image image) {
return createReflectionBufferedImage(convertToBufferedImage(image));
}
}
Because we can only do image manipulation using BufferedImage, first of all we need to convert an Image into BufferedImage one. That is the use of the first method, convertToBufferedImage(Image image). After that, we parse it to createReflectionBufferedImage(BufferedImage image) method to get the new image with its reflection. The important thing to understand this method is by studying about masking and compositing of Java 2D. The idea here is to composite destination image with a gradient.
After that, we can create our new class for label. We can easily create it by extending JLabel class of javax.swing package. I use NetBeans to create this class, and others too. So, I just right click and choose to add a new property that saves the reflection icon variable.
Don’t forget to generate setter and getter method, so that it can be used using GUI parameter panel in NetBeans. I named my class as HQLabel, since it will be included in my HQLibrary.
Here is the source code of HQLabel:
/**
* DO NOT REMOVE THIS LICENSE
*
* This source code is created by Muhammad Fauzil Haqqi.
* You can use and modify this source code freely but
* you are forbidden to change or remove this license.
*
* Nick : Haqqi
* YM : xp_guitarist
* Email : fauzil.haqqi@gmail.com
* Blog : http://www.fauzilhaqqi.net
*/
package hq.widget;
import hq.utility.HQIconUtil;
import java.awt.image.BufferedImage;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;
/**
*
* @author Haqqi
*/
public class HQLabel extends javax.swing.JLabel {
private static final long serialVersionUID = 1L;
// *******variables area****** //
private Icon iconReflect;
// *****constructors area***** //
/**
* Default constructor.
* Set label's text to the bottom center.
*/
public HQLabel() {
super();
setHorizontalTextPosition(SwingConstants.CENTER);
setVerticalTextPosition(SwingConstants.BOTTOM);
}
// ********methods area******* //
/**
* Get icon reflection.
* @return iconReflect - Icon reflection
*/
public Icon getIconReflect() {
return this.iconReflect;
}
/**
* Set the icon reflection. Current icon will be
* replaced by the icon reflection.
* @param iconReflect
*/
public void setIconReflect(Icon iconReflect)
throws IllegalArgumentException {
// check whether the argument is null
if (iconReflect == null) {
firePropertyChange("iconReflection", getIconReflect(), iconReflect);
this.iconReflect = iconReflect;
setIcon(iconReflect);
} else {
if (!(iconReflect instanceof ImageIcon)) {
throw new IllegalArgumentException();
}
firePropertyChange("iconReflection", getIconReflect(), iconReflect);
this.iconReflect = iconReflect;
BufferedImage img = HQIconUtil.createReflectionBufferedImage(((ImageIcon) iconReflect).getImage());
setIcon(new ImageIcon(img));
}
}
}
Ok, those all the way to create a label class that has an image reflection. If you create it correctly, you will see a new parameter in NetBeans parameter editor panel. You can test it by building it and then adding it to a GUI form such as JPanel in NetBeans. You can do that by simply dragging from the file panel. Happy coding…
-
Lukman




