1

I'm trying to create a little button game for a school project. What the game is, is there are a couple buttons that every like 3 seconds will flash a color then if you press it when the color is up, it stays that color.

I have all the buttons created and they display just fine. I just need help with the actual handling code.

  • What I'm getting stuck on

    timer = new Timer(length, jButton1ActionPerformed);
    timer.setInitialDelay(pause);
    
    
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {  
    
        jButton1.setBackground(Color.blue);
    
    } 
    

Now this isn't my actual game, I'm just trying to get the hang of the Swing Timers

Now that I have the button changing colors this is the code that I tried to get it to stay that color when clicked.

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 

       if(jButton1.getBackground().equals(Color.blue){
          jButton1.setBackground(Color.blue);
          timer.stop();  
       }

    }  
Jonah
  • 1,013
  • 15
  • 25
  • 2
    This isn't really a 'write my app for me' site - what specific code are you having trouble with? Can you post an example? – Ideasthete Sep 23 '14 at 15:35
  • I completely know that. As you can see, I said I appreciated Hovercraft Full Of Eels for NOT giving me the code. I probably should have stated I want a nudge in the right direction and not just the code given to me. It's my fault. I'll be more clear next time – Jonah Sep 23 '14 at 16:41
  • No worries. It's just hard to answer questions that would require a lot of code-writing. Narrowing down the problem helps get to an answer more quickly. Looks like you got what you needed though! – Ideasthete Sep 23 '14 at 17:06

1 Answers1

2

You will want to use a Swing Timer to handle your animation. In the Timer's ActionListener, you would have code that randomly selects a button (the Random class can help here) and changes its color, possibly via setForground(...), or even by using ImageIcons and swapping icons via setIcon(...). The JButton's ActionListener can then check the button's icon or foreground color and act accordingly.

Since this is a school project, I'm not posting a code solution but will add some links that should help:


To create an ActionListener for your Timer, do just that -- create one inline:

ActionListener timerListener = new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
     // code to be performed every xxx mSec goes here
  }
}
int timerDelay = 3 * 1000; // or whatever length of time needed
Timer timer = new Timer(timerDelay, timerListener);

// later on in the block where you want to start your Timer
timer.start();
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 2
    Thank you very much. I will look into the Swing timer and give it a try. Thanks for the nudge in the right direction instead of actually just posting the code too. :) – Jonah Sep 23 '14 at 15:38
  • @Jonah: you're welcome. As always, try to break the big problem into small steps, and then solve each small step one at a time. If you get stuck later, please come back, and do show your code. – Hovercraft Full Of Eels Sep 23 '14 at 15:39
  • I will definitely come back if I have more questions. I need to find better ways to break down problems. Kinda sloppy at the moment. Thanks for the advice! – Jonah Sep 23 '14 at 15:44
  • So as I'm using Netbeans, you can just easily have it create it's own action listener I did that but I'm having trouble connecting the action listener to the timer. I'll post my code – Jonah Sep 23 '14 at 16:35
  • @Jonah: check the timer tutorial link that I've provided. Just create your own ActionListener inline and use it. See edit to answer. – Hovercraft Full Of Eels Sep 23 '14 at 19:00
  • Thanks for the more in-depth explanation. I was kinda having an "off" day yesterday. I got it up and working. Really Appreciate your time and effort. :) – Jonah Sep 24 '14 at 15:21
  • @Jonah: you're welcome, and very glad that you've got things moving forward! – Hovercraft Full Of Eels Sep 24 '14 at 16:58
  • I hope you don't mind me asking another question but I'm having some problems making it so when you click the button it stays that color. I'll edit my post once again. – Jonah Sep 24 '14 at 19:10