package hussam.gui;
import java.util.*;
import javax.swing.*;
import java.awt.*;
public class MyScrollingLabel extends JLabel{
	int x,y;
	int speed=100;
	FontMetrics metric;
	static int MAX_SPEED=100;
	public MyScrollingLabel(String text, int textSpeed){
		super(text);
		if(textSpeed<0)textSpeed=0;
		if(textSpeed>MAX_SPEED)textSpeed=MAX_SPEED;
		speed=textSpeed;
		
		Thread t=new Thread(new Runnable(){
			public void run(){
				while(true){
					x--;
					try {
						if(x+metric.stringWidth(getText())<=0)x=getWidth();
						Thread.sleep(MAX_SPEED-speed);
				    }
				    catch (Exception ex) {
				   // 	ex.printStackTrace();
				    }
					repaint();
				}
			}
		});
		t.start();
	}
	public void paint(Graphics g){
		metric=g.getFontMetrics();
		g.setColor(this.getForeground());
		g.setFont(getFont());
		g.drawString(getText(), x, (getHeight()+metric.getHeight())/2);
	}
	public static void main(String[]args)throws Exception{
		
 		JFrame f=new JFrame();
 		f.add(new MyScrollingLabel("This is a scrolling Text Label", 90));
 		f.pack();
 		f.setVisible(true);
 		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}