aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes how can I use JRadioButton to color a rectangle? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how can I use JRadioButton to color a rectangle?" Watch "how can I use JRadioButton to color a rectangle?" New topic
Author

how can I use JRadioButton to color a rectangle?

Rylann S. Reave
Greenhorn

Joined: Nov 23, 2012
Posts: 8

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: 39409
    
  28
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

Joined: Aug 07, 2007
Posts: 2316
    
  49
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

Joined: Nov 23, 2012
Posts: 8

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

Joined: Nov 23, 2012
Posts: 8

maybe if I send you the code through this? I dunno and i've been fretting with this all morning
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2517
    
    8

what exception you getting in main? Are you getting the values from the radio button group? Can the button group access the rectangle?


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Rylann S. Reave
Greenhorn

Joined: Nov 23, 2012
Posts: 8

nope
Rylann S. Reave
Greenhorn

Joined: Nov 23, 2012
Posts: 8

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
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

Joined: Aug 07, 2007
Posts: 2316
    
  49
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: 39409
    
  28
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

Joined: Nov 23, 2012
Posts: 8

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

Joined: Nov 23, 2012
Posts: 8

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

Joined: Aug 07, 2007
Posts: 2316
    
  49
(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: 39409
    
  28
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

Joined: Nov 23, 2012
Posts: 8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how can I use JRadioButton to color a rectangle?