Canvas Demo 1

Aus ProgrammingWiki
Archivierte Version vom 2008-11-17T10:44:38+00:00. Aktuelle Version

Aufgabe 1

Verwende for Schleifen, um 25 farbige Rechtecke in einer Matrix von 5 mal 5 Feldern in der Zeichenfläche auszugeben. Hierfür kannst du die Befehle canvas.setColor(r,g,b) und canvas.fillRect(x1,y1,x2,y2) verwenden. <eval> canvas.clear(); for (int i=0;i&lt;6;i++){</eval>

   for (int j=0;j<6;j++){
     canvas.setColor((int)(255-42.5*i),(int)(255-42.5*j),0);
     canvas.fillRect(j*25,i*25,25,25);
   }
 }

Ihr Browser unterstützt Canvas nicht.

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

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 >  100) bounce();
 if (y < -100) bounce();
 if (x >  250) bounce();
 if (x < -250) 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++;
     canvas2.setColor(r,g,255-r);
  }else{state = 1;}
 }
 if (state == 1){
  if(g<255){ 
     g++;
     canvas2.setColor(255-g,g,b);
  }else{state = 2;}
 }
 if (state == 2){
  if(b<255){ 
     b++;
     canvas2.setColor(0,255-b,b);
  }else{state = 0; r=0; g=0; b=0;}
 }

} canvas2.clear();

for (int i =0; i<1000; i++){

canvas2.setWidth(3);
canvas2.ellipse((int)x+300,(int)y+100,3);
advance();

}

Ihr Browser unterstützt Canvas nicht.