Changing button color

hi,
i am writing an applet and i have many buttons. when you click on a button i want to check what color it currently is and change it appropiatly.
i have hit a brick wall. i can set the color no probs with;
onea.setBackGround(red);
but how would you go about comparing onea's color with my two colors red and green?
thanks

Is this a project for school?
Anyway, you could get the RGB int values and compare them one by one...Like
int currentRed = someColor.getRed();
int currentGreen = someColor.getGreen();
int currentBlue = someColor.getBlue();
int anotherRed = anotherColor.getRed();
int anotherGreen = anotherColor.getGreen();
int anottherBlue = anotherColor.getBlue();
// now compare the two colors
if ( currentRed == anotherRed &&
currentGreen == anotherGreen &&
currentBlue == anotherBlue )
// if we get here then the two Colors are equivalent
A more professional solution would be to create a custom Comparator by implementing the Comparator interface. From there you could manually call compare( currentColor, anotherColor );
Also, with a custom Comparator you could store multiple Color objects in a List and then use the Arrays class with the Comparator to perform sorting and lookup.
For simplicity sake, just create a convenience method to check two colors of your choosing. Like:
public int myColorComparingMethod( Color colorA, Color colorB )
// compare here and return an int based upon order
Eric
P.S. - you should offer Duke Dollars when you post, it will encourage more people to offer responses to your questions.

Similar Messages

  • How to change button colors in a loop?

    I am working on a task that should imitate an elevator. I have two vertical
    rows of round buttons "Up" and "Down" When a circle is selected randomly by
    the program, the circle becomes yellow and the elevator moves to that
    button.
    Here is what I did:
    1. created a class Circle where I save buttons' parameters
    2. saved Circle objects in an array
    3. drew the buttons depending on their parameters
    4. generated a random number, matched it with an array index and assigned
    the object color to yellow.
    Everything is fine except that I can't figure out how to change colors of my
    buttons in a loop.
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    public class Elevator3 extends JPanel
    private int n = 40;
    private int width = 200;
    private int floors = 10;
    private int interval = 1000;
    private boolean selected = false;
    private Circle[] buttons = new Circle[2*(floors-1)];
    public Elevator3()
    build();
    JFrame frame = new JFrame("Elevator3");
    setBackground(Color.WHITE);
    setFont(new Font("SansSerif", Font.PLAIN, Math.round(n/3)));
    frame.getContentPane().add(this);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(width, n*(floors+2) );
    frame.setVisible(true);
    public void build()
    Random r = new Random();
    int buttonPick;
    int timeUntilNextButton = r.nextInt(interval);
    for (int k =0; ; k++)
    if (timeUntilNextButton-- ==0)
    buttonPick = r.nextInt(2*(floors-1));
    //populate my buttons array here - how??
    timeUntilNextButton = r.nextInt(interval);
    //adding "Down" buttons
    for (int i=1, count=0; i < floors; i++, count++)
    if (count == buttonPick)
    selected = true;
    else
    selected = false;
    buttons[count]= new Circle(n*2, n*i, selected, Math.round(n/2));
    //build an array of "Up" circles
    for (int i=2, count=floors-1; i < floors+1; i++, count++)
    if (count == buttonPick)
    selected = true;
    else
    selected = false;
    buttons[count]= new Circle(n, n*i, selected, Math.round(n/2));
    public static void main(String[] args)
    new Elevator3();
    protected void paintComponent(Graphics g)
    super.paintComponent(g);
    //draw buttons
    for (int i=0; i < buttons.length; i++)
    g.setColor(buttons.getColor());
    g.fillOval(buttons[i].getX(), buttons[i].getY(), buttons[i].getWidth(), buttons[i].getWidth());
    class Circle
    private int x;
    private int y;
    private Color c;
    private boolean pressed;
    private int width;
    public Circle(int xCoordinate, int yCoordinate, boolean selected, int diameter)
    x = xCoordinate;
    y = yCoordinate;
    pressed = selected;
    width = diameter;
    if (pressed)
    c = Color.YELLOW;
    else
    c = Color.LIGHT_GRAY;
    public Color getColor()
    return c;
    public int getX()
    return x;
    public int getY()
    return y;
    public int getWidth()
    return width;

    hi,
    am sorry, i couldn't make out what exactly the problem, but as ur subject line says...
    may be the code give below will help you to change button colors in a loop..
              for(int i = 0; i < button.length; i++){
                   int color1 = (int)(250*Math.random());
                   int color2 = (int)(250*Math.random());
                   int color3 = (int)(250*Math.random());
                   Color c = new Color(color1,color2,color3);
                   button[i] = new JButton("button name");
                   button.addActionListener(this);
                   //to check the r, g, b combination.
                   //System.out.println(c);
                   button[i].setBackground(c);
                   button[i].setForeground(Color.white);
    //adding into the panel
                   panel.add(button[i]);
    hope this would help you.

  • Change button color

    i have a group of buttons and when each one is clicked its color should change. when another button is clicked the color of previous button should change back to normal..plz help..thanks in advance

    Hi Srivatsa,
    You can do the same for chnanging your button icons if you have only tow icons to display ....one is default icon and the other one is the icon which you wnat to display when the button is clicked...
    Check the below code:
    Just replace the image path in the embed variable with your image path and check...
    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script>
      <![CDATA[
       private var previousButtonClicked:Button;
       [Embed("assets/Rose20Icon.jpg") ]
             [Bindable]private var buttonIcon:Class;
             [Embed("assets/arrowIcon.JPG") ]
             [Bindable]private var buttonIconDefault:Class;
       private function buttonClickHandler(event:MouseEvent):void
        if(previousButtonClicked)
         previousButtonClicked.setStyle("icon",buttonIconDefault);
        previousButtonClicked = event.currentTarget as Button;
        Button(event.currentTarget).setStyle("icon",buttonIcon);
      ]]>
    </mx:Script>
    <mx:HBox>
      <mx:Button id="btn1" icon="{buttonIconDefault}" label="Button" color="green" click="buttonClickHandler(event);"/>
      <mx:Button id="btn2" icon="{buttonIconDefault}" label="Button" color="green" click="buttonClickHandler(event);"/>
      <mx:Button id="btn3" icon="{buttonIconDefault}" label="Button" color="green" click="buttonClickHandler(event);"/>
      <mx:Button id="btn4" icon="{buttonIconDefault}" label="Button" color="green" click="buttonClickHandler(event);"/>
      <mx:Button id="btn5" icon="{buttonIconDefault}" label="Button" color="green" click="buttonClickHandler(event);"/>
      <mx:Button id="btn6" icon="{buttonIconDefault}" label="Button" color="green" click="buttonClickHandler(event);"/>
      <mx:Button id="btn7" icon="{buttonIconDefault}" label="Button" color="green" click="buttonClickHandler(event);"/>
    </mx:HBox>
    </mx:Application>
    Thanks,
    Bhasker Chari 

  • How can i change the color of the text on a button in Roll Over or Click state (Cs5.5)

    Hello,
    Here is the problematic:
    1° How can i change the color of the text on a button in Roll Over or Click state (Cs5.5)
    - I've many button in a doc im creating
    - the color of the buttons is white with black stoke & black text
    - in roll over state i want them to inverse the color coding (the text white & bottom black)
    - currently the text becomes invisible as black on black cant be seen (ive stroked it white but that looks ugly)
    - i would just like to know if there is a function made for this?
    My other questions:
    2° Ive a presentation on which Ive created buttons which tigger pop-up animations
    - i have 5 buttons which trigger animations
    - the viewer can chose to Click on any of the buttons randomly
    - the problem is once the animation is open how can it be close... either:
    a) by it self after "40" seconds
    b) when ther viewer clicks another button
    3° Ive created buttons linking pages..
    - "go to page xxx"
    - when i do a preview and click on the buttons the links dont take me to the right pages
    - is that normal? (maybe in preview its suppose to be like that)
    4° I would like to intergrate YouTube videos in the presentation so they play inside the docment.. when i tried.. it said the 'link isnt a flash video'
    - what should i do?
    5° Once i export it into Swf. how how can i visualize it? or put it online?
    Thank you for your time.

    stlbbl4u wrote:
    Hello,
    Here is the problematic:
    1° How can i change the color of the text on a button in Roll Over or Click state (Cs5.5)
    - I've many button in a doc im creating
    - the color of the buttons is white with black stoke & black text
    - in roll over state i want them to inverse the color coding (the text white & bottom black)
    - currently the text becomes invisible as black on black cant be seen (ive stroked it white but that looks ugly)
    - i would just like to know if there is a function made for this?
    You should setup Object Styles - these can also include Paragarph Styles that can be triggered when the Style is selected.
    http://help.adobe.com/en_US/indesign/cs/using/WS5CEDB81A-0011-4dc9-9DE8-AC7AD4C80076a.html
    My other questions:
    2° Ive a presentation on which Ive created buttons which tigger pop-up animations
    - i have 5 buttons which trigger animations
    - the viewer can chose to Click on any of the buttons randomly
    - the problem is once the animation is open how can it be close... either:
    a) by it self after "40" seconds
    b) when ther viewer clicks another button
    I'm not sure what you mean - I don't do files with Animations
    3° Ive created buttons linking pages..- "go to page xxx"
    - when i do a preview and click on the buttons the links dont take me to the right pages
    - is that normal? (maybe in preview its suppose to be like that)
    Does it work properly when you export it to the finished file?
    4° I would like to intergrate YouTube videos in the presentation so they play inside the docment.. when i tried.. it said the 'link isnt a flash video'- what should i do?
    InDesign won't link directly to youtube videos. I think your best bet would be to use Downloader to download the video and embed them directly in Indesign.
    5° Once i export it into Swf. how how can i visualize it? or put it online? 
    Thank you for your time.
    You can look up how to embed a SWF file into your HTML - but it depends what you mean by "online" there's a dozen ways to get a file "online".

  • How can I change the color of the new tab button?

    Hi,
    I was wondering if there was a way to change the color of the new tab button, list all tab button, and tab groups button. I have been searching for personas that don't make it hard to see those particular buttons for days now, but since I can't seem to find any I was hoping there was a way to change those buttons' colors to make them more visible with the personas.
    [http://i54.tinypic.com/2ui9351.jpg Here's a picture of the situation]
    I want to change those 3 buttons (and the "show more bookmarks" button that appears when the window is too small to show them all) to white or some other more visible color. I know there's a way to change the background of those buttons, but I'd prefer if I could change the '+' button itself in the new tab button for example. Is there any way to do it? Because it's so hard to find personas that don't make it hard to see text or buttons.
    Thanks.

    jus an example
    Ian F
    Since LabVIEW 5.1... 7.1.1... 2009, 2010
    依恩与LabVIEW
    LVVILIB.blogspot.com
    Attachments:
    Blinking_Indicator_2003.vi ‏27 KB

  • How can I change the color of the buttons in Firefox 8?

    I have Stylish, and had a code to change the button colors (Home, Forward/Back, Reload, etc.). But now it isn't working. Is there an HTML code in order to be able to do this with Firefox 8? I would also like a code to change the background color of the Bookmarks Toolbar, if there is one.
    Thanks!

    The box will print... but there is no way to just assign a color to a box. The closest you can get is ot create a small solid colored image in Photoshop and assign that to the box. You may get a resolution warning but as long as it's a solid color it does not matter how few pixels you have.

  • How do I change the color of a button in swing?

    I'm using swing to create an interface and it's made up mainly of buttons. Gray isn't exactly the prettiest of colors and I'd love if I could change the color.
    Cheers,
    elmicko

    Hi!
    You can set the color of each button independently using:
    JButton1.setBackground(Color.BLUE); // for the background
    JButton1.setForeground(Color.CYAN); // for the text displayedOr if you want all the button to have the same color you can overwrite the look and feel of the application using:
    javax.swing.UIManager.put("Button.background", new Color(230, 230, 230));

  • How can i change the color of a blinking button?

    Dear all,
    how can i change the color of a blinking button with property nodes, when the button changes from "normal" state to blinking state? I can change the color of the normal state, but how can I change the color when the button is in blinking state?
    thanks

    jus an example
    Ian F
    Since LabVIEW 5.1... 7.1.1... 2009, 2010
    依恩与LabVIEW
    LVVILIB.blogspot.com
    Attachments:
    Blinking_Indicator_2003.vi ‏27 KB

  • I want to change the color of the firefox button, the menu button is normaly orange what file/line in the .xpi do i need to edit to change it

    I just want to change the color of the button but there so many files so much code, and i just cant find a page that will tell me please help

    You can change the color by using the App Button Color add-on - https://addons.mozilla.org/firefox/addon/app-button-color/

  • Change pressed button Color

    Hi,
    I want to change my pressed button Color and I haven't been able to find a way, I tried creating my own button and add a listener when it is pressed and then change its background color but it doesn't work! it works for enter and exit but not for pressed and released.
    any one has an idea?
    thanks
    Here's my code:
    package components;
    import javax.swing.*;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.*;
    * JButton con una nueva propiedad de Color presionado
    * @author Ana Mar�a Franco
    * @version 1.0
    public class PressedColorButton extends JButton
        // Atributos ************************************************************************
        private Color unpressedButtonColor;
        private Color pressedButtonColor;
        // Constructores ********************************************************************
        public PressedColorButton()
            super();
            this.addMouseListener(new PressedColorButton_this_mouseAdapter(this));
        // M�todos **************************************************************************
         * Obtiene el color sin presionar
         * @return Color
        public Color getUnpressedButtonColor()
            return this.unpressedButtonColor;
         * Define el color sin presionar
         * @param nuevoColor Color
        public void setUnpressedButtonColor(Color nuevoColor)
            this.unpressedButtonColor = nuevoColor;
         * Obtiene el color presionado
         * @return Color
        public Color getPressedButtonColor()
            return this.pressedButtonColor;
         * Define el color presionado
         * @param nuevoColor Color
        public void setPressedButtonColor(Color nuevoColor)
            this.pressedButtonColor = nuevoColor;
        public void this_mouseExited(MouseEvent e)
            this.setBackground(unpressedButtonColor);
        public void this_mousePressed(MouseEvent e)
            this.setBackground(pressedButtonColor);
        public void this_mouseReleased(MouseEvent e)
            this.setBackground(unpressedButtonColor);
        public void this_mouseEntered(MouseEvent e)
            this.setBackground(pressedButtonColor);
    // Escuchadores *************************************************************************
    class PressedColorButton_this_mouseAdapter extends MouseAdapter
        private PressedColorButton adaptee;
        PressedColorButton_this_mouseAdapter(PressedColorButton adaptee)
            this.adaptee = adaptee;
        public void mouseExited(MouseEvent e)
            adaptee.this_mouseExited(e);
        public void mousePressed(MouseEvent e)
            adaptee.this_mousePressed(e);
        public void mouseReleased(MouseEvent e)
            adaptee.this_mouseReleased(e);
        public void mouseEntered(MouseEvent e)
            adaptee.this_mouseEntered(e);
    }

    Swing related questions should be posted in the Swing forum. Try setting the following properties on the button:
    setContentAreaFilled( false );
    setOpaque(true);

  • Theme colors - Change the color of buttons

    Hi
    I went through the tuttorial, "Adding Styles to Components"
    I were able to change different properties of buttons by creating a Style Class with the same name as that of the theme but with different style attributes.
    This had the desired effect except for the background color.
    I played around abit with the style editor settings of a button with a theme applied on it, and found that when you are in the background tab of the editor and you select your desired new color, that there are no changes EXCEPT for when you select the " Repeat-y" from the "Tile" drop-down list. Only problem is you get a funny stripe through the middle of the button.
    Is this the only way to change the color of buttons, linked to a theme, to a new color?
    Any Advise
    Thank you

    To find the style properties defined in class Btn2, go to the theme's read-only css_master.css file:
    In the Projects window, expand Libraries > Default Theme > com.sun.rave.web.ui.defaultheme.css.
    Double-click the css_master.css file to open the file.
    Search the file for the style class of interest, such as Btn2.
    For example, to set a Btn2 background-color, both background-color and background-image must be defined to override the existing style class. Since you cannot make edits to the read-only css_master.css, use the style property editor to set the background-image to null and the background-color to the one desired for your component.

  • Changing box color with buttons

    I'm kind of new to this whole flash thing...but here's what i
    have:
    I have a couple different .fla files that are opened via
    buttons on a main "index" file.
    It's probably easier for me to give an example:
    Let's say I have files: "index" (as the main page), then
    "page 1," "page 2," "page 3," and "page 4." On the "index" page, i
    have buttons that display the other pages; 1,2,3, and 4.
    What i'm trying to do is have three different boxes, say a,
    b, and c on page 2, each one representing a different piece of
    equipment.
    On the additional pages, i'll have three buttons...each one
    changing the color of one of the boxes. For example...button 1 will
    turn "box a" green, button 2 will turn it yellow, etc... Then when
    you go to the next page, button 1 will turn "box b" green, etc...
    I've been doing tutorials, but i just cant figure out how to
    code this. If anybody can help, please email me
    Here
    I also have a sample file i can send.
    Thanks!

    thanks for the reply dzedward....
    So I right clicked on the button I want to use this with and
    pasted this code. I changed the (box.mc) to (box_a)...the name of
    the color box. When i run it, an error comes up "Statement must
    appear within on handler" and the source is
    "btn.onRelease=function(){
    I don't know if i'm supposed to change something else, but i
    tried it with "btn1.onRelease" also (the name of the button)
    All help is appreciated.
    Thanks.

  • How to change highlight color of a focus Button

    When a Button gained focus, there is a dotted line around the button label. I'd like to change the color. Not able to find out how. Is it possible? Please help.

    Hi,
    Use the following methods to that label and set its values to false
    void setBorderPainted(boolean b)
    void setContentAreaFilled(boolean b)
    void setFocusPainted(boolean b)
    This will work out for you

  • Programatically changing the color of buttons on LV OI

    Hi,
    I've modified the sample Operator Interface shipped with TestStand.
    Buttons are linked to subsequences, so when a button is pushed a subsequence is running (boolean buttons).
    What I would like to achieve is that, once a subsequence was called and the result was OK (pass) then the color of the button should change to green.
    If the subsequence is failed then the button should change to red.
    Thanks for any suggestions in advance.
    Solved!
    Go to Solution.

    Hi,
    You can find an example in Example Finder: "Property Nodes.vi" . It shows how you can change any properties for an object from your program.
    You can find more examples on zone.ni.com, like these:
    - Using Property Nodes to Change the Color of a Slide Bar https://decibel.ni.com/content/docs/DOC-2133
    - Programatically Change the Color and Visibility of a Boolean using Property Nodes https://decibel.ni.com/content/docs/DOC-16569
    Regards,
    Peter Vago
    AE Hungary

  • Change the Colors of Expand, Minimize and Close Window Buttons

    How do I change the colors of the Expand, Minimize and Close window buttons?
    Red, green, and yellow are not conducive to red-green colorblind individuals, and the gray scheme in the Graphite appearance is too dull.

    If the change in theme can do it, there must be an alternative.

Maybe you are looking for

  • Product: BusinessObjects Edge Series 3.0 -- Error 2902. Operation ixfAssemb

    Good Afternoon. I want to upgrade XIR3 to XIR3.1 for enabling IIS+SSO with Active Directory. But when I want to upgrade my system, I have the Following error: Product: BusinessObjects Edge Series 3.0 -- Error 2902. Operation ixfAssemblyCopy called ou

  • How to implement more than one AP in a LAN

    Hi If I take two basic LinkSys access points, and want to set it up in my LAN, what extra configuration do I have to do than configuring only one AP in my LAN? I'm talking about minimum configuration. So, if I take only one LinkSys AP, I would config

  • How to put material and service in the same order

    I want to put the material and service in the same order. How do it input different taxes. and also migo for one material item and other service item.

  • Can't get mac back to how it was following manual restore from Time Machine

    My MBP, 13-inch, early 2011 10.8.4 has an SSD to boot from in HD bay and a HD in the optical bay where I have my user folder. I dropped the mac when it was switched on and the HD got screwed up and I consequently had to initialise it. The drive is fi

  • HP Photosmart 4400 Series, All-in-one C4480

    My Printer has worked great for years, I recently moved and my Scanner when trying to Scan to PC has not worked ever since. I tried Uninstalling my printer and re-installing using my original disc. Everything says it was successfully re-installed. Bu