• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JButtons fail to change

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, I'm writing a program that has buttons that flash between various colors depending on a random number generated. Unfortunately, the buttons refuse to flash between the two colors. As a test, I made a JOptionPane pop up after I set the background to black and the color change occured. This made me suspect that the problem was a failure to call repaint but after adding that to my code, they still refused to flash. Can anyone explain this failure to flash?


colors is an ArrayList of Colors.
choices[j] is an array of four colors, red, yellow, orange and blue.
Thanks a lot!

 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the Swing forum.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would just like to make a comment about your design here.

You shouldn't have so much duplicate code. That method could easily be flattened into the size of one of your switch cases. How about putting your "up" "down" "left" "right" into an array, and then using array[j].setBackground(...)... At the very least, make a method that takes the specific button as a parameter.
 
Darin Niard
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now to your question...

What exactly is 'colors' if I may ask?

Edit: If you want the colors to constantly flash, you're going to need another thread to do it. Something like this:


[ July 09, 2004: Message edited by: Darin Niard ]
 
Matthew Fyffe
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
aye, I was sure the efficiency wouldn't be the greatest. A friend of mine asked me to write a quick game for him so that he could show me how a network would work. I would write the game and he would write a side class that took the high score, compared it with other scores on a server and sent back the highest 5. So the game I wrote is essentially the electronic toy Simon. When the user clicks a JButton titled begin, the program generates a series of flashes of the JButtons. so depending on what is randomly generated, either the top, right, left, or bottom button will flash. Colors is the series of randomly generated directions. I don't want the buttons to continually flash, but rather to do it once when it reaches that point of code. I put a JOptionPane between the setBackground() methods which made the change in color visible, however if I remove the pop up, no change in the color is seen. Why is no change seen? Should I add more code or is that understandable enough?
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are working with the AWT your code might work okay. Swing is finicky about threads because of its one–thread design. Once the gui is realized (visible on the screen) everything affecting it occurs in a single thread: the event dispatching thread. You can have other threads running in the vm but they cannot update the gui except by queuing into the edt. The primary agency for this is the static SwingUtilities method invokeLater.

For simple timing event code the Swing Timer is useful since it is designed to update via the edt.
 
Matthew Fyffe
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hrm, I took your code and ran it and it functioned okay, but the key difference is that yours uses JPanels. I might be able to do that with mine but it would take a lot of work. Here's my code for the whole program (I changed the code according to the first guy so its quite a bit different now but still the same problem).

Where the problem arises in the runTimeStep(int k) method. The method should find which button it is to change, change the color, pause, and then change back. However it merely skips over the changing color part. However, if I put a JOptionPane between the code, it works fine and the change is seen.


[ July 10, 2004: Message edited by: Matthew Fyffe ]
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The purpose of the code in my earlier post was:
1 - to demonstrate how to use a javax.swing.Timer for animation in Swing

2 - to illustrate how to design a class that does animation.

The way you are trying to use threads does not work in Swing. If you want to learn how to use them properly I recommend that you start with How to Use Threads in the Creating a GUI with JFC/Swing trail of the java tutorial.

I made two changes in your runTimeStep method and it seems to update the gui okay now. I moved the int l declaration into class scope to avoid trouble with it in the inner Runnable class.

This line else if(((Color)colors.get(i)).equals(determineColor(o))) is throwing IndexOutOfBoundsExceptions.
 
Matthew Fyffe
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, my mistake. I tend to avoid threads at all costs as I've never really become familiar with them (I've only taken one java course and we never even touched the subject). Thanks for your help, I'll run it through and see what happens.

[ July 10, 2004: Message edited by: Matthew Fyffe ]

After testing the new code it still doesn't quite work as intended but its close enough that I can fix it from here. Thanks much.
[ July 10, 2004: Message edited by: Matthew Fyffe ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic