我一直致力于将我的一些处理代码移植到 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;
}
希望这对您有所帮助。
关于java - 加工过程中的不同颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/165346/