Search code examples
javacolorsprocessingspiral

Varying Colors in Processing


I've been working on porting some of my Processing code over to regular Java in NetBeans. So far so well, most everything works great, except for when I go to use non-grayscale colors.

I have a script that draws a spiral pattern, and should vary the colors in the spiral based on a modulus check. The script seems to hang, however, and I can't really explain why.

If anyone has some experience with Processing and Java, and you could tell me where my mistake is, I'd really love to know.

For the sake of peer-review, here's my little program:

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++;
    }
}

Solution

  • Have you considered adding debugging statements (System.out.println) and looking at the Java Console?

    There may be a massive amount of output and definitive slowdown, but you can at least see what happens when nothing seems to happen.

    What I do think is a logic error is the filling if statement; every iteratation you decide the color of that iteration and fill with that color. Only iterations with i == 11, 13, or 17 get filled with a color. And the next iteration that color is overwritten by gray. I would think it tends to flicker, possibly to fast to see.

    Didn't you want something like

    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);
    
            ...
    }
    

    In that way you start with gray, go to green, blue, pink, green, blue, pink etc. If you also want to see gray at some point you'd have to add something like

      else if ( i % 19 ) {
        currentColor = gray;
      }
    

    Hope this helps.