simple action methods

original code given:

int[] x = {50,61,83,69,71,50,29,31,17,39};
int[] y = {18,37,43,60,82,73,82,60,43,37};
beginShape();
for(int i=0;i < x.length; i++)
vertex(x[i],y[i]);
endShape(CLOSE);`

Add the appropriate methods so the star will appear at a random location. This method does NOT allow the user to select the location of a star.

Use a "for" loop in the setup method to call the star() method 100 times to produce 100 stars on the screen.

This is what I have so far...something obviously isn't right

void setup()
{
size(800,600);
background(#0F4D7C);

for (int i = 0; i < 100; i++)
{
randomStar();

}
}


void randomStar() 
{
pushMatrix();
translate(0,0); 
int[] x = {50,61,83,69,71,50,29,31,17,39};
int[] y = {18,37,43,60,82,73,82,60,43,37};

beginShape();
for(int i=0;i < x.length; i++)
vertex(x[i],y[i]);
endShape(CLOSE);
popMatrix();
}

Answers

  • Answer ✓
    // forum.processing.org/two/discussion/10235/simple-action-methods
    
    static final int[] X = {50, 61, 83, 69, 71, 50, 29, 31, 17, 39};
    static final int[] Y = {18, 37, 43, 60, 82, 73, 82, 60, 43, 37};
    static final int LEN = X.length;
    
    void setup() {
      size(800, 600, JAVA2D);
      smooth(4);
      noLoop();
      frameRate(5);
    
      fill(0350);
      stroke(0);
      strokeWeight(2.0);
    }
    
    void mousePressed() {
      redraw();
    }
    
    void keyTyped() {
      redraw();
    }
    
    void draw() {
      background(#0F4D7C);
      translate(random(width - 80), random(height - 80));
    
      beginShape();
      for (int i = 0; i != LEN; vertex(X[i], Y[i++]));
      endShape(CLOSE);
    }
    
  • Oh, it's supposed to stamp 100 stars! I guess this is how:

    // forum.processing.org/two/discussion/10235/simple-action-methods
    
    static final int[] X = {50, 61, 83, 69, 71, 50, 29, 31, 17, 39};
    static final int[] Y = {18, 37, 43, 60, 82, 73, 82, 60, 43, 37};
    static final int LEN = X.length;
    
    void setup() {
      size(800, 600, JAVA2D);
      smooth(4);
      frameRate(10);
    
      fill(0350);
      stroke(0);
      strokeWeight(2.0);
    
      background(#0F4D7C);
    }
    
    void draw() {
      translate(random(width - 80), random(height - 80));
    
      beginShape();
      for (int i = 0; i != LEN; vertex(X[i], Y[i++]));
      endShape(CLOSE);
    
      frame.setTitle(str(frameCount));
      if (frameCount == 100)  noLoop();
    }
    
  • Hello GoToLoop !

    for (int i = 0; i != LEN; vertex(X[i], Y[i++]));

    ^:)^ :)

    Why are you using "static final" at the beginning ?

  • edited April 2015

    Funny you've only noticed it now since I do that almost in all of my sketches! :-$

    final guarantees that those fields X & Y gonna be forever bound to their initial arrays.
    static avoids redundant re-instantiation of those 2 arrays in case the same sketch is instantiated more than once. Since those arrays' contents aren't supposed to change. They're immutable!

  • "static avoids redundant re-instantiation of those 2 arrays in case the same sketch is instantiated more than once"

    I see ! It was unnecessary here but it's smart :) Thanks !

Sign In or Register to comment.