我正在尝试在Java Draw中扩散一种颜色(它没有正常扩散的能力),但是我遇到了一个错误,而且似乎无法发现它。
我进行扩散的方式是编写一个小的脚本,该脚本可以绘制数百次形状,每次绘制时都会变小并且颜色会略有不同。这是我的片段:

import javax.swing.*;
import java.awt.*;
public class DiffuseDraw extends JFrame
{
  //set size of window here
  final int length = 350;
  final int height = 370;

  public DiffuseDraw()
  {
    super("Graphics Project Window");
    Container container = getContentPane();
    setBackground(new Color(0,0,0));
    setSize(length, height);
    setVisible(true);
  }


// my problem comes here:

  public void paint(Graphics g)
  {

    Draw.fillRectangle(g,0,0,length,height,new Color(19,24,32));

    int rad = 325; // size of the biggest circle

    float floatGreen = 0; // Color components for black
    float floatBlue = 0;
    float floatRed = 0;

    int counter = 0; // the counter
    while (counter < 290) {
       rad = rad - 1; // Here we shrink the by a small incriment

       floatRed = floatRed + 87/290;  //red component slowly goes brighter
       floatBlue = floatBlue + 178/290; // blue component slowly goes brighter
       floatGreen = floatGreen + 211/290; // green component slowly goes brighter
       int intRed = (int)Math.round(floatRed);
       int intBlue = (int)Math.round(floatBlue);
       int intGreen = (int)Math.round(floatGreen);

       Draw.fillCircle(g,173,307,rad,new Color(intRed,intBlue,intGreen));
       counter = counter + 1;
    }

  }

  public static void main(String args[]) {
    DiffuseDraw prog = new DiffuseDraw();
    prog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}


当我编译并运行时,我只会得到黑屏。我猜问题出在颜色没有变化。我要添加浮点数红色,蓝色和绿色的数字来自我想要扩散一种蓝色的类型,因此我采用了出现的最亮的蓝色,并将其除以290(循环运行的次数)。

最佳答案

代替

87/290178/290211/290

尝试使用

(float)87/290(float)178/290(float)211/290

至少,这将为窗口添加一些颜色-问题是默认情况下,像87/290这样的数字将被评估为0;将其强制转换为float会得到所需的0.3结果。

09-04 05:42