Link Search Menu Expand Document

Loopy Colors

To get more interesting colors, you could write lots of color in a long list, and then keep changing the color of the turtle according to the color of the list. You can create arrays in Java, using square brackets [] and curly braces {}.

Below is an example of a two-dimensional array of RGB colors:

int[][] colors = { {85, 211, 136}, {197, 196, 126}, {235, 233, 166}, {25, 135, 222}, {211, 64, 159}, {159, 165, 106}, {178, 160, 125}, {36, 192, 70}, {231, 184, 204}, {63, 203, 219} };

This next bit gets a bit complicated. Have a look at the code below, then run it to see what happens.

int[][] colors = { {85, 211, 136}, {197, 196, 126}, {235, 233, 166}, {25, 135, 222}, {211, 64, 159}, {159, 165, 106}, {178, 160, 125}, {36, 192, 70}, {231, 184, 204}, {63, 203, 219} };

for (int i = 0; i < 10; i++)
{
	setColor(colors[i][0], colors[i][1], colors[i][2]);
	moveAndDraw(50);
}

The statement colors[i][0] is telling the program to choose the “ith” item in the list. Remember that i starts from 0 and goes up to 9. The [0] is getting the “0th” number in the subarray, which we will use for the red value.

What if you want a longer line? Try changing the number of loops in the for loop to i < 20 and see what happens. Do you get an error?