小学校教員のためのプログラミング入門

黒板スケッチ例

int r = 5;
color nowcolor;
color mc[ ] = new color [5];
int mx = 750, my = 10, ms = 40;

void setup()
{
  size(800,600);
  colorMode(HSB,360,100,100);
  background(119,100,25);
  
  mc[0] = color(0,0,100);
  mc[1] = color(0,100,100);
  mc[2] = color(239,100,100);
  mc[3] = color(59,100,100);
  mc[4] = color(119,100,25);
  nowcolor = color(0,0,100);
}

void draw()
{
  if( isdrag == true )
  {
    stroke( nowcolor );
    strokeWeight( r );
    line( bx, by, mouseX, mouseY );
    bx = mouseX;
    by = mouseY;
  }
  else
  {
    stroke( 0,0,100 );
    for( int i = 0 ; i < 5 ; i++ )
    {
      fill( mc[i] );
      strokeWeight( 1 );
      rect( mx, my+(ms+10)*i, ms, ms );
    }
  }
}

boolean isdrag = false;
int bx, by;
void mousePressed()
{
  int i = 5;
  if( mouseX >=mx && mouseX <= mx+ms )
  {
    for( i = 0 ; i < 5 ; i++ )
    {
      if( mouseY >= my+(ms+10)*i && mouseY <= my+(ms+10)*(i+1) ) break;
    }
  }
  if( i < 5 )
  {
    nowcolor = mc[i];
  }
  else
  {
    isdrag = true;
    bx = mouseX;
    by = mouseY;
  }
}
void mouseReleased()
{
  isdrag = false;
}