Help for my background color changer Java program

Java programming topics
Post Reply
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Help for my background color changer Java program

Post by Nipuna » 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,

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();
		
		}	
		
}
Can anyone help me with this? This is just written for fun to gain my knowledge :)





Thanks
User avatar
SevenZero
Major
Major
Posts: 263
Joined: Sun Nov 01, 2009 8:37 pm

Re: Help for my background color changer Java program

Post by SevenZero » Tue Mar 06, 2012 8:32 pm

I'll give you a tip on how to do it.

Define a variable say int curColour and set it to 0 at the constructor. Delete the line getContentPane().setBackground(Color.BLUE); and add something like below with function actionPerformed.

Code: Select all

switch (curColour){
	case 0: //Blue 
		getContentPane().setBackground(Color.BLUE);
		break;
	case 1: //Red
		getContentPane().setBackground(Color.RED);
		break;
	.......
	.......
	.......
	case n: //XXXX  Last colour you need
		getContentPane().setBackground(Color.XXXX);
		break;
}

if (curColour == n){ // We reached last colour -> start from beginning  (replace n with the number for your last colour)
	curColour  = 0;
}
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: Help for my background color changer Java program

Post by Nipuna » Wed Mar 07, 2012 7:37 am

Hi

Thanks for the reply

But it is working same as before not changing according to seconds. after 1st second it become blue and then it doesn't change :?

Here is the new 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;
		  int curColour;
 
        public BGChanger(){
                super("Background Changer");
                setSize(300,300);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setVisible(true);
               
                curColour = 0;
               
                ActionListener ac = new ActionListener(){
                                                public void actionPerformed(ActionEvent e){
                                                         
								switch (curColour){
       									case 0: 
          									getContentPane().setBackground(Color.BLUE);
          								break;
       									case 1: 
          									getContentPane().setBackground(Color.RED);
          								break;
			 
       									case 3: 
          									getContentPane().setBackground(Color.WHITE);
          								break;
    								}

    								if (curColour == 3){
       									curColour  = 0;
    								}                      
                                                        
                                                }
					};
                                                                               
                Timer timer = new Timer(delay,ac);
               
                timer.start();
					 
 
                }
               
               
               
                public static void main(String args[]){
               
                        BGChanger gui = new BGChanger();
               
                }      
               
}
Since we get those ugly marks in the syntax highlighting so I am attaching my code too. :) Since Expertcore doesn't let to attach .java files. I added a .zip file and inside that there is the .java file. ;)
BGChanger.zip
(628 Bytes) Downloaded 281 times
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Re: Help for my background color changer Java program

Post by Saman » Wed Mar 07, 2012 12:26 pm

Put timer.start(); again as the last line of function actionPerformed.
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: Help for my background color changer Java program

Post by Nipuna » Wed Mar 07, 2012 1:42 pm

Yeah :yahoo: I did few adjustments and got it to work.

I added timer.start() again but it didn't work so I did few changes and then it worked.

Thanks friends.

Here goes the new 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;
	private int curColour;
        private Timer timer;
 
        public BGChanger(){
                super("Background Changer");
                setSize(300,300);

                curColour = 0;
                                 
                
                Handler ac = new Handler();
                
                timer = new Timer(delay,ac);
               
                timer.start();
                }
               
               
               
                public static void main(String args[]){
               
                        BGChanger gui = new BGChanger();
                        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        gui.setVisible(true);
               
                }      
               
                
                
       private class Handler implements ActionListener {
                    
                
          public void actionPerformed(ActionEvent e){
                                                         
              switch (curColour){
                  case 0: 
                      getContentPane().setBackground(Color.BLUE);
                      curColour++;
                  break;
                                
                  case 1: 
                      getContentPane().setBackground(Color.RED);
                      curColour++;
                  break;
			 
                  case 2: 
                       getContentPane().setBackground(Color.WHITE);
                       curColour++;
                  break;
                        }

               if (curColour == 3)
                   curColour  = 0;
                       
                
          }
     }
}
Post Reply

Return to “Java Programming”