首页 > 解决方案 > 如何更改图形颜色

问题描述

所以我试图改变这些矩形在画布上绘制时的颜色,但即使我为图形设置了颜色,它们也总是呈灰色。这是我的代码:

public static void rectTransition(int[] array, int smallerNumberIndex) throws InterruptedException
{
    for(int x = 0;x<10;x++)
    {
        if(smallerNumberIndex%2==0)
        {
            canvas.getGraphics().setColor(new Color(000000));
            canvas.getGraphics().clearRect(((smallerNumberIndex*10)+9)-x, 600, 1, -array[smallerNumberIndex]*10);
            canvas.getGraphics().clearRect(((smallerNumberIndex*10)-1)-x, 600, 1, -array[smallerNumberIndex]*10);
            canvas.getGraphics().drawRect(((smallerNumberIndex*10)-1)-x, 600, 1, -array[smallerNumberIndex]*10);
        }
        else
        {
            canvas.getGraphics().setColor(new Color(20, 217, 72));
            canvas.getGraphics().clearRect(((smallerNumberIndex*10)+9)-x, 600, 1, -array[smallerNumberIndex]*10);
            canvas.getGraphics().clearRect(((smallerNumberIndex*10)-1)-x, 600, 1, -array[smallerNumberIndex]*10);
            canvas.getGraphics().drawRect(((smallerNumberIndex*10)-1)-x, 600, 1, -array[smallerNumberIndex]*10);
        }
        Thread.sleep(200);
    }

}

标签: java

解决方案


推荐阅读