Blink your image in Java  

Posted by Fast Fire Fish in ,

Ever need to make an image blink continuously to signal for some kind of incoming activity? Well....I'm going to share a simple sample on how to do that.



import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;


public class BlinkingLabel extends JLabel implements Runnable{

private boolean show=false,blink=true;
private Icon iconImg;
private JLayeredPane parent;

public BlinkingLabel(Icon icon, JLayeredPane pane)
{
super(icon);
this.iconImg = icon;
this.parent = pane;

this.setVisible(true);
new Thread(this).start();

}

public void run() {
while (blink) {
show=!show;

if(show)
parent.moveToFront(this);
else
parent.moveToBack(this);

repaint();
try { Thread.sleep(400); } catch (Exception e) { }
}
}

public boolean isBlink() {
return blink;
}

public void setBlink(boolean blink) {
this.blink = blink;
}

}

The concept of this BlinkingLabel class is to pass in a secondary image along with another JLayeredPane object which is the main container for the original image. That is to say we are actually making use of the 2 images one of which shows the original state and the other is actually the lighted up state. Upon instantiating an object with this class, the secondary image will be toggled on and off with a variable of 400 milliseconds. You can tweak this setting if you wish it to be faster or slower.


Of course this is just one of the ways to make an image blink repeatedly. I'm sure there are other methods like overriding the paintComponent method or something.

If you've enjoyed this article, drop me a comment!


Related Posts by Categories



Bookmark this post:
StumpleUpon Ma.gnolia DiggIt! Del.icio.us Blinklist Yahoo Furl Technorati Simpy Spurl Reddit Google

This entry was posted on Monday, September 29, 2008 at Monday, September 29, 2008 and is filed under , . You can follow any responses to this entry through the comments feed .

9 comments

You could implement this in one of two ways -- either using one label per image, or using one custom component that paints both images. Because this lesson features painting, this section features the custom component approach.

December 23, 2009 at 10:13 PM

Hi! please help me know how to get the pixel values of a gray image in java. I really need to know as soon as possible. Thanks a lot!

January 4, 2010 at 6:58 PM

i want to scale down/up an image in my java project with the help of Affine Transformation.Can any one please help me? it is urgent.

January 5, 2010 at 2:39 AM

If we want to attach image in JAVA than all of we will leran the JAVA after we think that how to blink the image in JAVA.

January 28, 2010 at 5:01 PM

This is the constructor of the AwtImage class in which, the instance of the MediaTracker class has been created to add the image on the frame using the addImage method of the MediaTracker class. This constructor set the size, visibility and the close operation on the close button of the frame.

August 4, 2010 at 1:46 PM

i wish you could make a part two, maybe, or you know what!you should make the same top half of the poem, and it'll be like two different ways you could go.

August 6, 2010 at 2:13 PM

Stupid question here but; I have been noticing lately that i blink extremely often! I have heard people say it is normal to blink about 10 times in a minute, and I am certain I blink a lot more than that.

August 16, 2010 at 5:30 PM

I have heard people say it is normal to blink about 10 times in a minute, and I am certain I blink a lot more than that.

November 9, 2010 at 6:47 PM

Really very useful java coding for image blinking that denote any particular activity.Also helpful for java learner.Nice sharing..

July 18, 2011 at 12:38 AM

Post a Comment

Your Ad Here