Results 1 to 18 of 18
  1. #1
    Bauss is offline Member
    Join Date
    Aug 2013
    Posts
    8
    Rep Power
    0

    Question JButtons Do Not Change Color

    Hello,

    I am making a game like simon. Buttons light up and you repeat the pattern.
    Lighting up the buttons is key, and is not working for me.
    On the first level (Only 1 button to push), the button lights up but from there on out nothing happens.
    Here is the code that lights it up:

    Java Code:
    public void flashButton(JButton button){
    button.setBackground(Color.white);
    	refresh();
    	try {
    		Thread.currentThread().sleep(400);
    	} catch (InterruptedException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
    	if(button==button1)
    		button.setBackground(Color.green);
    	else if(button==button2)
    	button.setBackground(Color.red);
    	else if (button==button3)
    	button.setBackground(Color.blue);
    	else if (button==button4)
    	button.setBackground(Color.yellow);
    	refresh();
    	
    }
    In the console it shows the program is moving a lot and from printing to the console the buttons that need to be pushed I can advance in levels.
    I really need some help because I need to finish very soon.
    I will be on this site all day so feel free to ask questions.

    Here is the whole program:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.GridLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Random;
    
    import javax.swing.*;
    
    public class MyFrame extends JFrame implements ActionListener{
    
    	int frameRate = 200;
    	int guess = 1;
    	int level = 1;
    	int endSplash = 1;
    	int g = 1;
    
    	boolean canClick = false;
    
    	ArrayList<JButton> guesses = new ArrayList<JButton>();
    	ArrayList<JButton> cpu = new ArrayList<JButton>(); //the next button is NOT being added so that the cpu.guess will equal theres
    	ArrayList<JButton> cpuChoices = new ArrayList<JButton>();
    
    	Queue<JButton> cpuMoves = new LinkedList<JButton>();
    	public JButton button1;
    	public JButton button2;
    	public JButton button3;
    	public JButton button4;
    	public JButton start;
    
    
    
    	boolean isTrue = true;
    	boolean switchColor = true;
    	boolean endThread = true;
    	MyFrame(){
    		super("Simon");
    		initialize();
    
    		this.setSize(400, 400);
    		this.setLocationRelativeTo(null);
    		this.setVisible(true);
    
    		try {
    			run();
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    
    	}
    
    	private void initialize() {
    		// TODO Auto-generated method stub
    
    
    		JPanel squarePane = new JPanel(new GridLayout(2, 2));
    		JPanel controlPane = new JPanel(new BorderLayout());
    		start = new JButton("Start");
    		button1 = new JButton();
    		button2 = new JButton();
    		button3 = new JButton();
    		button4 = new JButton();
    		squarePane.add(button1);
    		squarePane.add(button2);
    		squarePane.add(button3);
    		squarePane.add(button4);
    
    		cpuChoices.add(button1);
    		cpuChoices.add(button2);
    		cpuChoices.add(button3);
    		cpuChoices.add(button4);
    
    		button1.addActionListener(this);
    		button2.addActionListener(this);
    		button3.addActionListener(this);
    		button4.addActionListener(this);
    
    		controlPane.add(squarePane);
    
    
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setLayout(new BorderLayout());
    		this.add(controlPane);
    		this.pack();
    		this.setLocationRelativeTo(null);
    		this.setVisible(true);
    
    
    
    	}
    	public void splash(){
    		while(isTrue){
    			switch(g){
    			case 1:
    
    				button1.setBackground(Color.green);
    				button2.setBackground(Color.red);
    				button4.setBackground(Color.yellow);
    				button3.setBackground(Color.blue);
    
    				if(!isTrue){
    					return;
    				}
    
    				try {
    					Thread.sleep(frameRate);
    				} catch (InterruptedException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    				if(endSplash==4){
    					return;
    				}
    
    				break;
    			case 2:
    				button2.setBackground(Color.green);
    				button4.setBackground(Color.red);
    				button3.setBackground(Color.yellow);
    				button1.setBackground(Color.blue);
    
    
    				try {
    					Thread.sleep(frameRate);
    				} catch (InterruptedException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    				break;
    			case 3:
    				button4.setBackground(Color.green);
    				button3.setBackground(Color.red);
    				button1.setBackground(Color.yellow);
    				button2.setBackground(Color.blue);
    
    
    				try {
    					Thread.sleep(frameRate);
    				} catch (InterruptedException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    				break;
    			case 4:
    				button3.setBackground(Color.green);
    				button1.setBackground(Color.red);
    				button2.setBackground(Color.yellow);
    				button4.setBackground(Color.blue);
    
    
    				try {
    					Thread.sleep(frameRate);
    				} catch (InterruptedException e) {
    					// TODO Auto-generated catch block
    					e
    					.printStackTrace();
    				}
    				g=0;
    
    				endSplash++;
    
    				break;
    
    			}
    			g++;
    		}
    		refresh();
    
    		switchColor = true;
    	}
    
    
    
    	private void run() throws InterruptedException {
    		// TODO Auto-generated method stub
    		splash();
    		flashButtons();
    		try {
    			Thread.sleep(250);
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		flashButtons();
    		Thread.sleep(500);
    		runLevel();
    	}
    
    
    
    public void flashButton(JButton button){
    button.setBackground(Color.white);
    	refresh();
    	try {
    		Thread.currentThread().sleep(400);
    	} catch (InterruptedException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
    	if(button==button1)
    		button.setBackground(Color.green);
    	else if(button==button2)
    	button.setBackground(Color.red);
    	else if (button==button3)
    	button.setBackground(Color.blue);
    	else if (button==button4)
    	button.setBackground(Color.yellow);
    	refresh();
    	
    }
    	private void runLevel() {
    		// TODO Auto-generated method stub
    		System.out.println("Level"+ level);
    		for(int i=0;i<level;i++){
    			Random rand = new Random();
    			int num = rand.nextInt(4);
    			System.out.println(num);
    			cpu.add(cpuChoices.get(num));
    			flashButton(cpuChoices.get(num));
    		}
    		
    			transferData();
    			cpu.clear();
    	}
    	private void transferData() {
    		// TODO Auto-generated method stub
    		for(int i=0;i<cpu.size();i++){
    			cpuMoves.offer(cpu.get(i));
    		}
    	}
    	
    	public void refresh(){
    		this.invalidate();
    		this.validate();
    		this.repaint();
    		button1.invalidate();
    		button1.validate();
    		button1.repaint();
    		button2.invalidate();
    		button2.validate();
    		button2.repaint();
    		button3.invalidate();
    		button3.validate();
    		button3.repaint();
    		button4.invalidate();
    		button4.validate();
    		button4.repaint();
    	}
    
    	public void flashButtons(){
    		button1.setBackground(Color.white);
    		button2.setBackground(Color.white);
    		button3.setBackground(Color.white);
    		button4.setBackground(Color.white);
    		try {
    			Thread.sleep(250);
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		button1.setBackground(Color.green);
    		button2.setBackground(Color.red);
    		button3.setBackground(Color.blue);
    		button4.setBackground(Color.yellow);
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		if(e.getSource() == button1){
    			this.checkGuess(button1);
    		} else if(e.getSource()==button2){
    			this.checkGuess(button2);
    		} else if(e.getSource()==button3){
    			this.checkGuess(button3);
    		} else if(e.getSource()==button4){
    			this.checkGuess(button4);
    		}
    		
    	}
    	public void checkGuess(JButton b){
    		//cpuMoves.add(button1);
    				if(cpuMoves.peek()!=b){
    					button1.setBackground(Color.black);
    					button2.setBackground(Color.black);
    					button3.setBackground(Color.black);
    					button4.setBackground(Color.black);
    					int reply = JOptionPane.showConfirmDialog(null, "You lost, run again?", "You are a Loser", JOptionPane.YES_NO_OPTION);
    					if(reply==0){
    						this.splash();
    					} else {
    						this.setVisible(false);
    						this.dispose();
    					}
    				}
    				
    				cpuMoves.poll();
    				if(cpuMoves.isEmpty()){
    					level++;
    					try {
    						Thread.sleep(250);
    					} catch (InterruptedException e1) {
    						// TODO Auto-generated catch block
    						e1.printStackTrace();
    					}
    					runLevel();
    				}
    				guess++;
    				System.out.println(cpu.size());
    			
    		
    	}
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: JButtons Do Not Change Color

    At a quick glance it looks to me like you are doing everything on the EDT.
    You want to be using (I expect) a SwingTimer for the parts where the computer is showing the sequence.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Bauss is offline Member
    Join Date
    Aug 2013
    Posts
    8
    Rep Power
    0

    Default Re: JButtons Do Not Change Color

    That's what I was thinking but I tried and it did not work. Could you help with another post or a PM?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: JButtons Do Not Change Color

    Darryl beat me to it.
    Those links cover everything you need to know about this, and anyone here would simply be repeating what they say.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Bauss is offline Member
    Join Date
    Aug 2013
    Posts
    8
    Rep Power
    0

    Default Re: JButtons Do Not Change Color

    So I have read and understand but where are some places I could use it where it would fix the color not changing?

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,090
    Rep Power
    6

    Default Re: JButtons Do Not Change Color

    In around 10 minutes time!? You must be an information absorbing machine!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    Bauss is offline Member
    Join Date
    Aug 2013
    Posts
    8
    Rep Power
    0

    Default Re: JButtons Do Not Change Color

    Quote Originally Posted by gimbal2 View Post
    In around 10 minutes time!? You must be an information absorbing machine!
    Well I was looking at those same ones earlier today.

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,090
    Rep Power
    6

    Default Re: JButtons Do Not Change Color

    Fair enough. But then...

    That's what I was thinking but I tried and it did not work.
    You'll have to share the code you produced that "did not work" so you can be helped further.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    Bauss is offline Member
    Join Date
    Aug 2013
    Posts
    8
    Rep Power
    0

    Default Re: JButtons Do Not Change Color

    Quote Originally Posted by gimbal2 View Post
    Fair enough. But then...



    You'll have to share the code you produced that "did not work" so you can be helped further.
    I included all the code in the OP

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: JButtons Do Not Change Color

    Quote Originally Posted by Bauss View Post
    I included all the code in the OP
    That code does not include any Timers.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,090
    Rep Power
    6

    Default Re: JButtons Do Not Change Color

    As Tolls says, that code doesn't use a Timer yet. People are telling you that you should use one, give you excellent and clear resources to read so you can learn how. You say you understand, you even say you already tried it but it didn't work. Here I'll quote you:

    You want to be using (I expect) a SwingTimer for the parts where the computer is showing the sequence.
    That's what I was thinking but I tried and it did not work. <--- see, you said that
    So show us. What did you try way back then that didn't work and you did not include in the above code.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    Bauss is offline Member
    Join Date
    Aug 2013
    Posts
    8
    Rep Power
    0

    Default Re: JButtons Do Not Change Color

    Oh, sorry, a missunderstanding. Let me write the timer again. I thought you meant the origonal code.

    EDIT: turns out the timer works but it does not change the color back. If that is fixed, the whole program is done!
    Any help here?

    Here is non-working with timers:

    Java Code:
    public void flashButton(JButton button){
    final JButton standIn = button;
    ActionListener taskPerformer = new ActionListener() {
    	public void actionPerformed(ActionEvent evt) {
    		standIn.setBackground(Color.white);
    //((Timer)evt.getSource()).stop(); //To get the source of the timer
    //myTimer.stop(); //Or use it's name
    }
    };
    
    Timer myTimer = new Timer(400,taskPerformer);
    myTimer.setRepeats(false); //This is so we don't loop.
    
    myTimer.start(); //To start the timer.
    	if(button==button1)
    		button.setBackground(Color.green);
    	else if(button==button2)
    	button.setBackground(Color.red);
    	else if (button==button3)
    	button.setBackground(Color.blue);
    	else if (button==button4)
    	button.setBackground(Color.yellow);
    	refresh();
    	
    }
    Last edited by Bauss; 08-08-2013 at 05:31 PM. Reason: Fixed the closing code tag

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: JButtons Do Not Change Color

    @Bauss: the closing code tag is [/CODE] (includes a slash)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  15. #15
    Bauss is offline Member
    Join Date
    Aug 2013
    Posts
    8
    Rep Power
    0

    Default Re: JButtons Do Not Change Color

    Quote Originally Posted by DarrylBurke View Post
    @Bauss: the closing code tag is [/CODE] (includes a slash)

    db
    Changed but does not look different. Any insight into whats happening?

  16. #16
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,090
    Rep Power
    6

    Default Re: JButtons Do Not Change Color

    well you also changed the opening tag...
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  17. #17
    Bauss is offline Member
    Join Date
    Aug 2013
    Posts
    8
    Rep Power
    0

    Default Re: JButtons Do Not Change Color

    My bad, changed it.

  18. #18
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,090
    Rep Power
    6

    Default Re: JButtons Do Not Change Color

    Well note how you call refresh() when you first change the color to green/red/blue/yellow, but you don't call refresh() when you switch it back to white in the timer action.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. change color
    By faraa in forum New To Java
    Replies: 3
    Last Post: 05-19-2013, 05:22 PM
  2. JButtons lose color afteryou mouse over them
    By nhmllr in forum AWT / Swing
    Replies: 3
    Last Post: 03-08-2013, 12:29 PM
  3. Replies: 2
    Last Post: 07-16-2012, 04:08 AM
  4. Replies: 4
    Last Post: 12-31-2010, 12:15 PM
  5. how to change the appearance of jbuttons
    By katie in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 10:26 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •