2

我试图在 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,即循环运行的次数。

4

1 回答 1

1

代替

87/290, 178/290, 和211/290,

尝试使用

(float)87/290, (float)178/290, 和(float)211/290.

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

于 2012-12-12T03:29:08.850 回答