Showing posts with label java. Show all posts

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!

Read More...


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

Create your own media player with Java Media Framework(JMF)  

Posted by Fast Fire Fish in ,

For those who are working with Java, you might be interested with Java's capability of playing media files with its Java Media Framework(JMF). Today I'm going to share a simple guide to get this working.

Steps

1)Get the installation software from Sun's website here.

2)You might need to restart your machine as one of my friends had this problem and it was resolved after that.

3)Copy and paste the code from here

4)If you do not have a sample mpeg file, you might want to download it here. Save the file to some location in your harddisk and remember where u save it.

5)Compile and launch the code with assuming you already have the standard jdk package installed. Select the mpeg file from the location where it is stored and it should start the video playing.

That's it! I hope I made it simpler if you are looking for a crash course.
For a more detailed explanation of the codes, I believe the source has already explained in great detail.

Read More...


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

Your Ad Here