Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how can I use JRadioButton to color a rectangle?

 
Rylann S. Reave
Greenhorn
Posts: 8
C++ Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Tony Docherty
Bartender
Posts: 2989
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rylann S. Reave
Greenhorn
Posts: 8
C++ Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

 
Rylann S. Reave
Greenhorn
Posts: 8
C++ Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe if I send you the code through this? I dunno and i've been fretting with this all morning
 
K. Tsang
Bartender
Posts: 3526
16
Android Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what exception you getting in main? Are you getting the values from the radio button group? Can the button group access the rectangle?
 
Rylann S. Reave
Greenhorn
Posts: 8
C++ Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nope
 
Rylann S. Reave
Greenhorn
Posts: 8
C++ Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tony Docherty
Bartender
Posts: 2989
59
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rylann S. Reave
Greenhorn
Posts: 8
C++ Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Rylann S. Reave
Greenhorn
Posts: 8
C++ Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Tony Docherty
Bartender
Posts: 2989
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rylann S. Reave
Greenhorn
Posts: 8
C++ Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, we don't do homework for people.


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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic