题
我一直在努力将部分处理代码移植到NetBeans中的常规Java。到目前为止,除了当我使用非灰度颜色时,大多数一切都很好。
我有一个绘制螺旋图案的脚本,并且应该根据模数检查改变螺旋中的颜色。然而,剧本似乎悬而未决,我无法解释原因。
如果有人对Processing和Java有一些经验,你可以告诉我我的错误在哪里,我真的很想知道。
为了同行评审,这是我的小程序:
package spirals;
import processing.core.*;
public class Main extends PApplet
{
float x, y;
int i = 1, dia = 1;
float angle = 0.0f, orbit = 0f;
float speed = 0.05f;
//color palette
int gray = 0x0444444;
int blue = 0x07cb5f7;
int pink = 0x0f77cb5;
int green = 0x0b5f77c;
public Main(){}
public static void main( String[] args )
{
PApplet.main( new String[] { "spirals.Main" } );
}
public void setup()
{
background( gray );
size( 400, 400 );
noStroke();
smooth();
}
public void draw()
{
if( i % 11 == 0 )
fill( green );
else if( i % 13 == 0 )
fill( blue );
else if( i % 17 == 0 )
fill( pink );
else
fill( gray );
orbit += 0.1f; //ever so slightly increase the orbit
angle += speed % ( width * height );
float sinval = sin( angle );
float cosval = cos( angle );
//calculate the (x, y) to produce an orbit
x = ( width / 2 ) + ( cosval * orbit );
y = ( height / 2 ) + ( sinval * orbit );
dia %= 11; //keep the diameter within bounds.
ellipse( x, y, dia, dia );
dia++;
i++;
}
}
解决方案
您是否考虑过添加调试语句(System.out.println)并查看Java控制台?
可能会有大量的输出和明确的减速,但你至少可以看到当似乎没有任何事情发生时会发生什么。
我认为逻辑错误是填充if语句;在每个迭代中,您决定该迭代的颜色并填充该颜色。只有i == 11,13或17的迭代才会填充颜色。并且下一次迭代颜色被灰色覆盖。我认为它往往会闪烁,可能会很快看到。
你不想要像
这样的东西public class Main extends PApplet
{
...
int currentColor = gray;
public Main(){}
...
public void draw()
{
if( i % 11 == 0 )
currentColor = green;
else if( i % 13 == 0 )
currentColor = blue;
else if( i % 17 == 0 )
currentColor = pink;
else {
// Use current color
}
fill(currentColor);
...
}
以这种方式,你从灰色开始,转到绿色,蓝色,粉红色,绿色,蓝色,粉红色等。如果你 也希望在某些时候看到灰色,你必须添加像
这样的东西 else if ( i % 19 ) {
currentColor = gray;
}
希望这有帮助。
其他提示
感谢所有的帮助,但我认为我的最终目标有点被误解了。
这是我使用Processing PDE生成的图像:
http://www.deviantart.com/download/97026288/spiral_render_5_by_ishkur88.png
我想要的输出看起来与此类似,不同的是螺旋的颜色和一般形状。
正如上一张海报所提到的:你每11日,第13次和第17次迭代都使用非灰色。
感谢您指出这一点,但我已经知道了!我实际上是这样设计的!
原因是如果我没有灰色默认值,输出会看起来非常混乱,并且非常令人不悦(至少我的眼睛)。
如果有一种方法我可以完全跳过渲染那个圆圈,我当然会更喜欢这个。
要查看此处发生的事情,请添加
stroke(255);
在抽签开始时。你会看到所有想要的圆圈画,但没有颜色。正如上一张海报所提到的:你每11日,第13次和第17次迭代都使用非灰色。
我认为你的颜色值是这里的主要问题。从参考页面
从技术角度来看,颜色是以AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB排序的32位信息,其中A包含alpha值,R是红色/色调值,G是绿色/饱和度,B是蓝色/亮度。
如果你看一下你的值,你会看到一个非常低的alpha值,这可能无法与背景区分开来。
不确定您是否还有问题。你提到挂。这是一个黑暗中的镜头,但我记得重复,size()调用必须是setup()中的第一个指令。因此,向下移动background()调用可能会有所帮助。无论如何都不会伤害。