JC2188
JC2188

Reputation: 337

How to Randomly pick a color from an array of colors in Java?

I'm learning Java and programming for the first time. I'm using the BlueJ environment.

I'm writing a program to draw polygons. I want to change color after every line has been drawn. To do this, I thought I would create an array of colors, then use a pseudo-random number generator to generate a value that would be used as the index to get a color from the array. I've included a snippet of code from the constructor method, as I've been told I need to create the array inside the constructor for this to work.

It should be noted that I've imported java.awt.Color, along with java.util.Random. I've also included

private Color[] colors;

as a field.

public PictureMaker()
{
    world = new TurtleWorld(500, 500, "Picture Maker");
    fred = new Turtle();
    //Allocate the size of the array
    colors = new Color[7];

    //Initialize the values of the array
    colors[0] = new Color(Color.red);
    colors[1] = new Color(Color.blue);
    colors[2] = new Color(Color.yellow);
    colors[3] = new Color(Color.green);
    colors[4] = new Color(Color.black);
    colors[5] = new Color(Color.pink);
    colors[6] = new Color(Color.orange);
}

Below is the code from the actual method that will be called to draw the polygon.

/**
 * This method takes sides (int) as input and draws a polygon with that number of sides.
 */
public void drawPolygon(int sides)
{
    world.dropIn(fred);
    fred.penDown();
    int angle = 360/sides;
    int linesDrawn = 0;


    while(linesDrawn < sides)
    {
        Random r = new Random();
        int minColor = 0;
        int maxColor = 10;
        int R = r.nextInt(maxColor-minColor) + minColor;
        fred.right(angle);
        fred.forward(60);
        linesDrawn += 1;

    }
}

I haven't included anything to get the color from the array yet, as I can't get the array to work.

I'm getting an error message Incompatible types: java.awt.Color cannot be converted to int.

I'm not sure why it doesn't work.

I hope this makes sense.

Please let me know if you need clarification, I'm new to using this site.

Upvotes: 1

Views: 9261

Answers (3)

Aaron C
Aaron C

Reputation: 343

Your array should be like so:

Color[] colors = new Color[7];

    //Initialize the values of the array
    colors[0] = Color.red;
    colors[1] = Color.blue;
    colors[2] = Color.yellow;
    colors[3] = Color.green;
    colors[4] = Color.black;
    colors[5] = Color.pink;
    colors[6] = Color.orange;

There is no constructor for individual colors, so new Color is undefined.

As for picking randomly:

Color c = colors[randInt(0, colors.length-1)]; 

where randInt is this method:

public static int randInt(int min, int max) {
    Random rand = new Random();
    int randomNum = rand.nextInt((max - min) + 1) + min;

    return randomNum;
}

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500145

The Color fields are already of type Color - you don't need to call the Color constructor. The compiler is complaining because there isn't a Color constructor which takes a Color parameter. I suggest you use the more conventionally-named capitalized ones, too - and initialize the array with an array initializer for simplicity:

colors = new Color[]
{
    Color.RED, Color.BLUE, Color.YELLOW, Color.GREEN,
    Color.BLACK, Color.PINK, Color.ORANGE
};

Upvotes: 2

nbz
nbz

Reputation: 3928

I think your array should be like this

//Initialize the values of the array
    colors[0] = Color.red;
    colors[1] = Color.blue;

And so on...

Upvotes: 0

Related Questions