开发者

JAVA: How to cycle color

开发者 https://www.devze.com 2023-03-16 13:50 出处:网络
public class MyPix () { pixColor = Color.BLUE; } public void draw(Graphics g) 开发者_运维问答{ g.setColor(pixColor);
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.


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


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消