有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在处理过程中改变颜色

我一直在努力将一些处理代码移植到NetBeans中的常规Java中。到目前为止,除了使用非灰度颜色外,大多数东西都很好用

我有一个脚本,绘制了一个螺旋模式,应该根据模数检查改变螺旋中的颜色。然而,剧本似乎悬而未决,我真的无法解释原因

如果有人有处理和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++;
    }
}

共 (1) 个答案

  1. # 1 楼答案

    您是否考虑过添加调试语句(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;
      }
    

    希望这有帮助