Canvas Demo 2

Aus ProgrammingWiki

Wiki-Quelltext dieser Seite.

<language>Java</language>
<eval>
double x = 50;
double y = 0;
double angle = 45;

int r = 0; int g = 0; int b = 0;
int state = 0;

void bounce() {
 angle = angle +45 -5+(int)(Math.random()*5);
 if (angle > 360) angle = angle -360;
 if (angle < 0) angle = angle +360;
}

void advance(){ 
  if (y >  80) bounce();
  if (y < -80) bounce();
  if (x >  280) bounce();
  if (x < -280) bounce();
  x = x + 10*Math.cos(angle*Math.PI/180); 
  y = y + 10*Math.sin(angle*Math.PI/180); 
  if (state == 0){
   if(r<255){ 
      r++;
      canvas.setColor(r,g,255-r);
   }else{state = 1;}
  }
  if (state == 1){
   if(g<255){ 
      g++;
      canvas.setColor(255-g,g,b);
   }else{state = 2;}
  }
  if (state == 2){
   if(b<255){ 
      b++;
      canvas.setColor(0,255-b,b);
   }else{state = 0; r=0; g=0; b=0;}
  }
}
canvas.clear();

for (int i =0; i<200; i++){
 canvas.setWidth(3);
 canvas.ellipse((int)x+300,(int)y+100,3);
 advance();
}
</eval>

<canvas></canvas>