Help for my background color changer Java program
Posted: Tue Mar 06, 2012 10:05 am
				
				Hi friends.
With my simple Java knowledge I made a program yesterday and it didn't work as expected. So I wrote it again today and posting here to get helps from you guys
 
In this program I tried to make a program to change background of a window according to seconds, Like this. After 1 second background is blue then red and etc... I wrote this to gain my knowledge so even changing to 5 colors is enough.
 
So I searched the net and and found there is a class for this in the API called Timer. And I found out there are 2 classes in Java that has same name, Here are they,
java.util.Timer
and
javax.swing.Timer
So for my task I found out that second one is the one I need. So I used it. Because I know the other stuff what I can't figure out is the timer. Finally I found that too
  So I made a program by using that. And using API I learned how to use it's functions and of course how to use the class itself too  
 
But my problem is, I could only write the program only to change to one color after 1 second then I couldn't figure out how to change the color according to seconds.
Program only works 1 time then stay as is, Of course you can see that I haven't written the other stuff to do the rest, I did so because I couldn't figure out it
  
So here goes the code,
Can anyone help me with this? This is just written for fun to gain my knowledge  
 
Thanks
			With my simple Java knowledge I made a program yesterday and it didn't work as expected. So I wrote it again today and posting here to get helps from you guys
In this program I tried to make a program to change background of a window according to seconds, Like this. After 1 second background is blue then red and etc... I wrote this to gain my knowledge so even changing to 5 colors is enough.
So I searched the net and and found there is a class for this in the API called Timer. And I found out there are 2 classes in Java that has same name, Here are they,
java.util.Timer
and
javax.swing.Timer
So for my task I found out that second one is the one I need. So I used it. Because I know the other stuff what I can't figure out is the timer. Finally I found that too
But my problem is, I could only write the program only to change to one color after 1 second then I couldn't figure out how to change the color according to seconds.
Program only works 1 time then stay as is, Of course you can see that I haven't written the other stuff to do the rest, I did so because I couldn't figure out it
So here goes the code,
Code: Select all
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.Timer;
public class BGChanger extends JFrame{
	private int delay = 1000;
	public BGChanger(){
		super("Background Changer");
		setSize(300,300);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		
		
		
		ActionListener ac = new ActionListener(){
						public void actionPerformed(ActionEvent e){
										
							getContentPane().setBackground(Color.BLUE);
							} 
						};
										
		Timer timer = new Timer(delay,ac);
		
		timer.start();
		}
		
		
		
		public static void main(String args[]){
		
			BGChanger gui = new BGChanger();
		
		}	
		
}
Thanks