1 / 7

C O L O R S

C O L O R S. The Plan. Arrays of Colors Random Selection of Colors Custom Colors Random Generation of Colors. Coloring 4 Cars; more …. Create Array of Colors to Use Color[] colors = {Color.GREEN, Color.BLUE, ... };

asta
Download Presentation

C O L O R S

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COLORS

  2. The Plan • Arrays of Colors • Random Selection of Colors • Custom Colors • Random Generation of Colors

  3. Coloring 4 Cars; more … • Create Array of Colors to Use Color[] colors = {Color.GREEN, Color.BLUE, ... }; • When using loop to create an array of sprites, assign color using same array index car[k].setColor(colors[k]); • What if we have more cars than colors in our array? car[k].setColor(colors[k % colors.length]); • Wraps around and re-uses the colors

  4. Random Assignment of Colors • Can select from our array of colors, or palette, at random • Math.random() gives us a random value between 0 and (but not including) 1.0. • (int)(Math.random()*n) gives us a random integer in the range 0 thru n-1 • Use car[k].setColor( colors[(int)(Math.random()*colors.length)] );

  5. Custom Colors • You do not have to limit yourself to the standard pre-defined Java colors. • Use new Color(r, g, b) to generate any colors where r, g, and b are each ints in the range 0-255 • They specify the mixture of red, green, and blue that you want. new Color(255, 0, 0) give us pure red new Color(0, 255, 0) give us pure green new Color(0, 0, 255) give us pure blue • Mix and stir at will

  6. Random Colors • We can use our Math.random function to help us generate random colors so that each time you run your program, the colors are different • (int)(Math.random()*255) gives us a random int in the required range. • Can then write car[k].setColor(new Color((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255)) );

  7. Transparency • The Color class allows you to specify a transparency to the color as well as the three color components. • Feel free to try this out and to experiment. • Use your Java API to look at what is available (and alternate ways of specifying colors).

More Related