I'm having a bit of a dificulty with this, I was given an asignment in which i have to 'paint a rectangle' using MouseDragged and the like.. okay i got THAT, but how do i paint it using JRadioButton? how do i even declare a color? help! i need to have to the options to paint it magenta,black,green,yellow,blue, and red.
D= some help? some helpful advice?
"There are three things all wise men fear: the sea in storm, a night with no moon, and the anger of a gentle man."
--Patrick Rothfuss, The Wise Man's Fear
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32710
4
posted
1
Welcome to the Ranch
Why do you want to use a radio button to colour a component? You would usually use a colour chooser.
Do you know how to add listeners to buttons?
Maybe you need a set of radio buttons in a ButtonGroup so clicking on one radio button turns the currently selected one off and each radio button has an ActionListener to change the colour of the rectangle to a color associated to the selected radio button.
yup, I know about adding listeners to buttons. The assignement is weird, I know, and I know the functions of JRadioButtons and all, but it doesn't work, guys D=
i am experimenting to see if with a normal Button it works, but there's an 'exception in function main' urrrgh that drives me crazy!
i'll send some code in a few.. to see what's your opinion
-by the way, thanks for wanting to help!
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
1
one way is:
declare an array of colors Color[] colors = {Color.RED,Color.BLUE, etc};
give each radioButton a 'name' which is an index number of the color array (the 'name' will be a string, to convert later)
a class variable - int someName
in the radioButton's actionListener, set someName to the int value of the radioButton's name, and call repaint()
in paintComponent() you set the color to colors[someName] and fillRect(..) paints it the color you've set
give each radioButton a 'name' which is an index number of the color array (the 'name' will be a string, to convert later)
Personally when faced with this sort of situation I would create a Map with the colours as the values and unique names as the keys, then set the command string of each radio button to one if the unique names. You now need just one ActionListener that can be shared by all Radio Buttons and in it you can get the source component, get it's command string and use that to extract the appropriate colour from the Map.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32710
4
posted
0
Another way would be to create a ColourListener class. This shows how you might use it (or maybe not ).
I usually do not like action commands at all, but Tony Docherty’s suggestion of a Map is an exception, where action commands are a good idea.
hmm, every one of your suggestions make sense i could try that, yes.
just, here's the code I made. The assignment is to change that to enable a background color for the created rectangle with a JRadioButton. Thus comes the source of my problems.
please ignore the fact that it's in spanish, and please help me with how to edit this and do what is asked D= i'd appreciate it a lot!
how would you guys do that? using the code provided above?
(i'm lazy enough to ask you this, though you probably would think weird of me: could you do it for me? i'm just asking without much hope lol)
(i'm lazy enough to ask you this, though you probably would think weird of me: could you do it for me?
No, we don't do homework for people.
BTW the fact that your code is in Spanish is hard to ignore if you don't speak Spanish because none of the method/variables names make any sense to a non Spanish speaker.
Several people have already given you advice on how to solve your problem, I suggest you read it and act on it. If you don't understand what has been said then please ask for clarification.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32710
4
posted
0
You should always use code tags, which I have added to your post, and you can see how much better it looks Beware of long lines, however.
As I said, I didn't say that with much hopes, I kind of already realized that this sight is a respectable one and doesn't induce people to cheat on their works. If I insulted any of you with that remark, I beg your pardon. And thank you, because your advice surely does help.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: how can I use JRadioButton to color a rectangle?