access to an array at a random index point, to compare to another random...

I don't know if there is another forum out there that will answer my qestion, so if anyone can point metoward it i would be very gratefull.

In the meantime however here is my problem.

I have been coding a GUI window to demonstrate the Stroop Effect (see this link http://en.wikipedia.org/wiki/Stroop_task for details of the Stroop Effect).

In essence I need to add a bunch of words (Red, Blue, Yellow, Green) to a window and have the colour of the typeface in a colour that is not the same as the word (hence the word "Red" will display in any colour other than "Red").

Well I have been able to get everything to work as desired, but decided that for a bit of fun I would go one step further.

what I have done is the following.

Created some arrays

String[] word = {"Blue", "Red", "Green", "Yellow"};

Color[] color = {Color.blue, Color.red, Color.Green, Color.Yellow};

I have now attempted to add in a level of randomness for adding the item into the window.

I try to ensure that the word

and it's color

are not the same using the following code:

private String setColourOfWord()

{//start setColourOfWord

String c = this.getColourOfWord();

String w = displayColour.toString();

boolean bool = c.equalsIgnoreCase(w);

/*set details for random number generation between 0 -4

*used for determining the index of the details to be sent to the

*paint method.

*/

while (bool)

{//start while

c = this.getColourOfWord();

bool = c.equalsIgnoreCase(w);

System.out.println("word is" + c +"colour is " + w);

}//end while

return c;

}//end setColourOFWord

/*

*Private helper methos related to setColourOfWord(), but in this

*instance the information returned is the colour that the font

*shall appear in.

*

*

*This method also checks that the displayColour is not the same as the

*word (eg Blue will not appear in Blue, but only in Red, Yellow or

*Green)

*

*/

private String getColourOfWord()

{//get colourOfWord

int i;

Random r =new Random();

i = r.nextInt(3);

String[] myStringArray ={"Blue",

"Red",

"Yellow",

"Green"};

colourOfWord = myStringArray[i];

return colourOfWord;

}//end getcolourof word

private Color getDisplayColour()

{//get displayColour

int i;

Random r =new Random();

i = r.nextInt(3);

Color[] myColourArray ={Color.BLUE,

Color.RED,

Color.YELLOW,

Color.GREEN};

displayColour = myColourArray[i];

return displayColour;

}//end get displayColour

the three methods

setColourOfWord()

getColourOfWord()

getDisplayColour()

are all utilised by the paintComponent() method

publicvoid paintComponent(Graphics g)

{//start paint

int i=1;

//System.out.println(setColours());

//this.setColours();

System.out.println("in paint" + i);

Font stroopFont =new Font("Serif", Font.BOLD,18);

g.setFont(stroopFont);

g.setColor(this.getDisplayColour());

g.drawString(this.setColourOfWord(), 25, 25);

i++;

}//start paint

When I run this code I regularly get a word (eg Red) appearing in the colour RED -

Can anyone help me understand where I am going wrong and what the solution would be.

The whole of the code for the project can be posted if required.

thanks for all the help in advance.

Dave

[5135 byte] By [IAamDavea] at [2007-11-27 10:59:43]
# 1

When you choose the two random int indexes, make sure that they aren't the same. If they are, choose a different index until they're different. That's the brute force way, but the chances of the same index being chosen more than a few times in a row is tiny, and since you're just picking a random int and comparing it, you won't see a performance issue.

hunter9000a at 2007-7-29 12:25:02 > top of Java-index,Java Essentials,New To Java...
# 2

ignore. I was wrong

Message was edited by:

petes1234

petes1234a at 2007-7-29 12:25:02 > top of Java-index,Java Essentials,New To Java...
# 3

You will see why your method failed by doing this:

{

Color myColor = Color.red;

System.out.println("Color.red.toString() = " + myColor.toString());

}

It's not what you expected, is it?

Hunter's method is a good one, and you would do well to go with it. Me, I'm too careless and liable to get the order of the two arrays mixed up. One way to get around a potential mix up problem like that is to use an enum. This may be over-kill, but it was fun to make:

import java.awt.Color;

import java.util.Random;

public enum ColorEnum

{

BLUE("Blue", Color.blue),

RED("Red", Color.red),

GREEN("Green", Color.green),

YELLOW("Yellow", Color.yellow),

ORANGE("Orange", Color.orange),

MAGENTA("Magenta", Color.magenta),

BLACK("Black", Color.black),

GRAY("Gray", Color.gray);

private String name;

private Color color;

private ColorEnum(String name, Color color)

{

this.name = name;

this.color = color;

}

public String getName()

{

return name;

}

public Color getColor()

{

return color;

}

public static ColorEnum randomColor()

{

Random rand = new Random();

int index = rand.nextInt(ColorEnum.values().length);

return ColorEnum.values()[index];

}

public static ColorEnum anythingButThisColor(ColorEnum ce)

{

ColorEnum randColorEnum = randomColor();

while (ce.equals(randColorEnum))

{

randColorEnum = randomColor();

}

return randColorEnum;

}

}

And it could be used like so:

public void tryItOut()

{

ColorEnum colorText = ColorEnum.randomColor();

ColorEnum colorTextColor = ColorEnum.anythingButThisColor(colorText);

System.out.println("colorText = " + colorText.getName());

System.out.println("colorTextColor = " + colorTextColor.getColor().toString() +

", name = " + colorTextColor.getName());

// in your paintComponent() method

// you would set your color with g.setColor(colorTextColor.getColor());

// and set text with: g.drawString(colorText.getName(), 25, 25);

// or something like this.

}

One reason that I like this is that if I want to add another color, I can just update the enum and leave everything else unchanged.

Message was edited by:

petes1234

petes1234a at 2007-7-29 12:25:02 > top of Java-index,Java Essentials,New To Java...
# 4

Pete and Hunter.

I do like the idea of using an "enum" the code looks a little "complex" but understandable to a beginer like me.

I also agree with hunter that the liklehood of having the same int used by both of my methods is unlikely, but it actually happens every time I run the main() method to generate the window - so 1 in 12 ! which was quite a surprise.

Looking at your suggestions I expect the "enum" idea will be best as that way I assume the chance of getting the same <Color.text> and <ColorEnum> more than twice in a run cycle will be reduced.

Thanks for the tips gents, I'll try it over the weekend and report back on the results.

Very helpfull Guys, thanks again.

Dave

IAamDavea at 2007-7-29 12:25:02 > top of Java-index,Java Essentials,New To Java...
# 5

I reread your code and noticed that you create a new Random object each time you call getColorOfWord(). Since the Random constructor with no args uses the current time as a seed, and the random sequence will be identical for identical seed, creating multiple Randoms in quick succession will give completely identical streams of numbers. Create one Random object for your entire class to use, and you'll get an actual random stream.

hunter9000a at 2007-7-29 12:25:02 > top of Java-index,Java Essentials,New To Java...
# 6

> I reread your code and noticed that you create a new

> Random object each time you call getColorOfWord().

> Since the Random constructor with no args uses the

> current time as a seed, and the random sequence will

> be identical for identical seed, creating multiple

> Randoms in quick succession will give completely

> identical streams of numbers. Create one Random

> object for your entire class to use, and you'll get

> an actual random stream.

You are right. My bad.

petes1234a at 2007-7-29 12:25:02 > top of Java-index,Java Essentials,New To Java...
# 7

> > I reread your code and noticed that you create a

> new

> > Random object each time you call getColorOfWord().

> > Since the Random constructor with no args uses the

> > current time as a seed, and the random sequence

> will

> > be identical for identical seed, creating multiple

> > Randoms in quick succession will give completely

> > identical streams of numbers. Create one Random

> > object for your entire class to use, and you'll

> get

> > an actual random stream.

>

> You are right. My bad.

It's cool, your suggestions were good too. I think that starting in 1.5 or 1.6, Random has an internal counter that increments everytime an instance is created so that doesn't happen, but it's still a good practice to use a single instance.

hunter9000a at 2007-7-29 12:25:03 > top of Java-index,Java Essentials,New To Java...