1
public class MyPix ()
 {
     pixColor = Color.BLUE;
 }

public void draw(Graphics g)
    {
        g.setColor(pixColor);
        g.fillOval(5,5,10,10);
    }

Given I have above simplified class. How would I make my shape cycle in color without interaction. I did not get the thread/sleep thing to work yet.

Stephan Kristyn
  • 15,015
  • 14
  • 88
  • 147
  • that's about javax.swing.Timer http://stackoverflow.com/questions/6171414/how-to-share-data-with-two2-swingworker-class-in-java/6174462#6174462 – mKorbel Jun 30 '11 at 07:10

2 Answers2

3

Without interaction your going to need to user something like SwingWorker, which is easier to work with in Swing than Thread/Sleep system. See the tutorial on SwingWorker for more information

TheLQ
  • 14,830
  • 14
  • 69
  • 107
  • 2
    Probably, more precisely, a javax.swing.Timer. – Lawrence Dol Jun 29 '11 at 23:13
  • 1
    @Software Monkey Put that in an answer, I forgot about it. Its much better for this problem – TheLQ Jun 29 '11 at 23:18
  • 1
    @Stephan No, you have to implement it. To do it quick just pass an anonymous ActionListener as the second parameter in the Swing Timer constructor. – TheLQ Jun 30 '11 at 00:56
  • @TheLQ: It works. Thanks a lot. - Also see: http://life.csu.edu.au/java-tut/uiswing/components/example-1dot4/ProgressBarDemo.java – Stephan Kristyn Jun 30 '11 at 02:05
  • @TheLQ: How is that technique (of invoking while constructing) called? Is there a common definition for that? Is it called a Singleton, because there can be only one instance of the anonymous class? – Stephan Kristyn Jun 30 '11 at 05:00
  • 1
    @Stephan Its not really any kind of pattern, its just passing an instance. And I guess technically an anonymous inner class is a singleton, but only in an extremely general way. – TheLQ Jun 30 '11 at 05:23
1

Do you want discrete or continuous color changes? If the latter --

static Color getBGColor1() {
    float h = System.currentTimeMillis()*1e-3f,
          s = .1f, b = .9f;
    return Color.getHSBColor(h, s, b);
}

Edit: perhaps you want something like this?

static Random rnd = new Random();
static Color getBGColor2() {
    rnd.setSeed(System.nanoTime()/1000000000*1337);
    float h = rnd.nextFloat(), s = .1f, b = .9f;
    return Color.getHSBColor(h, s, b);
}
Daniel Lubarov
  • 7,796
  • 1
  • 37
  • 56
  • That looks elegant, but I don't understand it fully. Why chose 5f and 9f and 1e3f ? – Stephan Kristyn Jun 29 '11 at 23:36
  • 1
    I basically made them up. Dividing a time in milliseconds by 1e3 (1000) should convert it to seconds, so `System.currentTimeMillis()/1e3f` is just the current time in seconds. `getHSBColor` normally takes a hue between 0 and 1, but we can also give it a float above 1 and it will use the fractional part. So this code cycles through hues with a period of one second. If that seems too fast or too slow, you can add any multiplier you like. I picked `s` and `b` (saturation and brightness) arbitrarily; feel free to change those too; – Daniel Lubarov Jun 30 '11 at 00:55
  • It cycles with this: h = (float) Math.random(), s=3, b=2; but is ugly as heck. – Stephan Kristyn Jun 30 '11 at 01:26
  • 1
    I added some different code, is that closer to what you're after? (The 1000000000 causes it to switch to a random color once per second, but you can use a larger int for slower changes or a small int for more rapid changes.) – Daniel Lubarov Jun 30 '11 at 02:44
  • 1
    Thank you Daniel, I especially like the way you integrated 1337 ;D – Stephan Kristyn Jun 30 '11 at 07:47