| Author |
How to setColor(Variable)
|
James Hambrick
Ranch Hand
Joined: Sep 04, 2004
Posts: 277
|
|
This is what I have and I was thinking that I can do away with the two ifs to determine color by using g.setColor(SetFontColor) where I use the variable to determine color. SetFontColor is a String and has the value of "Red" is I cilcked red and "Black" if I choose black. This code does not work, but i'm sure there's a way to do this.
|
Visit my blog! http://jameshambrick.com
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24081
|
|
|
This looks generally OK; what does it do wrong? Are you sure those are the exact values of the String (this is a case where I'd use named constants like 'public static final String RED = "Red";' to be sure all copies were the same) ? Are you sure the values of the line end coordinates are what you expect?
|
[Jess in Action][AskingGoodQuestions]
|
 |
James Hambrick
Ranch Hand
Joined: Sep 04, 2004
Posts: 277
|
|
Sorry I meant that the code does not work when I put a variable name into setColor(variable). But as the code stands now it works. Yea I'm sure of the values of SetFonrColor because this code sets the variable depending on whether the user clicked the Red button or the Black button. I just thought the code was repetitive and I could reduce it by using a variable. [ July 10, 2007: Message edited by: James Hambrick ]
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24081
|
|
Well, if the variable "SetFontColor" were of type java.awt.Color instead of String, and you did something like if ("Red".equals(action)) SetFontColor = Color.red; then in your paint method you could just say g.setColor(SetFontColor); A word to the wise: the convention is that Java variable names start with a lower case. Starting them with an upper case letter makes them look like class names, and will confuse people who try to read your code -- myself included. One gets used to seeing things done a certain way!
|
 |
James Hambrick
Ranch Hand
Joined: Sep 04, 2004
Posts: 277
|
|
|
Thanks for the advice Ill do that!
|
 |
James Hambrick
Ranch Hand
Joined: Sep 04, 2004
Posts: 277
|
|
|
How do I make setFontColor a type java.awt.Color???
|
 |
 |
|
|
subject: How to setColor(Variable)
|
|
|