How to slowly change object color from one to another?

前端 未结 6 1712
迷失自我
迷失自我 2020-12-02 01:38

I am trying to achieve a scenario where the color of an object changes slowly from one color to another color.

I have the initial color as targetColor and final col

6条回答
  •  攒了一身酷
    2020-12-02 02:01

    Yea, instead of using the for loop in line

    while(i<=10);

    you can actually use your changing speed. Meanwhile your changingSpeed should

    be an array of length 3 because the different offset of colors interval i.e

    dr, dg, db so that they can be taking care of independently.

    like this...

    int [] changingSpeed(int []Offset){
    
    int loop = 5;
    
    // 5 means the color should be change 5 times
    
    int [] changeSpeed = new int[3];
    
    changeSpeed[0]= offset[0]/loop;
    
    changeSpeed[1]= offset[1]/loop;
    
    changeSpeed[2]= offset[2]/loop;
    
    return changeSpeed;
    }
    
    // your update method will now look like this
    
    updateColor(int [] changeSpeed) throws AWTException{
    
    int dr = changeSpeed[0];
    
    int dg = changeSpeed[1];
    
    int db = changeSpeed[2];
    
    Robot slow = new Robot();
    
    int i=0;
    
    
    int f= loop; // the number of time you want the color to change
    
    while(i<=f){
    
    slow.delay(1000)
    
    //sleep will sleep for 1000ms
    
    setColor(targetColor.getRed() + dr/10, targetColor.getGreen(),targetColor.getBlue());
    
    setColor(targetColor.getRed(), targetColor.getGreen() + (dg/10),targetColor.getBlue());
    
    setColor(targetColor.getRed(), targetColor.getGreen(),targetColor.getBlue() + db/10);
    
    i++;
    
    }
    
    }
    

提交回复
热议问题