How to slowly change object color from one to another?

前端 未结 6 1723
迷失自我
迷失自我 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:18

    In order to achieve the act of slowness, it might be a good idea to use the instance of Robot class.

    //Method to obtain the offset of the color
    
    static int [] getColorOffset(Color initial, Color final){
        int [] colorOffset = new int[3];
        colorOffset[0]= final.getRed()-initial.getRed();
        colorOffset[1] = final.getGreen()-initial.getGreen();
        colorOffset[2]= final.getBlue()-initial.getBlue();
        return colorOffset;
    }
    
    updateColor(int [] colorOffset) throws AWTException{
        int  dr = colorOffset[0];
        int  dg = colorOffset[1];
        int  db = colorOffset[2];
        Robot slow = new Robot();
        int i=0;
        while(i<=10){
            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++;
        }
    
    }
    
    public static void main(String args[]) throws AWTException{
        Color initial = Color.black;
        Color final = Color,white;
        int [] colorOffset = getColorOffset(initial, final);
        updateColor(colorOffset);
    }
    

提交回复
热议问题