Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

random colour problems

 
Es Tresidder
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run the code below to produce a circle with a random colour gradient on it I get the following error message.

SimpleGraphicsTestDrive.java:34: red is already defined in paintComponent(java.a
wt.Graphics)
int red = (int) (Math.random() * 256);
^

The code worked before I inserted the random colours, and the random colours code was lifted straight from the book. Seems it has a point, the code is defining "red" as a random colour, right? Which will surely contradict what it says in the paintComponent code. What to do? Code below.


 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't declare 2 variables with the same name. Just assign the value instead of defining it again.
 
Es Tresidder
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that seems to have fixed it (just naming the three ints once each), funny that it was written that way in the book.
 
Es Tresidder
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This now works to produce a randomly coloured oval, but the colour gradient doesn't work anymore. It did work before I put these random colours in. Now it just produces a solid, randomly coloured oval.

While I'm here, how does the random colour assignation work here. the int "red" isn't actually red is it? It's a random colour defined by a random fraction, multiplied by 256 to give the index colour defined somewhere else in java, right? And why three colours, am I asking it to do a gradient through these three colours?

Why isn't it working? Code below.


 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just read the code:
 
Es Tresidder
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David

You're going to have to spell it out to me I'm afraid. I'm a beginner at all this and am struggling a bit. I can read that but fail to spot what is wrong with it.

Thanks.

Es
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the values used for the start and end colors?
 
Es Tresidder
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what the values are that are used for the start and end colors. Looking at the code I think they should each be a random mix of three colors, but I'm not at all sure! I don't understand why we are giving three different colors from which to make the start and end color, I don't understand whether these really are red, green and blue or whether they are random colors, and if I'm meant to be giving arguments from the command line I don't understand how I should be doing that. Currently I'm just typing "java SimpleGraphicsTestDrive". I have tried it by putting 3 numbers after this, which makes no difference.

Sorry for being thick.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Es Tresidder wrote:I don't know what the values are that are used for the start and end colors.

...
The variable names "startColor" and "endColor" are a clue as to their purpose: they are the start and end colors. So what values are used for the start and end colors? Are they the same, or different? What would a gradient look like if, say, the start and end colors were the same?
 
Es Tresidder
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I understand that they are the start and end colors. What I don't understand is how to find out what their values are. Are they not random? I thought that was the point of this bit of code! Why the three colours? And is "red" really red or is it a random colour?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same values are used for the start and end colors. That's why the gradient doesn't gradate.

The value for red is a random number between 0-255, as are the values for green and blue. A pixel is made up of a combination of red, green, and blue values (or at least a Color is--there are other color systems). You could find out the value by printing it, examining it in a debugger, and so on. So you're generating two colors (technically, in reality they're the same values because you don't generate new random numbers) with random RGB values.
 
Es Tresidder
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David, got that working now. Realised that my fix for the initial problem just created the current one!

So am I right in thinking that the "red" I have defined here is not necessarily anything like the red that I would see on a rose, but just a random color picked from a list of 255 options?

Why give it three colours to blend to produce the colour, why not just give it one colour for the start colour and one for the end colour?

Thanks for your help.

Es
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Es Tresidder wrote:So am I right in thinking that the "red" I have defined here is not necessarily anything like the red that I would see on a rose, but just a random color picked from a list of 255 options?

No, you're not right. "Red" is a range, from white to full-on red. Pink, for example, is a red (for the sake of this discussion).
Why give it three colours to blend to produce the colour, why not just give it one colour for the start colour and one for the end colour?

Because that's how color works (or at least one way--there's also HSV and several other systems). Take a magnifying glass to your TV or LCD: what do you see?

http://en.wikipedia.org/wiki/RGB_color_model
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Es Tresidder wrote: . . . Why give it three colours to blend . . .?
Because your eyes have three kinds of colour receptor, which are most sensitive to red, green and blue light. That is why David said "that's how color works."

Unless you are one of the 4% of the male population who are colour-blind.
 
Es Tresidder
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the help guys. Think I'm more or less getting it now. So is my summary below correct:

My code chooses a random red colour between 0 and 256
My code chooses a random green colour between 0 and 256
My code chooses a random blue colour between 0 and 256

My code blends the three colours above to produce a start colour.

Then what does it do? For endColor does it choose three more random colours and blend these?

That should be my final annoying question! thanks again.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then what does it do? For endColor does it choose three more random colours and blend these?
Code executes in order: you tell us.
 
Es Tresidder
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David

Sorry, forgot to say that I've changed the code a bit now. See below. So does it produce a different set of three random colours for the endColor or does it use the same ones as for startColor? What about my other statements, were they right?

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again: you tell us. Does it? Please ShowSomeEffort -- we're all happy (dare I say tickled pink) to help out when people have problems, but there has to be *some* display of effort on your part. Tell me, chunk by chunk, what the code you just posted does. If you do, and that *still* doesn't answer your question, then I'm nervous.

Also not that since you're asking questions about random numbers, there is an element of chance involved--so no matter which answer I give, I have a 1-in-256^3 chance of being wrong any given run.

Recall the hint I gave earlier about printing out the values, or inspecting them in a debugger, etc.
 
Es Tresidder
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David, thanks for the encouragement to put some effort in, I just have done and made some progress. I should have done this from the start, sorry.

So I now understand the red, green, blue thing to create the random colour. And I have put a "println" tag in the code so that it tells me what the value of red is after it has assigned the startColor and the endColor. However, when I do this I get four values, indicating that it is running through the color assignation code twice. I can't see why it would do this from the code, there doesn't seem to be anything in there telling it to loop through.

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
paintComponent can happen at several times, and for several reasons, during a component's lifecycle--if you have specific questions about you should post in the Swing/etc. forum, since that's outside the purview of Beginning Java. If you want to ensure the gradient stays the same, the values for the start and end colors should be chosen only once, probably at application startup.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic